Class: ForestLiana::HasManyGetter
- Inherits:
-
Object
- Object
- ForestLiana::HasManyGetter
- Defined in:
- app/services/forest_liana/has_many_getter.rb
Instance Method Summary collapse
- #count ⇒ Object
- #field_names_requested ⇒ Object
- #includes ⇒ Object
-
#initialize(resource, association, params) ⇒ HasManyGetter
constructor
A new instance of HasManyGetter.
- #perform ⇒ Object
- #records ⇒ Object
Constructor Details
#initialize(resource, association, params) ⇒ HasManyGetter
Returns a new instance of HasManyGetter.
3 4 5 6 7 8 |
# File 'app/services/forest_liana/has_many_getter.rb', line 3 def initialize(resource, association, params) @resource = resource @association = association @params = params @field_names_requested = field_names_requested end |
Instance Method Details
#count ⇒ Object
46 47 48 |
# File 'app/services/forest_liana/has_many_getter.rb', line 46 def count @records.to_a.length end |
#field_names_requested ⇒ Object
10 11 12 13 14 |
# File 'app/services/forest_liana/has_many_getter.rb', line 10 def field_names_requested return nil unless @params[:fields] && @params[:fields][@association.table_name] @params[:fields][@association.table_name].split(',') .map { |name| name.to_sym } end |
#includes ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/forest_liana/has_many_getter.rb', line 29 def includes @association.klass .reflect_on_all_associations .select do |association| inclusion = !association.[:polymorphic] && SchemaUtils.model_included?(association.klass) && [:belongs_to, :has_and_belongs_to_many].include?(association.macro) if @field_names_requested inclusion && @field_names_requested.include?(association.name) else inclusion end end .map { |association| association.name.to_s } end |
#perform ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'app/services/forest_liana/has_many_getter.rb', line 16 def perform @records = @resource .unscoped .find(@params[:id]) .send(@params[:association_name]) .eager_load(includes) @records = sort_query end |
#records ⇒ Object
25 26 27 |
# File 'app/services/forest_liana/has_many_getter.rb', line 25 def records @records.limit(limit).offset(offset) end |