Module: IAmICan::Role::Assignment

Defined in:
lib/i_am_i_can/role/assignment.rb

Instance Method Summary collapse

Instance Method Details

#_roles_assignment(action = :assign, roles, save, exp: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/i_am_i_can/role/assignment.rb', line 39

def _roles_assignment(action = :assign, roles, save, exp: nil)
  instances, names = Role.extract(roles.flatten, i_am_i_can)
  if save
    assignment = _stored_roles_exec(action, instances, name: names)
    _set_roles_expire(exp, assignment.map(&:id)) if exp
  else
    assignment = _temporary_roles_exec(action, instances, name: names)
  end

  ResultOf.role assignment, i_am_i_can, given: [instances, names]
end

#_set_roles_expire(exp, ids) ⇒ Object



51
52
53
54
55
# File 'lib/i_am_i_can/role/assignment.rb', line 51

def _set_roles_expire(exp, ids)
  send("assoc_with_#{__roles}")
      .where("#{i_am_i_can.role_class.underscore}_id" => ids)
      .update_all(expire_at: exp)
end

#becomes_a(*roles, which_can: [ ], obj: nil, _d: i_am_i_can.auto_definition, auto_definition: _d || which_can.present?, expires_in: nil, expires_at: (expires_in.after if expires_in), save: true) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/i_am_i_can/role/assignment.rb', line 6

def becomes_a *roles, which_can: [ ], obj: nil,
              _d: i_am_i_can.auto_definition, auto_definition: _d || which_can.present?,
              expires_in: nil, expires_at: (expires_in.after if expires_in), save: true
  self.class.have_roles *roles, which_can: which_can, obj: obj if auto_definition
  run_callbacks(:role_assign) do
    _roles_assignment(roles, save, exp: expires_at)
  end
end

#falls_from(*roles, saved: true) ⇒ Object



19
20
21
22
23
# File 'lib/i_am_i_can/role/assignment.rb', line 19

def falls_from *roles, saved: true
  run_callbacks(:cancel_role_assign) do
    _roles_assignment(:cancel, roles, saved)
  end
end

#is_a_temporary(*roles, **options) ⇒ Object



15
16
17
# File 'lib/i_am_i_can/role/assignment.rb', line 15

def is_a_temporary *roles, **options
  becomes_a *roles, save: false, **options
end

#is_not_a_temporary(*roles) ⇒ Object



25
26
27
# File 'lib/i_am_i_can/role/assignment.rb', line 25

def is_not_a_temporary *roles
  falls_from *roles, saved: false
end

#is_only_a(*roles) ⇒ Object



29
30
31
32
33
# File 'lib/i_am_i_can/role/assignment.rb', line 29

def is_only_a *roles
  run_callbacks(:role_update) do
    _roles_assignment(:replace, roles, true)
  end
end