Module: HaveAPI::ModelAdapters::ActiveRecord::Action::InstanceMethods
- Defined in:
- lib/haveapi/model_adapters/active_record.rb
Instance Method Summary collapse
-
#ar_default_includes ⇒ Object
Default includes contain all associated resources specified inaction output parameters.
-
#ar_inner_includes(includes) ⇒ Object
Called by ar_parse_includes for recursion purposes.
-
#ar_parse_includes(raw) ⇒ Object
Parse includes sent by the user and return them in an array of symbols and hashes.
-
#with_includes(q = nil) ⇒ Object
Helper method that sets correct ActiveRecord includes according to the meta includes sent by the user.
Instance Method Details
#ar_default_includes ⇒ Object
Default includes contain all associated resources specified inaction output parameters. They are fetched from the database anyway, to return the label for even unresolved association.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/haveapi/model_adapters/active_record.rb', line 87 def ar_default_includes ret = [] self.class.output.params.each do |p| if p.is_a?(HaveAPI::Parameters::Resource) && self.class.model.reflections[p.name.to_sym] ret << p.name.to_sym end end ret end |
#ar_inner_includes(includes) ⇒ Object
Called by ar_parse_includes for recursion purposes.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/haveapi/model_adapters/active_record.rb', line 66 def ar_inner_includes(includes) args = [] includes.each do |assoc| if assoc.index('__') tmp = {} parts = assoc.split('__') tmp[parts.first.to_sym] = ar_inner_includes([parts[1..-1].join('__')]) args << tmp else args << assoc.to_sym end end args end |
#ar_parse_includes(raw) ⇒ Object
Parse includes sent by the user and return them in an array of symbols and hashes.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/haveapi/model_adapters/active_record.rb', line 46 def ar_parse_includes(raw) return @ar_parsed_includes if @ar_parsed_includes @ar_parsed_includes = ar_inner_includes(raw).select do |inc| # Drop associations that are not registered in the AR: # The API resource may have associations that are not based on # associations in AR. if inc.is_a?(::Hash) inc.each_key do |k| next(false) unless self.class.model.reflections.has_key?(k.to_s) end else next(false) unless self.class.model.reflections.has_key?(inc.to_s) end true end end |
#with_includes(q = nil) ⇒ Object
Helper method that sets correct ActiveRecord includes according to the meta includes sent by the user. q
is the model or partial AR query. If not set, action’s model class is used instead.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/haveapi/model_adapters/active_record.rb', line 26 def with_includes(q = nil) q ||= self.class.model includes = && [:includes] args = includes.nil? ? [] : ar_parse_includes(includes) # Resulting includes may still contain duplicities in form of nested # includes. ar_default_includes returns a flat array where as # ar_parse_includes may contain hashes. But since ActiveRecord is taking # it well, it is not necessary to fix. args.concat(ar_default_includes).uniq if !args.empty? q.includes(*args) else q end end |