Sha256: 07992c308b891366855ca74bad24e5a86a784407df216164c20d714bc362eef6

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

# ActiveRecord v4 specific changes

module HasDynamicColumns
	module ActiveRecord
		module QueryMethods
			module ClassMethods
			end

			# lifted from
			# http://erniemiller.org/2013/10/07/activerecord-where-not-sane-true/
			module WhereChainCompatibility
				#include ::ActiveRecord::QueryMethods
				#define_method :build_where,
				#	::ActiveRecord::QueryMethods.instance_method(:build_where)

				# Extends where to chain a has_dynamic_columns method
				# This builds all the joins needed to search the has_dynamic_columns_data tables
				def has_dynamic_columns(opts = :chain, *rest)
					# Map
					dynamic_columns_value = {
						:scope => nil,
						:where => @scope.send(:build_where, opts, rest)
					}
					@scope.where_dynamic_columns_values = dynamic_columns_value

					chain = ::ActiveRecord::QueryMethods::WhereChain.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
						def without_scope
							@scope
						end
					end

					chain
				end
			end

			def where_with_dynamic_columns(opts = :chain, *rest)
				if opts == :chain
					scope = spawn
					chain = ::ActiveRecord::QueryMethods::WhereChain.new(scope)
					chain.send(:extend, WhereChainCompatibility)
				else
					where_without_dynamic_columns(opts, *rest)
				end
			end

			def order_with_dynamic_columns(*args)
				# Chain - by_dynamic_columns
				if args.empty?
					::ActiveRecord::QueryMethods::OrderChain.new(spawn)
				else
					order_without_dynamic_columns(*args)
				end
			end
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
has_dynamic_columns-0.3.10 lib/has_dynamic_columns/active_record/v4/query_methods.rb
has_dynamic_columns-0.3.9 lib/has_dynamic_columns/active_record/v4/query_methods.rb
has_dynamic_columns-0.3.8 lib/has_dynamic_columns/active_record/v4/query_methods.rb