Module: Law::Legalize
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/law/legalize.rb
Instance Method Summary collapse
- #adjudicated? ⇒ Boolean
- #authorize(action = nil, object: nil, petitioner: nil, permissions: nil, parameters: nil, law_class: nil) ⇒ Object
- #authorize!(action = nil, **options) ⇒ Object
- #authorized? ⇒ Boolean
- #law(object = nil, petitioner = nil, permissions: nil, parameters: nil, law_class: nil) ⇒ Object
- #violations ⇒ Object
Instance Method Details
#adjudicated? ⇒ 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
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/law/legalize.rb', line 40 def (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? = { permissions: , parameters: parameters, law_class: law_class } @judgement = law(object, petitioner, **).(action) end |
#authorize!(action = nil, **options) ⇒ Object
36 37 38 |
# File 'lib/law/legalize.rb', line 36 def (action = nil, **) (action, **) or raise Law::NotAuthorizedError end |
#authorized? ⇒ Boolean
13 14 15 |
# File 'lib/law/legalize.rb', line 13 def judgement.try(:authorized?) || false end |
#law(object = nil, petitioner = nil, permissions: nil, parameters: nil, law_class: nil) ⇒ Object
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) ||= 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: , source: petitioner, target: object, params: parameters) end |
#violations ⇒ Object
21 22 23 |
# File 'lib/law/legalize.rb', line 21 def violations judgement.try(:violations) || [] end |