Sha256: b0bbaa493473fd45e906c6848fef69d2797d040332cf3232588c928cd54227b8

Contents?: true

Size: 1.34 KB

Versions: 7

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

7 entries across 7 versions & 1 rubygems

Version Path
outpost-cms-0.1.4 app/helpers/authorization_helper.rb
outpost-cms-0.1.3 app/helpers/authorization_helper.rb
outpost-cms-0.1.2 app/helpers/authorization_helper.rb
outpost-cms-0.1.1 app/helpers/authorization_helper.rb
outpost-cms-0.1.0 app/helpers/authorization_helper.rb
outpost-cms-0.0.5 app/helpers/authorization_helper.rb
outpost-cms-0.0.4 app/helpers/authorization_helper.rb