Module: UsersHelper
- Defined in:
- lib/branston/app/helpers/users_helper.rb
Instance Method Summary collapse
-
#if_authorized?(action, resource, &block) ⇒ Boolean
Use this to wrap view elements that the user can’t access.
-
#link_to_current_user(options = {}) ⇒ Object
Link to the current user’s page (using link_to_user) or to the login page (using link_to_login_with_IP).
-
#link_to_login_with_IP(content_text = nil, options = {}) ⇒ Object
Link to login page using remote ip address as link content.
-
#link_to_user(user, options = {}) ⇒ Object
Link to user’s page (‘users/1’).
Instance Method Details
#if_authorized?(action, resource, &block) ⇒ Boolean
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 %>
14 15 16 17 18 |
# File 'lib/branston/app/helpers/users_helper.rb', line 14 def (action, resource, &block) if (action, resource) yield action, resource end end |
#link_to_current_user(options = {}) ⇒ Object
Link to the current user’s page (using link_to_user) or to the login page (using link_to_login_with_IP).
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/branston/app/helpers/users_helper.rb', line 82 def link_to_current_user(={}) if current_user link_to_user current_user, else content_text = .delete(:content_text) || 'not signed in' # kill ignored options from link_to_user [:content_method, :title_method].each{|opt| .delete(opt)} link_to_login_with_IP content_text, end end |
#link_to_login_with_IP(content_text = nil, options = {}) ⇒ Object
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>
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/branston/app/helpers/users_helper.rb', line 67 def link_to_login_with_IP content_text=nil, ={} ip_addr = request.remote_ip content_text ||= ip_addr .reverse_merge! :title => ip_addr if tag = .delete(:tag) content_tag tag, h(content_text), else link_to h(content_text), login_path, end end |
#link_to_user(user, options = {}) ⇒ Object
Link to user’s page (‘users/1’)
By default, their login is used as link text and link title (tooltip)
Takes options
-
:content_text => ‘Content text in place of user.login’, escaped with the standard h() function.
-
:content_method => :user_instance_method_to_call_for_content_text
-
:title_method => :user_instance_method_to_call_for_title_attribute
-
as well as link_to()‘s standard 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">[email protected]</span></span>
link_to_user @user, :content_text => 'Your user page'
# => <a href="/users/3" title="barmy" class="nickname">Your user page</a>
46 47 48 49 50 51 52 53 |
# File 'lib/branston/app/helpers/users_helper.rb', line 46 def link_to_user(user, ={}) raise "Invalid user" unless user .reverse_merge! :content_method => :login, :title_method => :login, :class => :nickname content_text = .delete(:content_text) content_text ||= user.send(.delete(:content_method)) [:title] ||= user.send(.delete(:title_method)) link_to h(content_text), user_path(user), end |