Class: InstantApi::Model::ActiveRecordQueryBuilder
- Inherits:
-
Object
- Object
- InstantApi::Model::ActiveRecordQueryBuilder
- Defined in:
- lib/instant_api/model/active_record_query_builder.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #find_first(params) ⇒ Object
-
#initialize(model) ⇒ ActiveRecordQueryBuilder
constructor
A new instance of ActiveRecordQueryBuilder.
- #query(params) ⇒ Object
Constructor Details
#initialize(model) ⇒ ActiveRecordQueryBuilder
Returns a new instance of ActiveRecordQueryBuilder.
6 7 8 |
# File 'lib/instant_api/model/active_record_query_builder.rb', line 6 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/instant_api/model/active_record_query_builder.rb', line 5 def model @model end |
Instance Method Details
#find_first(params) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/instant_api/model/active_record_query_builder.rb', line 20 def find_first(params) result = if has_associations?(params) query(params).first else model.find(params[:id]) end raise ActiveRecord::RecordNotFound.new if !result result end |
#query(params) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/instant_api/model/active_record_query_builder.rb', line 10 def query(params) if has_associations?(params) reflector = InstantApi::Model::AssociationReflector.new(params.resources) model.joins(reflector.calculate_join). where(reflector.calculate_conditions(params)) else model.all end end |