Module: DatastaxRails::SpawnMethods

Included in:
Relation
Defined in:
lib/datastax_rails/relation/spawn_methods.rb

Overview

Relation methods for building a new relation from an existing one (which may be empty).

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

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).

Parameters:

  • options (Hash)

    the options hash

Options Hash (options):

  • :conditions (Hash)
  • :consistency (Symbol, String)
  • :fulltext (String)
  • :group (Symbol, String)
  • :limit (Integer, String)
  • :offset (Integer, String)
  • :order (String, Hash)
  • :page (Integer, String)
  • :per_page (Integer, String)
  • :select (Array)
  • :where (Hash)
  • :where_not (Hash)
  • :with_cassandra (Boolean)
  • :with_solr (Boolean)

Returns:

Raises:

  • (ArgumentError)

    if an invalid option is passed in



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 111

def apply_finder_options(options)
  relation = self
  return relation unless options

  options.assert_valid_keys(VALID_FIND_OPTIONS)
  finders = options.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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 45

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

:nodoc:



4
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
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 4

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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/datastax_rails/relation/spawn_methods.rb', line 70

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