Module: Roles::Mongoid::ClassMethods

Defined in:
lib/roles_mongoid/base.rb

Constant Summary collapse

MAP =
{
  :admin_flag   => "field :admin_flag, :type => Boolean",
  
  :many_roles_old   => "references_many :many_roles, :stored_as => :array, :class_name => 'Role'",
  :many_roles       => "references_and_referenced_in_many :many_roles, :class_name => 'Role'",
  :one_role         => "referenced_in :one_role, :class_name => 'Role'",

  :embed_many_roles   => "embeds_many :many_roles, :class_name => 'Role'",
  :embed_one_role     => "embeds_one :one_role, :class_name => 'Role'",

  :roles_mask   => "field :roles_mask, :type => Integer, :default => 0",
  :role_string  => "field :role_string, :type => String",
  :role_strings => "field :role_strings, :type => Array",
  :roles_string => "field :roles_string, :type => String"
}

Instance Method Summary collapse

Instance Method Details

#strategy(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/roles_mongoid/base.rb', line 33

def strategy name, options = {}
  strategy_name = name.to_sym
  raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
  use_roles_strategy strategy_name

  set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name
  
  if default_options?(options) && MAP[strategy_name]
    instance_eval statement(MAP[strategy_name])
  end       

  case name
  when :embed_one_role
    raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
    @role_class_name.embedded_in :user, :inverse_of => :one_role
  when :embed_many_roles
    raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
    @role_class_name.embedded_in :user, :inverse_of => :many_roles
  end

  set_role_strategy strategy_name, options
end