Class: ForestLiana::ResourcesGetter
- Inherits:
-
Object
- Object
- ForestLiana::ResourcesGetter
- Defined in:
- app/services/forest_liana/resources_getter.rb
Instance Method Summary collapse
- #count ⇒ Object
- #includes ⇒ Object
-
#initialize(resource, params) ⇒ ResourcesGetter
constructor
A new instance of ResourcesGetter.
- #perform ⇒ Object
- #records ⇒ Object
Constructor Details
#initialize(resource, params) ⇒ ResourcesGetter
Returns a new instance of ResourcesGetter.
3 4 5 6 7 8 9 10 |
# File 'app/services/forest_liana/resources_getter.rb', line 3 def initialize(resource, params) @resource = resource @params = params @count_needs_includes = false @field_names_requested = field_names_requested get_segment() end |
Instance Method Details
#count ⇒ Object
38 39 40 |
# File 'app/services/forest_liana/resources_getter.rb', line 38 def count @records_to_count.count end |
#includes ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/services/forest_liana/resources_getter.rb', line 42 def includes includes = SchemaUtils.one_associations(@resource) .select { |association| SchemaUtils.model_included?(association.klass) } .map(&:name) if @field_names_requested includes & @field_names_requested else includes end end |
#perform ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/services/forest_liana/resources_getter.rb', line 12 def perform @records = @resource.unscoped if @segment && @segment.scope @records = @records.send(@segment.scope) elsif @segment && @segment.where @records = @records.where(@segment.where.call()) end @records = search_query @records_to_count = @records # NOTICE: For performance reasons, do not eager load the data if there is # no search or filters on associations. if @count_needs_includes @records_to_count = @records_to_count.eager_load(includes) end @records = @records.eager_load(includes) @records_sorted = sort_query end |
#records ⇒ Object
34 35 36 |
# File 'app/services/forest_liana/resources_getter.rb', line 34 def records @records_sorted.offset(offset).limit(limit).to_a end |