Sha256: 22c170e59c9dbc035c9bdfb04034a355dc7cb44ab9263d3a4d3c8ea3b420799c

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "rolify/configure"

module Rolify
  module Dynamic
    def load_dynamic_methods
      if ENV['ADAPTER'] == 'mongoid'
        # for compatibility with MongoidDB - does not support polymorphic includes
        self.role_class.all.each do |r|
          define_dynamic_method(r.name, r.resource)
        end
      else
        # otherwise should be able to support polymorphic includes.
        # supported Rails version >= 3.2 with AR should use find_each, since use of .all.each is deprecated
        self.role_class.includes(:resource).find_each do |r|
          define_dynamic_method(r.name, r.resource)
        end
      end
    end

    def define_dynamic_method(role_name, resource)
      class_eval do 
        define_method("is_#{role_name}?".to_sym) do
          has_role?("#{role_name}")
        end if !method_defined?("is_#{role_name}?".to_sym)

        define_method("is_#{role_name}_of?".to_sym) do |arg|
          has_role?("#{role_name}", arg)
        end if !method_defined?("is_#{role_name}_of?".to_sym) && resource
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rolify-4.1.0 lib/rolify/dynamic.rb