datastax_rails 2.0.12 → 2.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +5 -5
  3. data/lib/blankslate.rb +8 -11
  4. data/lib/cql-rb_extensions.rb +5 -3
  5. data/lib/datastax_rails/associations/association.rb +93 -101
  6. data/lib/datastax_rails/associations/association_scope.rb +7 -7
  7. data/lib/datastax_rails/associations/belongs_to_association.rb +46 -48
  8. data/lib/datastax_rails/associations/builder/association.rb +32 -31
  9. data/lib/datastax_rails/associations/builder/belongs_to.rb +19 -20
  10. data/lib/datastax_rails/associations/builder/collection_association.rb +32 -32
  11. data/lib/datastax_rails/associations/builder/has_and_belongs_to_many.rb +21 -21
  12. data/lib/datastax_rails/associations/builder/has_many.rb +39 -40
  13. data/lib/datastax_rails/associations/builder/has_one.rb +30 -31
  14. data/lib/datastax_rails/associations/builder/singular_association.rb +31 -33
  15. data/lib/datastax_rails/associations/collection_association.rb +129 -135
  16. data/lib/datastax_rails/associations/collection_proxy.rb +21 -21
  17. data/lib/datastax_rails/associations/has_and_belongs_to_many_association.rb +26 -26
  18. data/lib/datastax_rails/associations/has_many_association.rb +38 -38
  19. data/lib/datastax_rails/associations/has_one_association.rb +31 -32
  20. data/lib/datastax_rails/associations/singular_association.rb +31 -30
  21. data/lib/datastax_rails/associations.rb +27 -24
  22. data/lib/datastax_rails/attribute_assignment.rb +17 -17
  23. data/lib/datastax_rails/attribute_methods/definition.rb +4 -4
  24. data/lib/datastax_rails/attribute_methods/dirty.rb +34 -33
  25. data/lib/datastax_rails/attribute_methods/primary_key.rb +3 -8
  26. data/lib/datastax_rails/attribute_methods/read.rb +10 -12
  27. data/lib/datastax_rails/attribute_methods/typecasting.rb +36 -35
  28. data/lib/datastax_rails/attribute_methods/write.rb +5 -6
  29. data/lib/datastax_rails/attribute_methods.rb +52 -56
  30. data/lib/datastax_rails/base.rb +122 -125
  31. data/lib/datastax_rails/callbacks.rb +15 -9
  32. data/lib/datastax_rails/cassandra_only_model.rb +6 -6
  33. data/lib/datastax_rails/collection.rb +5 -7
  34. data/lib/datastax_rails/column.rb +130 -118
  35. data/lib/datastax_rails/connection/statement_cache.rb +3 -3
  36. data/lib/datastax_rails/connection.rb +42 -33
  37. data/lib/datastax_rails/cql/alter_column_family.rb +19 -21
  38. data/lib/datastax_rails/cql/base.rb +8 -11
  39. data/lib/datastax_rails/cql/column_family.rb +11 -10
  40. data/lib/datastax_rails/cql/consistency.rb +2 -2
  41. data/lib/datastax_rails/cql/create_column_family.rb +15 -15
  42. data/lib/datastax_rails/cql/create_index.rb +5 -5
  43. data/lib/datastax_rails/cql/create_keyspace.rb +7 -7
  44. data/lib/datastax_rails/cql/delete.rb +16 -29
  45. data/lib/datastax_rails/cql/drop_column_family.rb +2 -2
  46. data/lib/datastax_rails/cql/drop_index.rb +2 -2
  47. data/lib/datastax_rails/cql/drop_keyspace.rb +2 -2
  48. data/lib/datastax_rails/cql/insert.rb +10 -16
  49. data/lib/datastax_rails/cql/select.rb +21 -33
  50. data/lib/datastax_rails/cql/truncate.rb +2 -2
  51. data/lib/datastax_rails/cql/update.rb +16 -24
  52. data/lib/datastax_rails/cql/use_keyspace.rb +2 -2
  53. data/lib/datastax_rails/cql.rb +2 -2
  54. data/lib/datastax_rails/dynamic_model.rb +32 -29
  55. data/lib/datastax_rails/errors.rb +6 -6
  56. data/lib/datastax_rails/grouped_collection.rb +3 -3
  57. data/lib/datastax_rails/inheritance.rb +9 -9
  58. data/lib/datastax_rails/payload_model.rb +24 -20
  59. data/lib/datastax_rails/persistence.rb +116 -110
  60. data/lib/datastax_rails/railtie.rb +7 -7
  61. data/lib/datastax_rails/reflection.rb +61 -59
  62. data/lib/datastax_rails/relation/batches.rb +12 -13
  63. data/lib/datastax_rails/relation/facet_methods.rb +44 -33
  64. data/lib/datastax_rails/relation/finder_methods.rb +95 -91
  65. data/lib/datastax_rails/relation/modification_methods.rb +5 -5
  66. data/lib/datastax_rails/relation/search_methods.rb +102 -102
  67. data/lib/datastax_rails/relation/spawn_methods.rb +25 -24
  68. data/lib/datastax_rails/relation/stats_methods.rb +9 -8
  69. data/lib/datastax_rails/relation.rb +165 -170
  70. data/lib/datastax_rails/rsolr_client_wrapper.rb +3 -3
  71. data/lib/datastax_rails/schema/cassandra.rb +44 -43
  72. data/lib/datastax_rails/schema/migrator.rb +52 -52
  73. data/lib/datastax_rails/schema/solr.rb +55 -47
  74. data/lib/datastax_rails/schema_cache.rb +1 -3
  75. data/lib/datastax_rails/scoping/default.rb +2 -3
  76. data/lib/datastax_rails/scoping/named.rb +3 -5
  77. data/lib/datastax_rails/scoping.rb +11 -12
  78. data/lib/datastax_rails/serialization.rb +34 -31
  79. data/lib/datastax_rails/serializers/xml_serializer.rb +178 -175
  80. data/lib/datastax_rails/timestamps.rb +4 -4
  81. data/lib/datastax_rails/types/dirty_collection.rb +57 -57
  82. data/lib/datastax_rails/types/dynamic_list.rb +1 -1
  83. data/lib/datastax_rails/types/dynamic_map.rb +5 -7
  84. data/lib/datastax_rails/types/dynamic_set.rb +2 -2
  85. data/lib/datastax_rails/util/solr_repair.rb +3 -3
  86. data/lib/datastax_rails/validations/associated.rb +8 -6
  87. data/lib/datastax_rails/validations/uniqueness.rb +8 -8
  88. data/lib/datastax_rails/validations.rb +9 -10
  89. data/lib/datastax_rails/version.rb +2 -1
  90. data/lib/datastax_rails/wide_storage_model.rb +6 -6
  91. data/lib/datastax_rails.rb +13 -9
  92. data/lib/schema_migration.rb +3 -3
  93. data/spec/datastax_rails/associations/belongs_to_association_spec.rb +2 -2
  94. data/spec/datastax_rails/associations/collection_association_spec.rb +14 -14
  95. data/spec/datastax_rails/associations/has_many_association_spec.rb +20 -20
  96. data/spec/datastax_rails/associations_spec.rb +11 -11
  97. data/spec/datastax_rails/attribute_methods_spec.rb +25 -25
  98. data/spec/datastax_rails/base_spec.rb +24 -24
  99. data/spec/datastax_rails/callbacks_spec.rb +21 -21
  100. data/spec/datastax_rails/column_spec.rb +133 -132
  101. data/spec/datastax_rails/connection/statement_cache_spec.rb +2 -2
  102. data/spec/datastax_rails/cql/base_spec.rb +4 -4
  103. data/spec/datastax_rails/cql/delete_spec.rb +19 -0
  104. data/spec/datastax_rails/cql/select_spec.rb +8 -8
  105. data/spec/datastax_rails/cql/update_spec.rb +8 -10
  106. data/spec/datastax_rails/dynamic_model_spec.rb +36 -22
  107. data/spec/datastax_rails/inheritance_spec.rb +11 -14
  108. data/spec/datastax_rails/persistence_spec.rb +73 -74
  109. data/spec/datastax_rails/relation/batches_spec.rb +13 -13
  110. data/spec/datastax_rails/relation/facet_methods_spec.rb +43 -35
  111. data/spec/datastax_rails/relation/finder_methods_spec.rb +77 -78
  112. data/spec/datastax_rails/relation/modification_methods_spec.rb +19 -19
  113. data/spec/datastax_rails/relation/search_methods_spec.rb +160 -160
  114. data/spec/datastax_rails/relation/spawn_methods_spec.rb +18 -18
  115. data/spec/datastax_rails/relation_spec.rb +119 -116
  116. data/spec/datastax_rails/schema/migrator_spec.rb +30 -30
  117. data/spec/datastax_rails/schema/solr_spec.rb +15 -15
  118. data/spec/datastax_rails/scoping/default_spec.rb +9 -9
  119. data/spec/datastax_rails/types/dynamic_list_spec.rb +12 -12
  120. data/spec/datastax_rails/types/dynamic_map_spec.rb +10 -10
  121. data/spec/datastax_rails/types/dynamic_set_spec.rb +22 -10
  122. data/spec/datastax_rails/validations/uniqueness_spec.rb +25 -25
  123. data/spec/datastax_rails/wide_storage_model_spec.rb +11 -0
  124. data/spec/datastax_rails_spec.rb +2 -2
  125. data/spec/dummy/config/application.rb +2 -3
  126. data/spec/dummy/config/boot.rb +1 -1
  127. data/spec/dummy/config/environments/development.rb +3 -3
  128. data/spec/dummy/config/environments/test.rb +1 -1
  129. data/spec/dummy/config/initializers/session_store.rb +1 -1
  130. data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
  131. data/spec/factories/audit_logs.rb +6 -0
  132. data/spec/factories/hobbies.rb +6 -0
  133. data/spec/factories/people.rb +5 -0
  134. data/spec/feature/dynamic_fields_spec.rb +4 -4
  135. data/spec/feature/overloaded_tables_spec.rb +11 -12
  136. data/spec/spec_helper.rb +17 -14
  137. data/spec/support/datastax_test_hook.rb +2 -2
  138. data/spec/support/default_consistency_shared_examples.rb +11 -11
  139. data/spec/support/models.rb +31 -32
  140. metadata +40 -6
  141. data/lib/datastax_rails/attribute_methods/before_type_cast.rb +0 -71
  142. data/lib/datastax_rails/log_subscriber.rb +0 -0
  143. data/spec/dummy/ks/migrate/20111117224534_models.rb +0 -20
@@ -1,44 +1,44 @@
1
1
  module DatastaxRails
2
2
  module SpawnMethods
3
3
  # def scoped #:nodoc:
4
- # self
4
+ # self
5
5
  # end
6
-
6
+
7
7
  def merge(r) #:nodoc:
8
8
  return self unless r
9
9
  return to_a & r if r.is_a?(Array)
10
-
10
+
11
11
  merged_relation = clone
12
-
12
+
13
13
  r = r.with_default_scope if r.default_scoped? && r.klass != klass
14
-
14
+
15
15
  (Relation::MULTI_VALUE_METHODS - [:where, :where_not]).each do |method|
16
16
  value = r.send(:"#{method}_values")
17
17
  merged_relation.send(:"#{method}_values=", merged_relation.send(:"#{method}_values") + value) if value.present?
18
18
  end
19
-
19
+
20
20
  merged_wheres = {}
21
21
  # This will merge all the where clauses into a single hash. If the same attribute is
22
22
  # specified multiple times, the last one will win.
23
- (@where_values + r.where_values).each { |w| merged_wheres.merge!(w)}
24
-
23
+ (@where_values + r.where_values).each { |w| merged_wheres.merge!(w) }
24
+
25
25
  merged_relation.where_values = [merged_wheres] unless merged_wheres.empty?
26
-
26
+
27
27
  merged_where_nots = {}
28
28
  # This will merge all the where not clauses into a single hash. If the same attribute is
29
29
  # specified multiple times, the last one will win.
30
- (@where_not_values + r.where_not_values).each { |w| merged_where_nots.merge!(w)}
31
-
30
+ (@where_not_values + r.where_not_values).each { |w| merged_where_nots.merge!(w) }
31
+
32
32
  merged_relation.where_not_values = [merged_where_nots] unless merged_where_nots.empty?
33
-
33
+
34
34
  (Relation::SINGLE_VALUE_METHODS).each do |method|
35
35
  value = r.send(:"#{method}_value")
36
36
  merged_relation.send(:"#{method}_value=", value) unless value.nil? || value == :default
37
37
  end
38
-
38
+
39
39
  merged_relation
40
40
  end
41
-
41
+
42
42
  # Removes from the query the condition(s) specified in +skips+.
43
43
  #
44
44
  # Example:
@@ -62,7 +62,7 @@ module DatastaxRails
62
62
 
63
63
  result
64
64
  end
65
-
65
+
66
66
  # Removes any condition from the query other than the one(s) specified in +onlies+.
67
67
  #
68
68
  # Example:
@@ -87,8 +87,9 @@ module DatastaxRails
87
87
 
88
88
  result
89
89
  end
90
-
91
- VALID_FIND_OPTIONS = [:conditions, :limit, :select, :offset, :order, :group, :page, :per_page, :fulltext, :consistency, :with_solr, :with_cassandra, :where, :where_not]
90
+
91
+ VALID_FIND_OPTIONS = %i(conditions limit select offset order group page per_page fulltext consistency with_solr
92
+ with_cassandra where where_not)
92
93
  # Applies the passed in finder options and returns a new Relation.
93
94
  # Takes any of the options below and calls them on the relation as if they
94
95
  # were methods (+conditions+ is passed to +where+).
@@ -113,21 +114,21 @@ module DatastaxRails
113
114
  def apply_finder_options(options)
114
115
  relation = self
115
116
  return relation unless options
116
-
117
+
117
118
  options.assert_valid_keys(VALID_FIND_OPTIONS)
118
119
  finders = options.dup
119
- finders.delete_if { |key, value| value.nil? }
120
-
120
+ finders.delete_if { |_key, value| value.nil? }
121
+
121
122
  ((VALID_FIND_OPTIONS - [:conditions]) & finders.keys).each do |finder|
122
- if(finder.to_s =~ /(with_solr|with_cassandra)/)
123
+ if finder.to_s =~ /(with_solr|with_cassandra)/
123
124
  relation = relation.send(finder)
124
125
  else
125
126
  relation = relation.send(finder, finders[finder])
126
127
  end
127
128
  end
128
-
129
- relation = relation.where(finders[:conditions]) if finders.has_key?(:conditions)
129
+
130
+ relation = relation.where(finders[:conditions]) if finders.key?(:conditions)
130
131
  relation
131
132
  end
132
133
  end
133
- end
134
+ end
@@ -1,7 +1,7 @@
1
1
  module DatastaxRails
2
2
  module StatsMethods
3
- STATS_FIELDS={'sum' => 'sum', 'maximum' => 'max', 'minimum' => 'min', 'average' => 'mean', 'stddev' => 'stddev'}
4
-
3
+ STATS_FIELDS = { 'sum' => 'sum', 'maximum' => 'max', 'minimum' => 'min', 'average' => 'mean', 'stddev' => 'stddev' }
4
+
5
5
  # @!method sum(field)
6
6
  # Calculates the sum of the field listed. Field must be indexed as a number.
7
7
  # @param [Symbol] field the field to calculate
@@ -42,28 +42,29 @@ module DatastaxRails
42
42
  # Calculates the standard deviation of the field listed for a grouped query.
43
43
  # @param [Symbol] field the field to calculate
44
44
  # @return [Hash] the standard deviation of the columns that match the query by group. Group name is the key.
45
- %w[sum maximum minimum average stddev].each do |op|
45
+ %w(sum maximum minimum average stddev).each do |op|
46
46
  define_method(op) do |field|
47
47
  calculate_stats(field)
48
48
  @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
49
49
  end
50
-
50
+
51
51
  define_method("grouped_#{op}") do |field|
52
52
  self.op unless @group_value
53
53
  calculate_stats(field)
54
54
  values = {}
55
- @stats[field]["facets"][@group_value].each do |k,v|
55
+ @stats[field]['facets'][@group_value].each do |k, v|
56
56
  values[k] = v[STATS_FIELDS[op]]
57
57
  end
58
58
  values
59
59
  end
60
60
  end
61
-
61
+
62
62
  private
63
+
63
64
  def calculate_stats(field)
64
65
  unless @stats[field]
65
66
  @stats[field] = limit(1).compute_stats(field).stats[field]
66
67
  end
67
68
  end
68
- end
69
- end
69
+ end
70
+ end
@@ -1,13 +1,15 @@
1
1
  require 'rsolr'
2
2
  require 'pp' if ENV['DEBUG_SOLR'] == 'true'
3
-
3
+ # TODO: Move functionality into modules
4
4
  module DatastaxRails
5
- class Relation
6
- MULTI_VALUE_METHODS = [:order, :where, :where_not, :fulltext, :greater_than, :less_than, :select, :stats, :field_facet, :range_facet, :slow_order]
7
- SINGLE_VALUE_METHODS = [:page, :per_page, :reverse_order, :query_parser, :consistency, :ttl, :use_solr, :escape, :group, :allow_filtering]
8
-
5
+ class Relation # rubocop:disable Style/ClassLength
6
+ MULTI_VALUE_METHODS = %i(order where where_not fulltext greater_than less_than select stats field_facet
7
+ range_facet slow_order)
8
+ SINGLE_VALUE_METHODS = %i(page per_page reverse_order query_parser consistency ttl use_solr escape group
9
+ allow_filtering)
10
+
9
11
  SOLR_CHAR_RX = /([\+\!\(\)\[\]\^\"\~\:\'\=\/]+)/
10
-
12
+
11
13
  Relation::MULTI_VALUE_METHODS.each do |m|
12
14
  attr_accessor :"#{m}_values"
13
15
  end
@@ -16,7 +18,7 @@ module DatastaxRails
16
18
  end
17
19
  attr_accessor :create_with_value, :default_scoped
18
20
  attr_accessor :highlight_options
19
-
21
+
20
22
  include SearchMethods
21
23
  include ModificationMethods
22
24
  include FinderMethods
@@ -24,11 +26,11 @@ module DatastaxRails
24
26
  include StatsMethods
25
27
  include Batches
26
28
  include FacetMethods
27
-
29
+
28
30
  attr_reader :klass, :column_family, :loaded, :cql
29
- alias :loaded? :loaded
30
- alias :default_scoped? :default_scoped
31
-
31
+ alias_method :loaded?, :loaded
32
+ alias_method :default_scoped?, :default_scoped
33
+
32
34
  # Initializes the Relation. Defaults page value to 1, per_page to the class
33
35
  # default, and solr use to true. Everything else gets defaulted to nil or
34
36
  # empty.
@@ -41,9 +43,9 @@ module DatastaxRails
41
43
  @results = []
42
44
  @default_scoped = false
43
45
  @cql = DatastaxRails::Cql.for_class(klass)
44
-
45
- SINGLE_VALUE_METHODS.each {|v| instance_variable_set(:"@#{v}_value", nil)}
46
- MULTI_VALUE_METHODS.each {|v| instance_variable_set(:"@#{v}_values", [])}
46
+
47
+ SINGLE_VALUE_METHODS.each { |v| instance_variable_set(:"@#{v}_value", nil) }
48
+ MULTI_VALUE_METHODS.each { |v| instance_variable_set(:"@#{v}_values", []) }
47
49
  @highlight_options = {}
48
50
  @per_page_value = @klass.default_page_size
49
51
  @page_value = 1
@@ -53,24 +55,24 @@ module DatastaxRails
53
55
  @escape_value = :default
54
56
  @stats = {}
55
57
  end
56
-
58
+
57
59
  # Returns true if the two relations have the same query parameters
58
60
  def ==(other)
59
61
  case other
60
62
  when Relation
61
63
  # This is not a valid implementation. It's a placeholder till I figure out the right way.
62
64
  MULTI_VALUE_METHODS.each do |m|
63
- return false unless other.send("#{m}_values") == self.send("#{m}_values")
65
+ return false unless other.send("#{m}_values") == send("#{m}_values")
64
66
  end
65
67
  SINGLE_VALUE_METHODS.each do |m|
66
- return false unless other.send("#{m}_value") == self.send("#{m}_value")
68
+ return false unless other.send("#{m}_value") == send("#{m}_value")
67
69
  end
68
70
  return true
69
71
  when Array
70
72
  to_a == other
71
73
  end
72
74
  end
73
-
75
+
74
76
  # Returns true if there are any results given the current criteria
75
77
  def any?
76
78
  if block_given?
@@ -79,33 +81,35 @@ module DatastaxRails
79
81
  !empty?
80
82
  end
81
83
  end
82
- alias :exists? :any?
83
-
84
+ alias_method :exists?, :any?
85
+
84
86
  # Returns the total number of entries that match the given search.
85
87
  # This means the total number of matches regardless of page size.
86
88
  # If the relation has not been populated yet, a limit of 1 will be
87
89
  # placed on the query before it is executed.
88
90
  #
89
- # For a grouped query, this still returns the total number of
91
+ # For a grouped query, this still returns the total number of
90
92
  # matching documents
91
93
  #
92
94
  # Compare with #size.
93
95
  def count
94
- @count ||= (with_default_scope.path_decision == :solr) ? with_default_scope.count_via_solr : with_default_scope.count_via_cql
96
+ @count ||= if with_default_scope.path_decision == :solr
97
+ with_default_scope.count_via_solr
98
+ else
99
+ with_default_scope.count_via_cql
100
+ end
95
101
  end
96
-
102
+
97
103
  def stats
98
- unless(loaded?)
99
- to_a
100
- end
104
+ loaded? || to_a
101
105
  @stats
102
106
  end
103
-
107
+
104
108
  # Returns the current page for will_paginate compatibility
105
109
  def current_page
106
- self.page_value.try(:to_i)
110
+ page_value.try(:to_i)
107
111
  end
108
-
112
+
109
113
  # current_page - 1 or nil if there is no previous page
110
114
  def previous_page
111
115
  current_page > 1 ? (current_page - 1) : nil
@@ -115,14 +119,14 @@ module DatastaxRails
115
119
  def next_page
116
120
  current_page < total_pages ? (current_page + 1) : nil
117
121
  end
118
-
122
+
119
123
  # Gets a default scope with no conditions or search attributes set.
120
124
  def default_scope
121
125
  klass.scoped.with_default_scope
122
126
  end
123
-
127
+
124
128
  def with_default_scope #:nodoc:
125
- if default_scoped? && default_scope = klass.send(:build_default_scope)
129
+ if default_scoped? && (default_scope = klass.send(:build_default_scope))
126
130
  default_scope = default_scope.merge(self)
127
131
  default_scope.default_scoped = false
128
132
  default_scope
@@ -130,15 +134,15 @@ module DatastaxRails
130
134
  self
131
135
  end
132
136
  end
133
-
137
+
134
138
  # Returns true if there are no results given the current criteria
135
139
  def empty?
136
140
  return @results.empty? if loaded?
137
-
141
+
138
142
  c = count
139
143
  c.respond_to?(:zero?) ? c.zero? : c.empty?
140
144
  end
141
-
145
+
142
146
  # Returns true if there are multiple results given the current criteria
143
147
  def many?
144
148
  if block_given?
@@ -147,20 +151,20 @@ module DatastaxRails
147
151
  count > 1
148
152
  end
149
153
  end
150
-
154
+
151
155
  # Constructs a new instance of the class this relation points to with
152
156
  # any criteria from this relation applied
153
157
  def new(*args, &block)
154
158
  scoping { @klass.new(*args, &block) }
155
159
  end
156
-
160
+
157
161
  # Reloads the results from cassandra or solr as appropriate
158
162
  def reload
159
163
  reset
160
164
  to_a
161
165
  self
162
166
  end
163
-
167
+
164
168
  # Empties out the current results. The next call to to_a
165
169
  # will re-run the query.
166
170
  def reset
@@ -168,25 +172,25 @@ module DatastaxRails
168
172
  @stats = {}
169
173
  @results = []
170
174
  end
171
-
175
+
172
176
  # Copies will have changes made to the criteria and so need to be reset.
173
- def initialize_copy(other)
177
+ def initialize_copy(_other)
174
178
  reset
175
179
  @search = nil
176
180
  end
177
-
181
+
178
182
  # Performs a deep copy using Marshal when cloning.
179
183
  def clone
180
184
  dup.tap do |r|
181
185
  MULTI_VALUE_METHODS.each do |m|
182
- r.send("#{m}_values=", Marshal.load(Marshal.dump(self.send("#{m}_values"))))
186
+ r.send("#{m}_values=", Marshal.load(Marshal.dump(send("#{m}_values"))))
183
187
  end
184
188
  SINGLE_VALUE_METHODS.each do |m|
185
- r.send("#{m}_value=", Marshal.load(Marshal.dump(self.send("#{m}_value")))) if self.send("#{m}_value")
189
+ r.send("#{m}_value=", Marshal.load(Marshal.dump(send("#{m}_value")))) if send("#{m}_value")
186
190
  end
187
191
  end
188
192
  end
189
-
193
+
190
194
  # Returns the size of the total result set for the given criteria
191
195
  # NOTE that this takes pagination into account so will only return
192
196
  # the number of results in the current page. DatastaxRails models
@@ -201,14 +205,14 @@ module DatastaxRails
201
205
  total_entries = count
202
206
  (per_page_value && total_entries > per_page_value) ? per_page_value : total_entries
203
207
  end
204
-
208
+
205
209
  # Returns the total number of pages required to display the results
206
210
  # given the current page size. Used by will_paginate.
207
211
  def total_pages
208
212
  return 1 unless @per_page_value
209
213
  (count / @per_page_value.to_f).ceil
210
214
  end
211
-
215
+
212
216
  # Actually executes the query if not already executed.
213
217
  # Returns a standard array thus no more methods may be chained.
214
218
  def to_a
@@ -222,9 +226,9 @@ module DatastaxRails
222
226
  @loaded = true
223
227
  @results
224
228
  end
225
- alias :all :to_a
226
- alias :results :to_a
227
-
229
+ alias_method :all, :to_a
230
+ alias_method :results, :to_a
231
+
228
232
  # Create a new object with all of the criteria from this relation applied
229
233
  def create(*args, &block)
230
234
  scoping { @klass.create(*args, &block) }
@@ -234,14 +238,14 @@ module DatastaxRails
234
238
  def create!(*args, &block)
235
239
  scoping { @klass.create!(*args, &block) }
236
240
  end
237
-
241
+
238
242
  # Override respond_to? so that it matches method_missing
239
243
  def respond_to?(method, include_private = false)
240
244
  Array.method_defined?(method) ||
241
245
  @klass.respond_to?(method, include_private) ||
242
246
  super
243
247
  end
244
-
248
+
245
249
  def path_decision
246
250
  return :cassandra if klass <= DatastaxRails::CassandraOnlyModel
247
251
  case use_solr_value
@@ -252,28 +256,27 @@ module DatastaxRails
252
256
  else
253
257
  [order_values, where_not_values, fulltext_values, greater_than_values, less_than_values, field_facet_values,
254
258
  range_facet_values, group_value].each do |solr_only_stuff|
255
- return :solr unless solr_only_stuff.blank?
259
+ return :solr unless solr_only_stuff.blank?
256
260
  end
257
261
  return :solr unless group_value.blank?
258
262
  return :solr unless page_value == 1
259
263
  @where_values.each do |wv|
260
- wv.each do |k,v|
261
- unless klass.column_for_attribute(k).options[:cql_index]
262
- return :solr
263
- end
264
+ wv.each do |k, _v|
265
+ next if klass.column_for_attribute(k).options[:cql_index]
266
+ return :solr
264
267
  end
265
268
  end
266
269
  # If we get here, we can safely run this query via Cassandra
267
270
  return :cassandra
268
271
  end
269
272
  end
270
-
273
+
271
274
  # If we index something into both cassandra and solr, we rename the cassandra
272
275
  # column. This method maps the column names as necessary
273
276
  def map_cassandra_columns(conditions)
274
277
  {}.tap do |mapped|
275
- conditions.each do |k,v|
276
- if(klass.attribute_definitions[k].indexed == :both)
278
+ conditions.each do |k, v|
279
+ if (klass.attribute_definitions[k].indexed == :both)
277
280
  mapped["__#{k}"] = v
278
281
  else
279
282
  mapped[k] = v
@@ -281,7 +284,7 @@ module DatastaxRails
281
284
  end
282
285
  end
283
286
  end
284
-
287
+
285
288
  def count_via_cql
286
289
  cql = @cql.select(['count(*)'])
287
290
  cql.using(@consistency_value) if @consistency_value
@@ -291,28 +294,25 @@ module DatastaxRails
291
294
  cql.allow_filtering if @allow_filtering_value
292
295
  cql.execute.first['count']
293
296
  end
294
-
297
+
295
298
  # Constructs a CQL query and runs it against Cassandra directly. For this to
296
299
  # work, you need to run against either the primary key or a secondary index.
297
300
  # For ad-hoc queries, you will have to use Solr.
298
301
  def query_via_cql
299
- select_columns = select_values.empty? ? (@klass.attribute_definitions.keys - @klass.lazy_attributes) : select_values.flatten
302
+ select_columns =
303
+ select_values.empty? ? (@klass.attribute_definitions.keys - @klass.lazy_attributes) : select_values.flatten
300
304
  cql = @cql.select((select_columns + [@klass.primary_key]).uniq)
301
305
  cql.using(@consistency_value) if @consistency_value
302
306
  @where_values.each do |wv|
303
- cql.conditions(Hash[wv.map {|k,v| [(k.to_s == 'id' ? @klass.primary_key : k), v]}])
307
+ cql.conditions(Hash[wv.map { |k, v| [(k.to_s == 'id' ? @klass.primary_key : k), v] }])
304
308
  end
305
309
  @greater_than_values.each do |gtv|
306
- gtv.each do |k,v|
310
+ gtv.each do |k, v|
307
311
  # Special case if inequality is equal to the primary key (we're paginating)
308
- if(k.to_s == @klass.primary_key)
309
- cql.paginate(v)
310
- end
312
+ cql.paginate(v) if (k.to_s == @klass.primary_key)
311
313
  end
312
314
  end
313
- if(@per_page_value)
314
- cql.limit(@per_page_value)
315
- end
315
+ cql.limit(@per_page_value) if @per_page_value
316
316
  cql.allow_filtering if @allow_filtering_value
317
317
  results = []
318
318
  begin
@@ -327,20 +327,20 @@ module DatastaxRails
327
327
  raise
328
328
  end
329
329
  end
330
- if(@slow_order_values.any?)
331
- results.sort! do |a,b|
332
- values = slow_ordering(a,b)
330
+ if @slow_order_values.any?
331
+ results.sort! do |a, b|
332
+ values = slow_ordering(a, b)
333
333
  values[0] <=> values[1]
334
334
  end
335
335
  end
336
336
  results
337
337
  end
338
-
338
+
339
339
  def slow_ordering(obj1, obj2)
340
- [[],[]].tap do |values|
341
- i=0
340
+ [[], []].tap do |values|
341
+ i = 0
342
342
  @slow_order_values.each do |ordering|
343
- ordering.each do |k,v|
343
+ ordering.each do |k, v|
344
344
  if v == :asc
345
345
  values[0][i] = obj1.send(k)
346
346
  values[1][i] = obj2.send(k)
@@ -353,14 +353,14 @@ module DatastaxRails
353
353
  end
354
354
  end
355
355
  end
356
-
356
+
357
357
  # Runs the query with a limit of 1 just to grab the total results attribute off
358
- # the result set.
358
+ # the result set.
359
359
  def count_via_solr
360
360
  results = limit(1).select(:id).to_a
361
361
  @group_value ? results.total_for_all : results.total_entries
362
362
  end
363
-
363
+
364
364
  # Escapes values that might otherwise mess up the URL or confuse SOLR.
365
365
  # If you want to handle escaping yourself for a particular query then
366
366
  # SearchMethods#dont_escape is what you're looking for.
@@ -371,82 +371,74 @@ module DatastaxRails
371
371
  str
372
372
  end
373
373
  end
374
-
374
+
375
375
  def full_solr_range(attr)
376
- if(self.klass.attribute_definitions[attr])
377
- self.klass.attribute_definitions[attr].full_solr_range
376
+ if klass.attribute_definitions[attr]
377
+ klass.attribute_definitions[attr].full_solr_range
378
378
  else
379
379
  '[\"\" TO *]'
380
380
  end
381
381
  end
382
-
383
- # Constructs a solr query to run against SOLR. At this point, only where, where_not,
382
+
383
+ # Constructs a solr query to run against SOLR. At this point, only where, where_not,
384
384
  # fulltext, order and pagination are supported. More will be added.
385
385
  #
386
386
  # It's also worth noting that where and where_not make use of individual filter_queries.
387
387
  # If that's not what you want, you might be better off constructing your own fulltext
388
388
  # query and sending that in.
389
- def query_via_solr
389
+ #
390
+ # TODO: break this apart into multiple methods
391
+ def query_via_solr # rubocop:disable all
390
392
  filter_queries = []
391
393
  orders = []
392
394
  @where_values.each do |wv|
393
- wv.each do |k,v|
395
+ wv.each do |k, v|
394
396
  # If v is blank, check that there is no value for the field in the document
395
397
  filter_queries << (v.blank? ? "-#{k}:#{full_solr_range(k)}" : "#{k}:(#{v})")
396
398
  end
397
399
  end
398
-
400
+
399
401
  @where_not_values.each do |wnv|
400
- wnv.each do |k,v|
402
+ wnv.each do |k, v|
401
403
  # If v is blank, check for any value for the field in document
402
404
  filter_queries << (v.blank? ? "#{k}:#{full_solr_range(k)}" : "-#{k}:(#{v})")
403
405
  end
404
406
  end
405
-
407
+
406
408
  @greater_than_values.each do |gtv|
407
- gtv.each do |k,v|
409
+ gtv.each do |k, v|
408
410
  filter_queries << "#{k}:[#{v} TO *]"
409
411
  end
410
412
  end
411
-
413
+
412
414
  @less_than_values.each do |ltv|
413
- ltv.each do |k,v|
415
+ ltv.each do |k, v|
414
416
  filter_queries << "#{k}:[* TO #{v}]"
415
417
  end
416
418
  end
417
-
419
+
418
420
  @order_values.each do |ov|
419
- ov.each do |k,v|
420
- if(@reverse_order_value)
421
+ ov.each do |k, v|
422
+ if @reverse_order_value
421
423
  orders << "#{k} #{v == :asc ? 'desc' : 'asc'}"
422
424
  else
423
425
  orders << "#{k} #{v == :asc ? 'asc' : 'desc'}"
424
426
  end
425
427
  end
426
428
  end
427
-
428
- sort = orders.join(",")
429
-
430
- if @fulltext_values.empty?
431
- q = "*:*"
432
- else
433
- q = @fulltext_values.collect {|ftv| "(" + ftv[:query] + ")"}.join(' AND ')
434
- hl_fields = @fulltext_values.collect { |ftv| ftv[:highlight].join(",") if ftv[:highlight].present? }.join(",")
435
- end
436
-
437
- params = {:q => q}
438
- unless sort.empty?
439
- params[:sort] = sort
440
- end
441
-
442
- unless filter_queries.empty?
443
- params[:fq] = filter_queries
444
- end
429
+
430
+ sort = orders.join(',')
431
+
432
+ q = @fulltext_values.empty? ? '*:*' : @fulltext_values.map { |ftv| '(' + ftv[:query] + ')' }.join(' AND ')
433
+
434
+ params = { q: q }
435
+ params[:sort] = sort
436
+ params[:fq] = filter_queries unless filter_queries.empty?
445
437
 
446
438
  # Facets
447
439
  # facet=true to enable faceting, facet.field=<field_name> (can appear more than once for multiple fields)
448
440
  # Additional options: f.<field_name>.facet.<option> [e.g. f.author.facet.sort=index]
449
-
441
+
450
442
  # Facet Fields
451
443
  unless field_facet_values.empty?
452
444
  params['facet'] = 'true'
@@ -454,7 +446,7 @@ module DatastaxRails
454
446
  field_facet_values.each do |facet|
455
447
  facet_field = facet[:field]
456
448
  facet_fields << facet_field
457
- facet[:options].each do |key,value|
449
+ facet[:options].each do |key, value|
458
450
  params["f.#{facet_field}.facet.#{key}"] = value.to_s
459
451
  end
460
452
  end
@@ -468,13 +460,13 @@ module DatastaxRails
468
460
  range_facet_values.each do |facet|
469
461
  facet_field = facet[:field]
470
462
  facet_fields << facet_field
471
- facet[:options].each do |key,value|
463
+ facet[:options].each do |key, value|
472
464
  params["f.#{facet_field}.facet.range.#{key}"] = value.to_s
473
465
  end
474
466
  end
475
467
  params['facet.range'] = facet_fields
476
468
  end
477
-
469
+
478
470
  if @highlight_options[:fields].present?
479
471
  params[:hl] = true
480
472
  params['hl.fl'] = @highlight_options[:fields]
@@ -485,52 +477,53 @@ module DatastaxRails
485
477
  params['hl.tag.pre'] = @highlight_options[:pre_tag] if @highlight_options[:pre_tag].present?
486
478
  params['hl.tag.post'] = @highlight_options[:post_tag] if @highlight_options[:post_tag].present?
487
479
  else
488
- params['hl.mergeContiguous'] = !!@highlight_options[:merge_contiguous]
480
+ params['hl.mergeContiguous'] = @highlight_options[:merge_contiguous].present?
489
481
  params['hl.simple.pre'] = @highlight_options[:pre_tag] if @highlight_options[:pre_tag].present?
490
482
  params['hl.simple.post'] = @highlight_options[:post_tag] if @highlight_options[:post_tag].present?
491
483
  end
492
484
  end
493
-
494
- select_columns = select_values.empty? ? (@klass.attribute_definitions.keys - @klass.lazy_attributes) : select_values.flatten
485
+
486
+ select_columns =
487
+ select_values.empty? ? (@klass.attribute_definitions.keys - @klass.lazy_attributes) : select_values.flatten
495
488
  select_columns << @klass.primary_key
496
- select_columns.collect! {|c| @klass.column_for_attribute(c).try(:type) == :map ? "#{c.to_s}*" : c.to_s}
497
- params[:fl] = select_columns.uniq.join(",")
498
- unless(@stats_values.empty?)
489
+ select_columns.map! { |c| @klass.column_for_attribute(c).try(:type) == :map ? "#{c}*" : c.to_s }
490
+ params[:fl] = select_columns.uniq.join(',')
491
+ unless @stats_values.empty?
499
492
  params[:stats] = 'true'
500
493
  @stats_values.flatten.each do |sv|
501
494
  params['stats.field'] = sv
502
495
  end
503
- if(@group_value)
504
- params['stats.facet'] = @group_value
505
- end
496
+ @params['stats.facet'] = @group_value
506
497
  end
507
498
  solr_response = nil
508
- if(@group_value)
499
+ if @group_value
509
500
  results = DatastaxRails::GroupedCollection.new
510
501
  params[:group] = 'true'
511
- params[:rows] = 10000
502
+ params[:rows] = 10_000
512
503
  params['group.field'] = @group_value
513
504
  params['group.limit'] = @per_page_value
514
505
  params['group.offset'] = (@page_value - 1) * @per_page_value
515
506
  params['group.ngroups'] = 'false' # must be false due to issues with solr sharding
516
- solr_response = rsolr.post('select', :data => params)
517
- response = solr_response["grouped"][@group_value.to_s]
507
+ solr_response = rsolr.post('select', data: params)
508
+ response = solr_response['grouped'][@group_value.to_s]
518
509
  results.total_groups = response['groups'].size
519
510
  results.total_for_all = response['matches'].to_i
520
511
  results.total_entries = 0
521
512
  response['groups'].each do |group|
522
513
  results[group['groupValue']] = parse_docs(group['doclist'], select_columns)
523
- results.total_entries = results[group['groupValue']].total_entries if results[group['groupValue']].total_entries > results.total_entries
514
+ if results[group['groupValue']].total_entries > results.total_entries
515
+ results.total_entries = results[group['groupValue']].total_entries
516
+ end
524
517
  end
525
518
  else
526
- solr_response = rsolr.paginate(@page_value, @per_page_value, 'select', :data => params, :method => :post)
527
- response = solr_response["response"]
519
+ solr_response = rsolr.paginate(@page_value, @per_page_value, 'select', data: params, method: :post)
520
+ response = solr_response['response']
528
521
  pp solr_response if ENV['DEBUG_SOLR'] == 'true'
529
522
  results = parse_docs(response, select_columns)
530
523
  results.highlights = solr_response['highlighting']
531
524
  end
532
- if solr_response["stats"]
533
- @stats = solr_response["stats"]["stats_fields"].with_indifferent_access
525
+ if solr_response['stats']
526
+ @stats = solr_response['stats']['stats_fields'].with_indifferent_access
534
527
  end
535
528
  # Apply Facets if they exist
536
529
  if solr_response['facet_counts']
@@ -541,7 +534,7 @@ module DatastaxRails
541
534
  pp params if ENV['DEBUG_SOLR'] == 'true'
542
535
  results
543
536
  end
544
-
537
+
545
538
  # Parse out a set of documents and return the results
546
539
  #
547
540
  # @param response [Hash] the response hash from SOLR with a set of documents
@@ -555,7 +548,7 @@ module DatastaxRails
555
548
  results.total_entries = response['numFound'].to_i
556
549
  response['docs'].each do |doc|
557
550
  id = @klass.attribute_definitions[@klass.primary_key].type_cast(doc[@klass.primary_key])
558
- if(@consistency_value)
551
+ if @consistency_value
559
552
  obj = @klass.with_cassandra.consistency(@consistency_value).find_by_id(id)
560
553
  results << obj if obj
561
554
  else
@@ -565,7 +558,7 @@ module DatastaxRails
565
558
  results
566
559
  end
567
560
  protected(:parse_docs)
568
-
561
+
569
562
  # Inspects the results of the search instead of the Relation itself.
570
563
  # Passing true causes the Relation to be inspected.
571
564
  #
@@ -573,7 +566,7 @@ module DatastaxRails
573
566
  def inspect(just_me = false)
574
567
  just_me ? super() : to_a.inspect
575
568
  end
576
-
569
+
577
570
  # Scope all queries to the current scope.
578
571
  #
579
572
  # ==== Example
@@ -587,10 +580,10 @@ module DatastaxRails
587
580
  def scoping
588
581
  @klass.send(:with_scope, self, :overwrite) { yield }
589
582
  end
590
-
583
+
591
584
  # Merges all of the where values together into a single hash
592
585
  def where_values_hash
593
- where_values.inject({}) { |values,v| values.merge(v) }
586
+ where_values.reduce({}) { |a, e| a.merge(e) }
594
587
  end
595
588
 
596
589
  # Creates a scope that includes all of the where values plus anything
@@ -598,12 +591,12 @@ module DatastaxRails
598
591
  def scope_for_create
599
592
  @scope_for_create ||= where_values_hash.merge(create_with_value)
600
593
  end
601
-
594
+
602
595
  # Sends a commit message to SOLR
603
596
  def commit_solr
604
- rsolr.commit :commit_attributes => {}
597
+ rsolr.commit commit_attributes: {}
605
598
  end
606
-
599
+
607
600
  SOLR_DATE_REGEX = /(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)/i
608
601
  # Everything that gets indexed into solr is downcased as part of the analysis phase.
609
602
  # Normally, this is done to the query as well, but if your query includes wildcards
@@ -611,36 +604,38 @@ module DatastaxRails
611
604
  # We therefore need to perform the downcasing ourselves. This does it while still
612
605
  # leaving boolean operations (AND, OR, NOT, TO) and dates upcased.
613
606
  def downcase_query(value)
614
- if(value.is_a?(String))
615
- value.split(/\bAND\b/).collect do |a|
616
- a.split(/\bOR\b/).collect do |o|
617
- o.split(/\bNOT\b/).collect do |n|
618
- n.split(/\bTO\b/).collect do |t|
607
+ # rubocop:disable Style/MultilineBlockChain
608
+ if value.is_a?(String)
609
+ value.split(/\bAND\b/).map do |a|
610
+ a.split(/\bOR\b/).map do |o|
611
+ o.split(/\bNOT\b/).map do |n|
612
+ n.split(/\bTO\b/).map do |t|
619
613
  t.downcase
620
- end.join("TO")
621
- end.join("NOT")
622
- end.join("OR")
623
- end.join("AND").gsub(SOLR_DATE_REGEX) { $1.upcase }
614
+ end.join('TO')
615
+ end.join('NOT')
616
+ end.join('OR')
617
+ end.join('AND').gsub(SOLR_DATE_REGEX) { Regexp.last_match[1].upcase }
624
618
  else
625
619
  value
626
620
  end
621
+ # rubocop:enable Style/MultilineBlockChain
627
622
  end
628
-
623
+
629
624
  protected
630
-
631
- def method_missing(method, *args, &block) #:nodoc:
632
- if DatastaxRails::Collection.method_defined?(method)
633
- to_a.send(method, *args, &block)
634
- elsif @klass.respond_to?(method, true)
635
- scoping { @klass.send(method, *args, &block) }
636
- else
637
- super
638
- end
639
- end
640
-
641
- # Calculates the solr URL and sets up an RSolr connection
642
- def rsolr
643
- @klass.solr_connection
625
+
626
+ def method_missing(method, *args, &block) #:nodoc:
627
+ if DatastaxRails::Collection.method_defined?(method)
628
+ to_a.send(method, *args, &block)
629
+ elsif @klass.respond_to?(method, true)
630
+ scoping { @klass.send(method, *args, &block) }
631
+ else
632
+ super
644
633
  end
634
+ end
635
+
636
+ # Calculates the solr URL and sets up an RSolr connection
637
+ def rsolr
638
+ @klass.solr_connection
639
+ end
645
640
  end
646
- end
641
+ end