Class: HasDynamicColumns::ActiveRecord::QueryMethods::OrderChain

Inherits:
Object
  • Object
show all
Defined in:
lib/has_dynamic_columns/active_record/query_methods.rb

Overview

OrderChain objects act as placeholder for queries in which #order does not have any parameter. In this case, #order must be chained with #by_dynamic_columns to return a new relation.

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ OrderChain

Returns a new instance of OrderChain.



199
200
201
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 199

def initialize(scope)
	@scope = scope
end

Instance Method Details

#by_dynamic_columns(*args) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 203

def by_dynamic_columns(*args)
	@scope.send(:preprocess_order_args, args)

	# Add now - want to keep the order with the regular column orders
	@scope.order_values += args

	# Map
	dynamic_columns_value = {
		:scope => nil,
		:order => args,
	}
	@scope.order_dynamic_columns_values = dynamic_columns_value

	chain = ::ActiveRecord::QueryMethods::OrderChain.new(@scope)
	chain.instance_eval do
		# Make outer scope variable accessible
		@dynamic_columns_value = dynamic_columns_value

		# Extends where to chain with a has_scope method
		# This scopes the where from above
		def with_scope(opt)
			@dynamic_columns_value[:scope] = opt

			@scope
		end
	end

	chain
end