Class: LegacyMigrations::Squirrel::Query
- Inherits:
-
Object
- Object
- LegacyMigrations::Squirrel::Query
- Defined in:
- lib/legacy_migrations/squirrel/squirrel.rb
Overview
The Query is what contains the query and is what handles execution and pagination of the result set.
Defined Under Namespace
Classes: Condition, ConditionGroup
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#execute(*args) ⇒ Object
Runs the block which builds the conditions.
- #from ⇒ Object
-
#initialize(model, from_record = nil, &blk) ⇒ Query
constructor
Creates a Query specific to the given model (which is a class that descends from AR::Base) and a block that will be run to find the conditions for the #find call.
-
#join_dependency ⇒ Object
Builds the dependencies needed to find what AR plans to call the tables in the query by finding and sending what would be passed in as the
include
parameter to #find. -
#paginate_result_set(set, conditions) ⇒ Object
Used by #execute to paginates the result set if pagination was requested.
-
#to_find_conditions ⇒ Object
Delegates the to_find_conditions call to the root ConditionGroup.
-
#to_find_include ⇒ Object
Delegates the to_find_include call to the root ConditionGroup.
-
#to_find_limit ⇒ Object
Delegates the to_find_limit call to the root ConditionGroup.
-
#to_find_offset ⇒ Object
Delegates the to_find_offset call to the root ConditionGroup.
-
#to_find_order ⇒ Object
Delegates the to_find_order call to the root ConditionGroup.
- #to_find_parameters ⇒ Object
Constructor Details
#initialize(model, from_record = nil, &blk) ⇒ Query
Creates a Query specific to the given model (which is a class that descends from AR::Base) and a block that will be run to find the conditions for the #find call.
62 63 64 65 66 67 68 69 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 62 def initialize model, from_record = nil, &blk @model = model @joins = nil @from = from_record @binding = blk && blk.binding @conditions = ConditionGroup.new(@model, "AND", @binding, @from, &blk) @conditions.assign_joins( join_dependency ) end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
58 59 60 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 58 def conditions @conditions end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
58 59 60 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 58 def model @model end |
Instance Method Details
#execute(*args) ⇒ Object
Runs the block which builds the conditions. If requested, paginates the result_set. If the first parameter to #find is :query (instead of :all or :first), then the query object itself is returned. Useful for debugging, but not much else.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 85 def execute *args if args.first == :query self else opts = args.last.is_a?(Hash) ? args.last : {} results = [] pagination = opts.delete(:paginate) || {} model.send(:with_scope, :find => opts) do @conditions.paginate(pagination) unless pagination.empty? results = model.find args[0], to_find_parameters if @conditions.paginate? paginate_result_set results, to_find_parameters end end results end end |
#from ⇒ Object
104 105 106 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 104 def from @from end |
#join_dependency ⇒ Object
Builds the dependencies needed to find what AR plans to call the tables in the query by finding and sending what would be passed in as the include
parameter to #find. This is a necessary step because what AR calls tables deeply nested might not be completely obvious.)
75 76 77 78 79 80 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 75 def join_dependency jd = ::ActiveRecord::Associations::ClassMethods::JoinDependency @join_dependency ||= jd.new model, @conditions.to_find_include, nil end |
#paginate_result_set(set, conditions) ⇒ Object
Used by #execute to paginates the result set if pagination was requested. In this case, it adds pages
and total_results
accessors to the result set. See Paginator for more details.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 146 def paginate_result_set set, conditions limit = conditions.delete(:limit) offset = conditions.delete(:offset) class << set attr_reader :pages attr_reader :total_results end total_results = model.count(conditions) set.instance_variable_set("@pages", Paginator.new( :count => total_results, :limit => limit, :offset => offset) ) set.instance_variable_set("@total_results", total_results) set.extend( Squirrel::WillPagination ) end |
#to_find_conditions ⇒ Object
Delegates the to_find_conditions call to the root ConditionGroup
119 120 121 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 119 def to_find_conditions @conditions.to_find_conditions end |
#to_find_include ⇒ Object
Delegates the to_find_include call to the root ConditionGroup
124 125 126 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 124 def to_find_include @conditions.to_find_include end |
#to_find_limit ⇒ Object
Delegates the to_find_limit call to the root ConditionGroup
134 135 136 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 134 def to_find_limit @conditions.to_find_limit end |
#to_find_offset ⇒ Object
Delegates the to_find_offset call to the root ConditionGroup
139 140 141 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 139 def to_find_offset @conditions.to_find_offset end |
#to_find_order ⇒ Object
Delegates the to_find_order call to the root ConditionGroup
129 130 131 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 129 def to_find_order @conditions.to_find_order end |
#to_find_parameters ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 108 def to_find_parameters find_parameters = {} find_parameters[:conditions] = to_find_conditions unless to_find_conditions.blank? find_parameters[:include ] = to_find_include unless to_find_include.blank? find_parameters[:order ] = to_find_order unless to_find_order.blank? find_parameters[:limit ] = to_find_limit unless to_find_limit.blank? find_parameters[:offset ] = to_find_offset unless to_find_offset.blank? find_parameters end |