Sha256: 3efb7c46cecffd62410f204d09a89f1e26ac178d2e3e9c5aac1f78eaaa8e625e

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module AuthorizationHelper
  # Public: Block out whole chunks of code based on permissions. 
  #
  # resource - (Class) The model that the user must have permission 
  #            to manage in order to see the block.
  # message  - (String) The message to display if the user isn't 
  #            authorized (default: "").
  # block    - The block that will be captured if the user is authorized.
  #            Should return a String.
  #
  # Examples
  #
  #   <%= guard Post, "You do not have permission to view this" do %>
  #     <%= @post.headline %>
  #   <% end %>
  #
  # Returns String of either the message or the captured block.
  def guard(resource, message=nil, &block)
    if current_user.can_manage?(resource)
      capture(&block)
    else
      message.to_s
    end
  end

  # Public: Conditionally link text based on permissions.
  #
  # resource - (Class) The model that the user must have permission
  #            to manage in order to see the link.
  # args     - Arguments to be passed directly to +link_to+ if necessary.
  #
  # Examples
  #
  #   <%= guarded_link_to Post, @post.headline, edit_post_path(@post) %>
  #
  # Returns String of either a link tag, or just the link title.
  def guarded_link_to(resource, *args)
    if current_user.can_manage?(resource)
      link_to *args
    else
      args[0] # Just the link title
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
outpost-cms-0.0.3 app/helpers/authorization_helper.rb