Class: Query::BaseQuery
- Inherits:
-
Object
- Object
- Query::BaseQuery
- Includes:
- Memery
- Defined in:
- app/services/query/base_query.rb
Overview
The purpose of this class is to provide common methods re-used in Query Objects.
Query object based on http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Direct Known Subclasses
CatalogItemQuery, ItemSoldFactQuery, NewOpportunityFactQuery, OpportunityQuery, OrderQuery, StoreItemQuery
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Delegated Instance Attributes collapse
-
#count ⇒ Object
Alias for @relation#count.
-
#to_sql ⇒ Object
Alias for @relation#to_sql.
Class Method Summary collapse
-
.median_record(relation) ⇒ Object
Class-level helper kept for callers that want to compute a median against an arbitrary relation without instantiating a query object (e.g. OpportunityQuery#median_opportunity_value uses it on a scoped relation).
Instance Method Summary collapse
- #find_each ⇒ Object
-
#initialize(relation, logger = nil) ⇒ BaseQuery
constructor
A new instance of BaseQuery.
- #median_record ⇒ Object
-
#to_s ⇒ Object
to_s is only useful here when you work in console in these objects, because the basic to_s will cause relations loaded in the initialize to trigger and that's not what we want unless we ask for it.
Constructor Details
#initialize(relation, logger = nil) ⇒ BaseQuery
Returns a new instance of BaseQuery.
10 11 12 13 |
# File 'app/services/query/base_query.rb', line 10 def initialize(relation, logger = nil) @relation = relation @logger = logger || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'app/services/query/base_query.rb', line 8 def logger @logger end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
8 9 10 |
# File 'app/services/query/base_query.rb', line 8 def relation @relation end |
Class Method Details
.median_record(relation) ⇒ Object
Class-level helper kept for callers that want to compute a median
against an arbitrary relation without instantiating a query object
(e.g. OpportunityQuery#median_opportunity_value uses it on a scoped
relation).
NOTE: previously memoized at the class level via Memery. That was
process-wide, never reset between requests, and keyed on the relation
object identity -- so distinct relations with identical SQL never hit
the cache, and identical relation instances reused across requests
returned stale records. Per-instance memoization above is enough
since query objects are short-lived per controller action.
49 50 51 52 |
# File 'app/services/query/base_query.rb', line 49 def median_record(relation) record_count = relation.count record_count.positive? ? relation.offset((record_count / 2).floor).first : nil end |
Instance Method Details
#count ⇒ Object
Alias for @relation#count
28 |
# File 'app/services/query/base_query.rb', line 28 delegate :count, to: :@relation |
#find_each ⇒ Object
23 24 25 26 |
# File 'app/services/query/base_query.rb', line 23 def find_each(&) @relation .find_each(&) end |
#median_record ⇒ Object
32 33 34 |
# File 'app/services/query/base_query.rb', line 32 def median_record self.class.median_record(@relation) end |
#to_s ⇒ Object
to_s is only useful here when you work in console in these objects, because the basic to_s will
cause relations loaded in the initialize to trigger and that's not what we want unless we ask for it
17 18 19 |
# File 'app/services/query/base_query.rb', line 17 def to_s "#{self.class.name} #{object_id}" end |
#to_sql ⇒ Object
Alias for @relation#to_sql
21 |
# File 'app/services/query/base_query.rb', line 21 delegate :to_sql, to: :@relation |