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.
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
- #count ⇒ Object
- #find_each(&block) ⇒ 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.
- #to_sql ⇒ Object
Constructor Details
#initialize(relation, logger = nil) ⇒ BaseQuery
Returns a new instance of BaseQuery.
8 9 10 11 |
# File 'app/services/query/base_query.rb', line 8 def initialize(relation, logger = nil) @relation = relation @logger = logger || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'app/services/query/base_query.rb', line 6 def logger @logger end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
6 7 8 |
# File 'app/services/query/base_query.rb', line 6 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.
51 52 53 54 |
# File 'app/services/query/base_query.rb', line 51 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
28 29 30 |
# File 'app/services/query/base_query.rb', line 28 def count @relation.count end |
#find_each(&block) ⇒ Object
23 24 25 26 |
# File 'app/services/query/base_query.rb', line 23 def find_each(&block) @relation. find_each(&block) end |
#median_record ⇒ Object
34 35 36 |
# File 'app/services/query/base_query.rb', line 34 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
15 16 17 |
# File 'app/services/query/base_query.rb', line 15 def to_s "#{self.class.name} #{object_id}" end |
#to_sql ⇒ Object
19 20 21 |
# File 'app/services/query/base_query.rb', line 19 def to_sql @relation.to_sql end |