| Module | UsersHelper |
| In: |
app/helpers/users_helper.rb
|
Use this to wrap view elements that the user can‘t access. !! Note: this is an interface, not security feature !! You need to do all access control at the controller level.
Example: <%= if_authorized?(:index, User) do link_to(‘List all users’, users_path) end %> | <%= if_authorized?(:edit, @user) do link_to(‘Edit this user’, edit_user_path) end %> | <%= if_authorized?(:destroy, @user) do link_to ‘Destroy’, @user, :confirm => ‘Are you sure?’, :method => :delete end %>
# File app/helpers/users_helper.rb, line 17
17: def if_authorized?(action, resource, &block)
18: if authorized?(action, resource)
19: yield action, resource
20: end
21: end
Link to the current user‘s page (using link_to_user) or to the login page (using link_to_login_with_IP).
# File app/helpers/users_helper.rb, line 85
85: def link_to_current_user(options={})
86: if current_user
87: link_to_user current_user, options
88: else
89: content_text = options.delete(:content_text) || 'not signed in'
90: # kill ignored options from link_to_user
91: [:content_method, :title_method].each{|opt| options.delete(opt)}
92: link_to_login_with_IP content_text, options
93: end
94: end
Link to login page using remote ip address as link content
The :title (and thus, tooltip) is set to the IP address
Examples:
link_to_login_with_IP # => <a href="/login" title="169.69.69.69">169.69.69.69</a> link_to_login_with_IP :content_text => 'not signed in' # => <a href="/login" title="169.69.69.69">not signed in</a>
# File app/helpers/users_helper.rb, line 70
70: def link_to_login_with_ip content_text=nil, options={}
71: ip_addr = request.remote_ip
72: content_text ||= ip_addr
73: options.reverse_merge! :title => ip_addr
74: if tag = options.delete(:tag)
75: content_tag tag, h(content_text), options
76: else
77: link_to h(content_text), login_path, options
78: end
79: end
Link to user‘s page (‘users/1’)
By default, their login is used as link text and link title (tooltip)
Takes options
Examples:
link_to_user @user
# => <a href="/users/3" title="barmy">barmy</a>
# if you've added a .name attribute:
content_tag :span, :class => :vcard do
(link_to_user user, :class => 'fn n', :title_method => :login, :content_method => :name) +
': ' + (content_tag :span, user.email, :class => 'email')
end
# => <span class="vcard"><a href="/users/3" title="barmy" class="fn n">Cyril Fotheringay-Phipps</a>: <span class="email">barmy@blandings.com</span></span>
link_to_user @user, :content_text => 'Your user page'
# => <a href="/users/3" title="barmy" class="nickname">Your user page</a>
# File app/helpers/users_helper.rb, line 49
49: def link_to_user(user, options={})
50: raise "Invalid user" unless user
51: options.reverse_merge! :content_method => :login, :title_method => :login, :class => :nickname
52: content_text = options.delete(:content_text)
53: content_text ||= user.send(options.delete(:content_method))
54: options[:title] ||= user.send(options.delete(:title_method))
55: link_to h(content_text), user_path(user), options
56: end