Module: Roles::ActiveRecord::ClassMethods

Defined in:
lib/roles_active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#default_roleObject



29
30
31
# File 'lib/roles_active_record/base.rb', line 29

def default_role
  self.to_s.gsub(/(.+)User$/, '\1').underscore.to_sym
end

#default_role=(drole) ⇒ Object



33
34
35
# File 'lib/roles_active_record/base.rb', line 33

def default_role= drole
  @default_role = drole
end

#strategies_with_role_classObject



45
46
47
# File 'lib/roles_active_record/base.rb', line 45

def strategies_with_role_class
  [:one_role, :many_roles]
end

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

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/roles_active_record/base.rb', line 53

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

  set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name

  # one_role reference
  if strategy_name == :one_role
    self.belongs_to :one_role, :foreign_key => 'role_id', :class_name => role_class_name.to_s
  end

  # many_roles references
  if strategy_name == :many_roles
    urc = user_roles_class options
    instance_eval many_roles_stmt(urc)
  end

  set_role_strategy name, options
end

#valid_multi_strategiesObject



41
42
43
# File 'lib/roles_active_record/base.rb', line 41

def valid_multi_strategies
  [:many_roles, :roles_mask]
end

#valid_single_strategiesObject



37
38
39
# File 'lib/roles_active_record/base.rb', line 37

def valid_single_strategies
  [:admin_flag, :one_role, :role_string]
end

#valid_strategiesObject



49
50
51
# File 'lib/roles_active_record/base.rb', line 49

def valid_strategies
  valid_single_strategies + valid_multi_strategies
end