Sha256: d44825b1f42eff8745a29672d6d75f1bbb5adb219aba354d8e6512cf5cb2ae1c

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Acl9
  module Helpers
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def access_control(method, opts = {}, &block)
        subject_method = opts.delete(:subject_method) || Acl9::config[:default_subject_method]
        raise ArgumentError, "Block must be supplied to access_control" unless block

        generator = Acl9::Dsl::Generators::HelperMethod.new(subject_method, method)

        generator.acl_block!(&block)
        generator.install_on(self, opts)
      end

    end

    # Usage:
    #
    #     <% show_to(:owner, :supervisor, :of => :account) do -%>
    #       <%= 'hello' %>
    #     <%- end -%>
    #
    def show_to(*args, &block)
      user = eval(Acl9.config[:default_subject_method].to_s)
      return '' if user.nil?

      has_any = false

      if args.last.is_a?(Hash)
        an_obj  = args.pop.values.first
        has_any = args.detect { |role| user.has_role?(role, an_obj) }
      else
        has_any = args.detect { |role| user.has_role?(role) }
      end

      has_any ? yield(:block) : ''
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
ktopping_acl9-0.13.0 lib/acl9/helpers.rb
ktopping_acl9-0.12.0 lib/acl9/helpers.rb
acl9-0.12.0 lib/acl9/helpers.rb