Module: ActiveRecord::SpawnMethods
- Included in:
- Relation
- Defined in:
- lib/active_record/relation/spawn_methods.rb
Constant Summary collapse
- VALID_FIND_OPTIONS =
[ :conditions, :include, :joins, :limit, :offset, :extend, :order, :select, :readonly, :group, :having, :from, :lock ]
Instance Method Summary collapse
- #apply_finder_options(options) ⇒ Object
- #except(*skips) ⇒ Object
- #merge(r) ⇒ Object (also: #&)
- #only(*onlies) ⇒ Object
Instance Method Details
#apply_finder_options(options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/active_record/relation/spawn_methods.rb', line 85 def () relation = clone return relation unless .assert_valid_keys(VALID_FIND_OPTIONS) [:joins, :select, :group, :having, :limit, :offset, :from, :lock].each do |finder| if value = [finder] relation = relation.send(finder, value) end end relation = relation.readonly([:readonly]) if .key? :readonly # Give precedence to newly-applied orders and groups to play nicely with with_scope [:group, :order].each do |finder| relation.send("#{finder}_values=", Array.wrap([finder]) + relation.send("#{finder}_values")) if .has_key?(finder) end relation = relation.where([:conditions]) if .has_key?(:conditions) relation = relation.includes([:include]) if .has_key?(:include) relation = relation.extending([:extend]) if .has_key?(:extend) relation end |
#except(*skips) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_record/relation/spawn_methods.rb', line 52 def except(*skips) result = self.class.new(@klass, table) (Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS).each do |method| result.send(:"#{method}_values=", send(:"#{method}_values")) unless skips.include?(method) end Relation::SINGLE_VALUE_METHODS.each do |method| result.send(:"#{method}_value=", send(:"#{method}_value")) unless skips.include?(method) end result end |
#merge(r) ⇒ Object Also known as: &
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_record/relation/spawn_methods.rb', line 5 def merge(r) merged_relation = clone return merged_relation unless r ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - [:joins, :where]).each do |method| value = r.send(:"#{method}_values") unless value.empty? if method == :includes merged_relation = merged_relation.includes(value) else merged_relation.send(:"#{method}_values=", value) end end end merged_relation = merged_relation.joins(r.joins_values) merged_wheres = @where_values r.where_values.each do |w| if w.respond_to?(:operator) && w.operator == :== merged_wheres = merged_wheres.reject { |p| p.respond_to?(:operator) && p.operator == :== && p.operand1.name == w.operand1.name } end merged_wheres += [w] end merged_relation.where_values = merged_wheres Relation::SINGLE_VALUE_METHODS.reject {|m| m == :lock}.each do |method| unless (value = r.send(:"#{method}_value")).nil? merged_relation.send(:"#{method}_value=", value) end end merged_relation.lock_value = r.lock_value unless merged_relation.lock_value # Apply scope extension modules merged_relation.send :apply_modules, r.extensions merged_relation end |
#only(*onlies) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_record/relation/spawn_methods.rb', line 66 def only(*onlies) result = self.class.new(@klass, table) onlies.each do |only| if (Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS).include?(only) result.send(:"#{only}_values=", send(:"#{only}_values")) elsif Relation::SINGLE_VALUE_METHODS.include?(only) result.send(:"#{only}_value=", send(:"#{only}_value")) else raise "Invalid argument : #{only}" end end result end |