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



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

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



49
50
51
52
53
# File 'lib/i_am_i_can/role/assignment.rb', line 49

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



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

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



17
18
19
20
21
# File 'lib/i_am_i_can/role/assignment.rb', line 17

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



13
14
15
# File 'lib/i_am_i_can/role/assignment.rb', line 13

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

#is_not_a_temporary(*roles) ⇒ Object



23
24
25
# File 'lib/i_am_i_can/role/assignment.rb', line 23

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

#is_only_a(*roles) ⇒ Object



27
28
29
30
31
# File 'lib/i_am_i_can/role/assignment.rb', line 27

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