datastax_rails 2.0.12 → 2.0.15

Sign up to get free protection for your applications and to get access to all the features.
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,19 +1,19 @@
1
1
  module DatastaxRails
2
2
  class DatastaxRailsError < StandardError #:nodoc:
3
3
  end
4
-
4
+
5
5
  class AssociationTypeMismatch < DatastaxRailsError #:nodoc:
6
6
  end
7
-
7
+
8
8
  class RecordNotSaved < DatastaxRailsError #:nodoc:
9
9
  end
10
-
10
+
11
11
  class DeleteRestrictionError < DatastaxRailsError #:nodoc:
12
12
  end
13
-
13
+
14
14
  class RecordNotFound < DatastaxRailsError #:nodoc:
15
15
  end
16
-
16
+
17
17
  class UnknownAttributeError < DatastaxRailsError
18
18
  end
19
- end
19
+ end
@@ -3,15 +3,15 @@ module DatastaxRails
3
3
  # for the thing that was grouped on. The hash entries point to instances of DatastaxRails::Collection.
4
4
  class GroupedCollection < Hash
5
5
  # @!attribute [r] total_entries
6
- # @return [Fixnum] the total number of entries *in the largest group*. This is to allow will_paginate to work properly.
6
+ # @return [Fixnum] the total number of entries *in the largest group*. This is to allow will_paginate to work.
7
7
  # @!attribute [r] total_groups
8
8
  # @return [Fixnum] the total number of groups if the groups were paginated (not supported yet)
9
9
  # @!attribute [r] total_for_all
10
10
  # @return [Fixnum] the total number of entries across all groups that match this search
11
11
  attr_accessor :total_entries, :total_groups, :total_for_all
12
-
12
+
13
13
  def inspect
14
14
  "<DatastaxRails::GroupedCollection##{object_id} contents: #{super} matching_records #{total_for_all}>"
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -1,24 +1,24 @@
1
1
  module DatastaxRails
2
2
  module Inheritance
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  module ClassMethods
6
6
  # Determines if one of the attributes passed in is the inheritance column,
7
7
  # and if the inheritance column is attr accessible, it initializes an
8
8
  # instance of the given subclass instead of the base class.
9
9
  def new(*args, &block)
10
10
  if abstract_class? || self == Base
11
- raise NotImplementedError, "#{self} is an abstract class and can not be instantiated."
11
+ fail NotImplementedError, "#{self} is an abstract class and can not be instantiated."
12
12
  end
13
13
  # if (attrs = args.first).is_a?(Hash)
14
- # if subclass = subclass_from_attrs(attrs)
15
- # return subclass.new(*args, &block)
16
- # end
14
+ # if subclass = subclass_from_attrs(attrs)
15
+ # return subclass.new(*args, &block)
16
+ # end
17
17
  # end
18
18
  # Delegate to the original .new
19
19
  super
20
20
  end
21
-
21
+
22
22
  # Returns the class descending directly from DatastaxRails::Base, or
23
23
  # an abstract class, if any, in the inheritance hierarchy.
24
24
  #
@@ -29,7 +29,7 @@ module DatastaxRails
29
29
  # and C.base_class would return B as the answer since A is an abstract_class.
30
30
  def base_class
31
31
  unless self < Base
32
- raise DatastaxRailsError, "#{name} doesn't belong in a hierarchy descending from DatastaxRails"
32
+ fail DatastaxRailsError, "#{name} doesn't belong in a hierarchy descending from DatastaxRails"
33
33
  end
34
34
 
35
35
  if superclass == Base || superclass.abstract_class?
@@ -38,7 +38,7 @@ module DatastaxRails
38
38
  superclass.base_class
39
39
  end
40
40
  end
41
-
41
+
42
42
  # Set this to true if this is an abstract class (see <tt>abstract_class?</tt>).
43
43
  # If you are using inheritance with DatastaxRails and don't want child classes
44
44
  # to utilize the implied STI table name of the parent class, this will need to be true.
@@ -58,4 +58,4 @@ module DatastaxRails
58
58
  end
59
59
  end
60
60
  end
61
- end
61
+ end
@@ -17,7 +17,7 @@ module DatastaxRails
17
17
  class PayloadModel < WideStorageModel
18
18
  include CassandraOnlyModel
19
19
  self.abstract_class = true
20
-
20
+
21
21
  def self.inherited(child)
22
22
  super
23
23
  child.primary_key = 'digest'
@@ -26,14 +26,16 @@ module DatastaxRails
26
26
  child.string :digest
27
27
  child.binary :payload
28
28
  child.integer :chunk
29
- child.validates :digest, :presence => true
29
+ child.validates :digest, presence: true
30
30
  end
31
-
31
+
32
32
  def self.find(digest, options = {})
33
- raise ArgumentError, "'#{options[:consistency]}' is not a valid Cassandra consistency level" unless valid_consistency?(options[:consistency].to_s.upcase) if options[:consistency]
34
- c = cql.select.conditions(:digest => digest).order('chunk')
33
+ if options[:consistency] && !valid_consistency?(options[:consistency].to_s.upcase)
34
+ fail ArgumentError, "'#{options[:consistency]}' is not a valid Cassandra consistency level"
35
+ end
36
+ c = cql.select.conditions(digest: digest).order('chunk')
35
37
  c.using(options[:consistency]) if options[:consistency]
36
- io = StringIO.new("","w+")
38
+ io = StringIO.new('', 'w+')
37
39
  found = false
38
40
  chunk = 0
39
41
  c.execute.each do |row|
@@ -41,34 +43,36 @@ module DatastaxRails
41
43
  chunk = row['chunk']
42
44
  found = true
43
45
  end
44
- raise DatastaxRails::RecordNotFound unless found
46
+ fail DatastaxRails::RecordNotFound unless found
45
47
  io.rewind
46
- self.instantiate(digest, {:digest => digest, :payload => io.read, :chunk => chunk}, [:digest, :payload])
48
+ instantiate(digest, { digest: digest, payload: io.read, chunk: chunk }, [:digest, :payload])
47
49
  end
48
-
50
+
49
51
  def self.write(record, options = {})
50
- raise ArgumentError, "'#{options[:consistency]}' is not a valid Cassandra consistency level" unless valid_consistency?(options[:consistency].to_s.upcase) if options[:consistency]
51
- c = self.cql.select("count(*)").conditions(:digest => record.id)
52
- count = c.execute.first["count"]
53
-
52
+ if options[:consistency] && !valid_consistency?(options[:consistency].to_s.upcase)
53
+ fail ArgumentError, "'#{options[:consistency]}' is not a valid Cassandra consistency level"
54
+ end
55
+ c = cql.select('count(*)').conditions(digest: record.id)
56
+ count = c.execute.first['count']
57
+
54
58
  i = 0
55
59
  io = StringIO.new(record.attributes['payload'])
56
- while chunk = io.read(1.megabyte)
57
- c = cql.insert.columns(:digest => record.id, :chunk => i, :payload => Base64.encode64(chunk))
60
+ while (chunk = io.read(1.megabyte))
61
+ c = cql.insert.columns(digest: record.id, chunk: i, payload: Base64.encode64(chunk))
58
62
  c.using(options[:consistency]) if options[:consistency]
59
63
  c.execute
60
64
  i += 1
61
65
  end
62
-
63
- if count and count > i
66
+
67
+ if count && count > i
64
68
  i.upto(count) do |j|
65
- c = cql.delete(record.id).key_name('digest').conditions(:chunk => j)
69
+ c = cql.delete(digest: record.id, chunk: j)
66
70
  c.using(options[:consistency]) if options[:consistency]
67
71
  c.execute
68
72
  end
69
73
  end
70
-
74
+
71
75
  record.id
72
76
  end
73
77
  end
74
- end
78
+ end
@@ -1,12 +1,21 @@
1
1
  module DatastaxRails
2
+ # Handles persisting data into Datastax
2
3
  module Persistence
3
4
  extend ActiveSupport::Concern
4
-
5
+
6
+ included do
7
+ attr_reader :destroyed, :new_record
8
+ alias_method :destroyed?, :destroyed
9
+ alias_method :new_record?, :new_record
10
+ end
11
+
12
+ # rubocop:disable Style/Documentation
5
13
  module ClassMethods
6
- # Removes one or more records with corresponding keys. Last parameter can be a hash
7
- # specifying the consistency level.
14
+ # Removes one or more records with corresponding keys. Last parameter can be a hash
15
+ # specifying the consistency level. The keys should be in the form returned by
16
+ # +#id_for_update+
8
17
  #
9
- # Model.remove('12345','67890', :consistency => 'LOCAL_QUORUM)
18
+ # Model.remove({id: '12345'},{id: '67890'}, :consistency => 'LOCAL_QUORUM)
10
19
  #
11
20
  # @overload remove(*keys, options)
12
21
  # Removes one or more keys with the given options
@@ -15,55 +24,60 @@ module DatastaxRails
15
24
  # @overload remove(*keys)
16
25
  # Removes one or more keys with the default options
17
26
  # @param [String] keys one or more keys to delete
27
+ # TODO: Submit multiple deletes as a batch
18
28
  def remove(*keys)
19
- options = {}
20
- if keys.last.is_a?(Hash)
21
- options = keys.pop
22
- end
23
- keys = keys.flatten.collect {|k| self.attribute_definitions[self.primary_key].type_cast(k)}
24
- ActiveSupport::Notifications.instrument("remove.datastax_rails", :column_family => column_family, :key => keys) do
25
- c = cql.delete(keys)
26
- if(options[:consistency])
27
- level = options[:consistency].to_s.upcase
28
- if(valid_consistency?(level))
29
- c.using(level)
30
- else
31
- raise ArgumentError, "'#{level}' is not a valid Cassandra consistency level"
29
+ options = keys.last.is_a?(Hash) ? keys.pop : {}
30
+ # keys = keys.flat_map { |k| attribute_definitions[primary_key].type_cast(k) }
31
+ keys.each do |key|
32
+ typecast_key = {}
33
+ key.each { |k, v| typecast_key[k] = attribute_definitions[k].type_cast(v) }
34
+
35
+ ActiveSupport::Notifications.instrument('remove.datastax_rails', column_family: column_family, key: key) do
36
+ c = cql.delete(typecast_key)
37
+ if options[:consistency]
38
+ level = options[:consistency].to_s.upcase
39
+ if valid_consistency?(level)
40
+ c.using(level)
41
+ else
42
+ fail ArgumentError, "'#{level}' is not a valid Cassandra consistency level"
43
+ end
32
44
  end
45
+ c.execute
33
46
  end
34
- c.execute
35
47
  end
36
48
  end
37
49
 
38
50
  # Truncates the column_family associated with this class
39
51
  def truncate
40
- ActiveSupport::Notifications.instrument("truncate.datastax_rails", :column_family => column_family) do
52
+ ActiveSupport::Notifications.instrument('truncate.datastax_rails', column_family: column_family) do
41
53
  cql.truncate.execute
42
54
  end
43
55
  end
44
- alias :delete_all :truncate
56
+ alias_method :delete_all, :truncate
45
57
 
46
58
  def create(attributes = {}, options = {}, &block)
47
59
  new(attributes, &block).tap do |object|
48
60
  object.save(options)
49
61
  end
50
62
  end
51
-
63
+
52
64
  # Write a record to cassandra. Can be either an insert or an update (they are exactly the same to cassandra)
53
65
  #
54
66
  # @param [DatastaxRails::Base] record the record that we are writing
55
67
  # @param [Hash] options a hash containing various options
56
68
  # @option options [Symbol] :consistency the consistency to set for the Cassandra operation (e.g., ALL)
57
69
  def write(record, options = {})
58
- level = (options[:consistency] || self.default_consistency).to_s.upcase
59
- if(valid_consistency?(level))
70
+ level = (options[:consistency] || default_consistency).to_s.upcase
71
+ if valid_consistency?(level)
60
72
  options[:consistency] = level
61
73
  else
62
- raise ArgumentError, "'#{level}' is not a valid Cassandra consistency level"
74
+ fail ArgumentError, "'#{level}' is not a valid Cassandra consistency level"
63
75
  end
64
76
  record.id.tap do |key|
65
- ActiveSupport::Notifications.instrument("insert.datastax_rails", :column_family => column_family, :key => key.to_s, :attributes => record.attributes) do
66
- if(self.storage_method == :solr)
77
+ ActiveSupport::Notifications.instrument('insert.datastax_rails', column_family: column_family,
78
+ key: key.to_s,
79
+ attributes: record.attributes) do
80
+ if (storage_method == :solr)
67
81
  write_with_solr(record, options)
68
82
  else
69
83
  write_with_cql(record, options)
@@ -71,18 +85,18 @@ module DatastaxRails
71
85
  end
72
86
  end
73
87
  end
74
-
88
+
75
89
  # Instantiates a new object without calling +initialize+.
76
90
  #
77
91
  # @param [String] key the primary key for the record
78
92
  # @param [Hash] attributes a hash containing the columns to set on the record
79
- # @param [Array] selected_attributes an array containing the attributes that were originally selected from cassandra
80
- # to build this object. Used so that we can avoid lazy-loading attributes that don't exist.
93
+ # @param [Array] selected_attributes an array containing the attributes that were originally selected from
94
+ # cassandra to build this object. Used so that we can avoid lazy-loading attributes that don't exist.
81
95
  # @return [DatastaxRails::Base] a model with the given attributes
82
- def instantiate(key, attributes, selected_attributes = [])
96
+ def instantiate(_key, attributes, _selected_attributes = [])
83
97
  allocate.init_with('attributes' => attributes)
84
98
  end
85
-
99
+
86
100
  # Encodes the attributes in preparation for storing in cassandra. Calls the coders on the various type classes
87
101
  # to do the heavy lifting.
88
102
  #
@@ -100,30 +114,23 @@ module DatastaxRails
100
114
  end
101
115
  encoded
102
116
  end
103
-
117
+
104
118
  private
105
- def write_with_cql(record, options)
106
- encoded = encode_attributes(record, true)
107
- if options[:new_record]
108
- cql.insert.columns(encoded).using(options[:consistency]).execute
109
- else
110
- cql.update(record.id_for_update).columns(encoded).using(options[:consistency]).execute
111
- end
112
- end
113
-
114
- def write_with_solr(record, options)
115
- encoded = encode_attributes(record, false)
116
- xml_doc = RSolr::Xml::Generator.new.add(encoded.merge(self.primary_key => record.id.to_s))
117
- self.solr_connection.update(:data => xml_doc, :params => {:replacefields => false, :cl => options[:consistency]})
118
- end
119
- end
120
119
 
121
- def new_record?
122
- @new_record
123
- end
120
+ def write_with_cql(record, options)
121
+ encoded = encode_attributes(record, true)
122
+ if options[:new_record]
123
+ cql.insert.columns(encoded).using(options[:consistency]).execute
124
+ else
125
+ cql.update(record.id_for_update).columns(encoded).using(options[:consistency]).execute
126
+ end
127
+ end
124
128
 
125
- def destroyed?
126
- @destroyed
129
+ def write_with_solr(record, options)
130
+ encoded = encode_attributes(record, false)
131
+ xml_doc = RSolr::Xml::Generator.new.add(encoded.merge(primary_key => record.id.to_s))
132
+ solr_connection.update(data: xml_doc, params: { replacefields: false, cl: options[:consistency] })
133
+ end
127
134
  end
128
135
 
129
136
  def persisted?
@@ -131,45 +138,43 @@ module DatastaxRails
131
138
  end
132
139
 
133
140
  def save(options = {})
134
- begin
135
- _create_or_update(options)
136
- rescue DatastaxRails::RecordInvalid
137
- false
138
- end
141
+ _create_or_update(options)
142
+ rescue DatastaxRails::RecordInvalid
143
+ false
139
144
  end
140
145
 
141
146
  def save!(options = {})
142
- _create_or_update(options) || raise(RecordNotSaved)
147
+ _create_or_update(options) || fail(RecordNotSaved)
143
148
  end
144
149
 
145
150
  def destroy(options = {})
146
- self.class.remove(id, options)
151
+ self.class.remove(id_for_update, options)
147
152
  @destroyed = true
148
153
  freeze
149
154
  end
150
-
155
+
151
156
  # TODO: Make this work
152
157
  # def touch(name = nil)
153
- # raise ActiveRecordError, "can not touch on a new record object" unless persisted?
154
- #
155
- # attributes = timestamp_attributes_for_update_in_model
156
- # attributes << name if name
157
- #
158
- # unless attributes.empty?
159
- # current_time = current_time_from_proper_timezone
160
- # changes = {}
161
- #
162
- # attributes.each do |column|
163
- # column = column.to_s
164
- # changes[column] = write_attribute(column, current_time)
165
- # end
166
- #
167
- # @changed_attributes.except!(*changes.keys)
168
- # primary_key = self.class.primary_key
169
- # self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
170
- # else
171
- # true
172
- # end
158
+ # raise ActiveRecordError, "can not touch on a new record object" unless persisted?
159
+ #
160
+ # attributes = timestamp_attributes_for_update_in_model
161
+ # attributes << name if name
162
+ #
163
+ # unless attributes.empty?
164
+ # current_time = current_time_from_proper_timezone
165
+ # changes = {}
166
+ #
167
+ # attributes.each do |column|
168
+ # column = column.to_s
169
+ # changes[column] = write_attribute(column, current_time)
170
+ # end
171
+ #
172
+ # @changed_attributes.except!(*changes.keys)
173
+ # primary_key = self.class.primary_key
174
+ # self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
175
+ # else
176
+ # true
177
+ # end
173
178
  # end
174
179
 
175
180
  # Updates a single attribute and saves the record.
@@ -181,24 +186,24 @@ module DatastaxRails
181
186
  # * Updates all the attributes that are dirty in this object.
182
187
  #
183
188
  def update_attribute(name, value)
184
- send("#{name.to_s}=", value)
185
- save(:validate => false)
189
+ send("#{name}=", value)
190
+ save(validate: false)
186
191
  end
187
192
 
188
193
  # Updates the attributes of the model from the passed-in hash and saves the
189
194
  # record If the object is invalid, the saving will fail and false will be returned.
190
- def update_attributes(attributes, options = {})
191
- self.assign_attributes(attributes)
195
+ def update_attributes(attributes, _options = {})
196
+ assign_attributes(attributes)
192
197
  save
193
198
  end
194
199
 
195
200
  # Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
196
201
  # of +save+, so an exception is raised if the record is invalid.
197
- def update_attributes!(attributes, options = {})
198
- self.assign_attributes(attributes)
202
+ def update_attributes!(attributes, _options = {})
203
+ assign_attributes(attributes)
199
204
  save!
200
205
  end
201
-
206
+
202
207
  # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
203
208
  # if the predicate returns +true+ the attribute will become +false+. This
204
209
  # method toggles directly the underlying value without calling any setter.
@@ -215,7 +220,7 @@ module DatastaxRails
215
220
  def toggle!(attribute)
216
221
  toggle(attribute).update_attribute(attribute, self[attribute])
217
222
  end
218
-
223
+
219
224
  # Reloads the attributes of this object from the database.
220
225
  # The optional options argument is passed to find when reloading so you
221
226
  # may do e.g. record.reload(:lock => true) to reload the same record with
@@ -223,7 +228,7 @@ module DatastaxRails
223
228
  def reload(options = nil)
224
229
  clear_association_cache
225
230
 
226
- fresh_object = self.class.unscoped { self.class.find(self.id, options) }
231
+ fresh_object = self.class.unscoped { self.class.find(id, options) }
227
232
  @attributes.update(fresh_object.instance_variable_get('@attributes'))
228
233
 
229
234
  @attributes_cache = {}
@@ -231,27 +236,28 @@ module DatastaxRails
231
236
  end
232
237
 
233
238
  private
234
- def _create_or_update(options)
235
- result = new_record? ? _create_record(options) : _update_record(options)
236
- result != false
237
- end
238
239
 
239
- def _create_record(options)
240
- # TODO: handle the non-UUID case
241
- self.id ||= ::Cql::TimeUuid::Generator.new.next
242
- _write(options)
243
- @new_record = false
244
- self.id
245
- end
246
-
247
- def _update_record(options)
248
- _write(options)
249
- end
250
-
251
- def _write(options) #:nodoc:
252
- options[:new_record] = new_record?
253
- return true if changed_attributes.empty?
254
- self.class.write(self, options)
255
- end
240
+ def _create_or_update(options)
241
+ result = new_record? ? _create_record(options) : _update_record(options)
242
+ result != false
243
+ end
244
+
245
+ def _create_record(options)
246
+ # TODO: handle the non-UUID case
247
+ self.id ||= ::Cql::TimeUuid::Generator.new.next
248
+ _write(options)
249
+ @new_record = false
250
+ self.id
251
+ end
252
+
253
+ def _update_record(options)
254
+ _write(options)
255
+ end
256
+
257
+ def _write(options) #:nodoc:
258
+ options[:new_record] = new_record?
259
+ return true if changed_attributes.empty?
260
+ self.class.write(self, options)
261
+ end
256
262
  end
257
263
  end