Module: DatastaxRails::SpawnMethods
- Included in:
- Relation
- Defined in:
- lib/datastax_rails/relation/spawn_methods.rb
Constant Summary collapse
- VALID_FIND_OPTIONS =
%i(conditions limit select offset order group page per_page fulltext consistency with_solr with_cassandra where where_not)
Instance Method Summary collapse
-
#apply_finder_options(options) ⇒ DatastaxRails::Relation
Applies the passed in finder options and returns a new Relation.
-
#except(*skips) ⇒ Object
Removes from the query the condition(s) specified in
skips
. -
#merge(r) ⇒ Object
def scoped #:nodoc: self end.
-
#only(*onlies) ⇒ Object
Removes any condition from the query other than the one(s) specified in
onlies
.
Instance Method Details
#apply_finder_options(options) ⇒ DatastaxRails::Relation
Applies the passed in finder options and returns a new Relation. Takes any of the options below and calls them on the relation as if they were methods (conditions
is passed to where
).
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 114 def () relation = self return relation unless .assert_valid_keys(VALID_FIND_OPTIONS) finders = .dup finders.delete_if { |_key, value| value.nil? } ((VALID_FIND_OPTIONS - [:conditions]) & finders.keys).each do |finder| if finder.to_s =~ /(with_solr|with_cassandra)/ relation = relation.send(finder) else relation = relation.send(finder, finders[finder]) end end relation = relation.where(finders[:conditions]) if finders.key?(:conditions) relation end |
#except(*skips) ⇒ Object
Removes from the query the condition(s) specified in skips
.
Example:
Post.where(:active => true).order('id').except(:order) # discards the order condition
Post.where(:active => true).order('id').except(:where) # discards the where condition but keeps the order
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 48 def except(*skips) result = self.class.new(@klass, table) result.default_scoped = default_scoped ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - skips).each do |method| result.send(:"#{method}_values=", send(:"#{method}_values")) end (Relation::SINGLE_VALUE_METHODS - skips).each do |method| result.send(:"#{method}_value=", send(:"#{method}_value")) end # Apply scope extension modules result.send(:apply_modules, extensions) result end |
#merge(r) ⇒ Object
def scoped #:nodoc: self end
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 |
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 7 def merge(r) #:nodoc: return self unless r return to_a & r if r.is_a?(Array) merged_relation = clone r = r.with_default_scope if r.default_scoped? && r.klass != klass (Relation::MULTI_VALUE_METHODS - [:where, :where_not]).each do |method| value = r.send(:"#{method}_values") merged_relation.send(:"#{method}_values=", merged_relation.send(:"#{method}_values") + value) if value.present? end merged_wheres = {} # This will merge all the where clauses into a single hash. If the same attribute is # specified multiple times, the last one will win. (@where_values + r.where_values).each { |w| merged_wheres.merge!(w) } merged_relation.where_values = [merged_wheres] unless merged_wheres.empty? merged_where_nots = {} # This will merge all the where not clauses into a single hash. If the same attribute is # specified multiple times, the last one will win. (@where_not_values + r.where_not_values).each { |w| merged_where_nots.merge!(w) } merged_relation.where_not_values = [merged_where_nots] unless merged_where_nots.empty? (Relation::SINGLE_VALUE_METHODS).each do |method| value = r.send(:"#{method}_value") merged_relation.send(:"#{method}_value=", value) unless value.nil? || value == :default end merged_relation end |
#only(*onlies) ⇒ Object
Removes any condition from the query other than the one(s) specified in onlies
.
Example:
Post.order('id').only(:where) # discards the order condition
Post.order('id').only(:where, :order) # uses the specified order
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 73 def only(*onlies) result = self.class.new(@klass, table) result.default_scoped = default_scoped ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) & onlies).each do |method| result.send(:"#{method}_values=", send(:"#{method}_values")) end (Relation::SINGLE_VALUE_METHODS & onlies).each do |method| result.send(:"#{method}_value=", send(:"#{method}_value")) end # Apply scope extension modules result.send(:apply_modules, extensions) result end |