Class: ApiMaker::Ability
- Inherits:
-
Object
- Object
- ApiMaker::Ability
- Includes:
- CanCan::Ability
- Defined in:
- lib/api_maker/ability.rb
Instance Attribute Summary collapse
-
#loader ⇒ Object
readonly
Returns the value of attribute loader.
Instance Method Summary collapse
- #active_record?(subject) ⇒ Boolean
-
#can?(*args) ⇒ Boolean
Override methods from CanCan::Ability to first load abilities from the given resource.
-
#initialize(api_maker_args: nil, locals: nil) ⇒ Ability
constructor
A new instance of Ability.
- #load_abilities(subject) ⇒ Object
- #model_adapter(*args) ⇒ Object
Constructor Details
#initialize(api_maker_args: nil, locals: nil) ⇒ Ability
Returns a new instance of Ability.
6 7 8 9 10 |
# File 'lib/api_maker/ability.rb', line 6 def initialize(api_maker_args: nil, locals: nil) @api_maker_args = api_maker_args || {} @locals = locals || api_maker_args&.dig(:locals) || {} @loader = ApiMaker::AbilityLoader.new(ability: self, locals: locals, api_maker_args: api_maker_args) end |
Instance Attribute Details
#loader ⇒ Object (readonly)
Returns the value of attribute loader.
4 5 6 |
# File 'lib/api_maker/ability.rb', line 4 def loader @loader end |
Instance Method Details
#active_record?(subject) ⇒ Boolean
49 50 51 52 53 |
# File 'lib/api_maker/ability.rb', line 49 def active_record?(subject) return subject < ActiveRecord::Base if subject.class == Class # rubocop:disable Style/ClassEqualityComparison subject.is_a?(ActiveRecord::Base) end |
#can?(*args) ⇒ Boolean
Override methods from CanCan::Ability to first load abilities from the given resource
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/api_maker/ability.rb', line 13 def can?(*args) subject = args.second load_abilities(subject) super rescue ActiveModel::MissingAttributeError => e if subject.is_a?(ActiveRecord::Base) # Add subject / model class name to the error message new_error = ActiveModel::MissingAttributeError.new("Error on #{subject.class.name}: #{e.}") new_error.set_backtrace(e.backtrace) raise new_error end raise e end |
#load_abilities(subject) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/api_maker/ability.rb', line 35 def load_abilities(subject) return unless active_record?(subject) if subject.class == Class # rubocop:disable Style/ClassEqualityComparison ApiMaker::Configuration.profile(-> { "Loading abilities for #{subject.name}" }) do loader.load_model_class(subject) end elsif subject.class != Class ApiMaker::Configuration.profile(-> { "Loading abilities for #{subject.class.name}" }) do loader.load_model_class(subject.class) end end end |
#model_adapter(*args) ⇒ Object
29 30 31 32 33 |
# File 'lib/api_maker/ability.rb', line 29 def model_adapter(*args) subject = args.first load_abilities(subject) super end |