Module: Law::Legalize

Extended by:
ActiveSupport::Concern
Defined in:
lib/law/legalize.rb

Instance Method Summary collapse

Instance Method Details

#adjudicated?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/law/legalize.rb', line 17

def adjudicated?
  judgement.try(:adjudicated?) || false
end

#authorize(action = nil, object: nil, petitioner: nil, permissions: nil, parameters: nil, law_class: nil) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
# File 'lib/law/legalize.rb', line 40

def authorize(action = nil, object: nil, petitioner: nil, permissions: nil, parameters: nil, law_class: nil)
  action ||= try(:action_name)
  parameters ||= try(:params)

  raise ArgumentError, "an action is required" if action.nil?

  options = { permissions: permissions, parameters: parameters, law_class: law_class }
  @judgement = law(object, petitioner, **options).authorize(action)
  authorized?
end

#authorize!(action = nil, **options) ⇒ Object



36
37
38
# File 'lib/law/legalize.rb', line 36

def authorize!(action = nil, **options)
  authorize(action, **options) or raise Law::NotAuthorizedError
end

#authorized?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/law/legalize.rb', line 13

def authorized?
  judgement.try(:authorized?) || false
end

#law(object = nil, petitioner = nil, permissions: nil, parameters: nil, law_class: nil) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/law/legalize.rb', line 25

def law(object = nil, petitioner = nil, permissions: nil, parameters: nil, law_class: nil)
  object ||= @record || try(:controller_name)&.singularize&.camelize&.safe_constantize
  petitioner ||= try(:current_user)
  permissions ||= petitioner.try(:permissions)
  law_class ||= object.try(:conjugate, Law::LawBase)

  raise ArgumentError, "a Law is required" unless law_class.is_a?(Class)

  law_class.new(permissions: permissions, source: petitioner, target: object, params: parameters)
end

#violationsObject



21
22
23
# File 'lib/law/legalize.rb', line 21

def violations
  judgement.try(:violations) || []
end