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.
- checksums.yaml +4 -4
- data/Rakefile +5 -5
- data/lib/blankslate.rb +8 -11
- data/lib/cql-rb_extensions.rb +5 -3
- data/lib/datastax_rails/associations/association.rb +93 -101
- data/lib/datastax_rails/associations/association_scope.rb +7 -7
- data/lib/datastax_rails/associations/belongs_to_association.rb +46 -48
- data/lib/datastax_rails/associations/builder/association.rb +32 -31
- data/lib/datastax_rails/associations/builder/belongs_to.rb +19 -20
- data/lib/datastax_rails/associations/builder/collection_association.rb +32 -32
- data/lib/datastax_rails/associations/builder/has_and_belongs_to_many.rb +21 -21
- data/lib/datastax_rails/associations/builder/has_many.rb +39 -40
- data/lib/datastax_rails/associations/builder/has_one.rb +30 -31
- data/lib/datastax_rails/associations/builder/singular_association.rb +31 -33
- data/lib/datastax_rails/associations/collection_association.rb +129 -135
- data/lib/datastax_rails/associations/collection_proxy.rb +21 -21
- data/lib/datastax_rails/associations/has_and_belongs_to_many_association.rb +26 -26
- data/lib/datastax_rails/associations/has_many_association.rb +38 -38
- data/lib/datastax_rails/associations/has_one_association.rb +31 -32
- data/lib/datastax_rails/associations/singular_association.rb +31 -30
- data/lib/datastax_rails/associations.rb +27 -24
- data/lib/datastax_rails/attribute_assignment.rb +17 -17
- data/lib/datastax_rails/attribute_methods/definition.rb +4 -4
- data/lib/datastax_rails/attribute_methods/dirty.rb +34 -33
- data/lib/datastax_rails/attribute_methods/primary_key.rb +3 -8
- data/lib/datastax_rails/attribute_methods/read.rb +10 -12
- data/lib/datastax_rails/attribute_methods/typecasting.rb +36 -35
- data/lib/datastax_rails/attribute_methods/write.rb +5 -6
- data/lib/datastax_rails/attribute_methods.rb +52 -56
- data/lib/datastax_rails/base.rb +122 -125
- data/lib/datastax_rails/callbacks.rb +15 -9
- data/lib/datastax_rails/cassandra_only_model.rb +6 -6
- data/lib/datastax_rails/collection.rb +5 -7
- data/lib/datastax_rails/column.rb +130 -118
- data/lib/datastax_rails/connection/statement_cache.rb +3 -3
- data/lib/datastax_rails/connection.rb +42 -33
- data/lib/datastax_rails/cql/alter_column_family.rb +19 -21
- data/lib/datastax_rails/cql/base.rb +8 -11
- data/lib/datastax_rails/cql/column_family.rb +11 -10
- data/lib/datastax_rails/cql/consistency.rb +2 -2
- data/lib/datastax_rails/cql/create_column_family.rb +15 -15
- data/lib/datastax_rails/cql/create_index.rb +5 -5
- data/lib/datastax_rails/cql/create_keyspace.rb +7 -7
- data/lib/datastax_rails/cql/delete.rb +16 -29
- data/lib/datastax_rails/cql/drop_column_family.rb +2 -2
- data/lib/datastax_rails/cql/drop_index.rb +2 -2
- data/lib/datastax_rails/cql/drop_keyspace.rb +2 -2
- data/lib/datastax_rails/cql/insert.rb +10 -16
- data/lib/datastax_rails/cql/select.rb +21 -33
- data/lib/datastax_rails/cql/truncate.rb +2 -2
- data/lib/datastax_rails/cql/update.rb +16 -24
- data/lib/datastax_rails/cql/use_keyspace.rb +2 -2
- data/lib/datastax_rails/cql.rb +2 -2
- data/lib/datastax_rails/dynamic_model.rb +32 -29
- data/lib/datastax_rails/errors.rb +6 -6
- data/lib/datastax_rails/grouped_collection.rb +3 -3
- data/lib/datastax_rails/inheritance.rb +9 -9
- data/lib/datastax_rails/payload_model.rb +24 -20
- data/lib/datastax_rails/persistence.rb +116 -110
- data/lib/datastax_rails/railtie.rb +7 -7
- data/lib/datastax_rails/reflection.rb +61 -59
- data/lib/datastax_rails/relation/batches.rb +12 -13
- data/lib/datastax_rails/relation/facet_methods.rb +44 -33
- data/lib/datastax_rails/relation/finder_methods.rb +95 -91
- data/lib/datastax_rails/relation/modification_methods.rb +5 -5
- data/lib/datastax_rails/relation/search_methods.rb +102 -102
- data/lib/datastax_rails/relation/spawn_methods.rb +25 -24
- data/lib/datastax_rails/relation/stats_methods.rb +9 -8
- data/lib/datastax_rails/relation.rb +165 -170
- data/lib/datastax_rails/rsolr_client_wrapper.rb +3 -3
- data/lib/datastax_rails/schema/cassandra.rb +44 -43
- data/lib/datastax_rails/schema/migrator.rb +52 -52
- data/lib/datastax_rails/schema/solr.rb +55 -47
- data/lib/datastax_rails/schema_cache.rb +1 -3
- data/lib/datastax_rails/scoping/default.rb +2 -3
- data/lib/datastax_rails/scoping/named.rb +3 -5
- data/lib/datastax_rails/scoping.rb +11 -12
- data/lib/datastax_rails/serialization.rb +34 -31
- data/lib/datastax_rails/serializers/xml_serializer.rb +178 -175
- data/lib/datastax_rails/timestamps.rb +4 -4
- data/lib/datastax_rails/types/dirty_collection.rb +57 -57
- data/lib/datastax_rails/types/dynamic_list.rb +1 -1
- data/lib/datastax_rails/types/dynamic_map.rb +5 -7
- data/lib/datastax_rails/types/dynamic_set.rb +2 -2
- data/lib/datastax_rails/util/solr_repair.rb +3 -3
- data/lib/datastax_rails/validations/associated.rb +8 -6
- data/lib/datastax_rails/validations/uniqueness.rb +8 -8
- data/lib/datastax_rails/validations.rb +9 -10
- data/lib/datastax_rails/version.rb +2 -1
- data/lib/datastax_rails/wide_storage_model.rb +6 -6
- data/lib/datastax_rails.rb +13 -9
- data/lib/schema_migration.rb +3 -3
- data/spec/datastax_rails/associations/belongs_to_association_spec.rb +2 -2
- data/spec/datastax_rails/associations/collection_association_spec.rb +14 -14
- data/spec/datastax_rails/associations/has_many_association_spec.rb +20 -20
- data/spec/datastax_rails/associations_spec.rb +11 -11
- data/spec/datastax_rails/attribute_methods_spec.rb +25 -25
- data/spec/datastax_rails/base_spec.rb +24 -24
- data/spec/datastax_rails/callbacks_spec.rb +21 -21
- data/spec/datastax_rails/column_spec.rb +133 -132
- data/spec/datastax_rails/connection/statement_cache_spec.rb +2 -2
- data/spec/datastax_rails/cql/base_spec.rb +4 -4
- data/spec/datastax_rails/cql/delete_spec.rb +19 -0
- data/spec/datastax_rails/cql/select_spec.rb +8 -8
- data/spec/datastax_rails/cql/update_spec.rb +8 -10
- data/spec/datastax_rails/dynamic_model_spec.rb +36 -22
- data/spec/datastax_rails/inheritance_spec.rb +11 -14
- data/spec/datastax_rails/persistence_spec.rb +73 -74
- data/spec/datastax_rails/relation/batches_spec.rb +13 -13
- data/spec/datastax_rails/relation/facet_methods_spec.rb +43 -35
- data/spec/datastax_rails/relation/finder_methods_spec.rb +77 -78
- data/spec/datastax_rails/relation/modification_methods_spec.rb +19 -19
- data/spec/datastax_rails/relation/search_methods_spec.rb +160 -160
- data/spec/datastax_rails/relation/spawn_methods_spec.rb +18 -18
- data/spec/datastax_rails/relation_spec.rb +119 -116
- data/spec/datastax_rails/schema/migrator_spec.rb +30 -30
- data/spec/datastax_rails/schema/solr_spec.rb +15 -15
- data/spec/datastax_rails/scoping/default_spec.rb +9 -9
- data/spec/datastax_rails/types/dynamic_list_spec.rb +12 -12
- data/spec/datastax_rails/types/dynamic_map_spec.rb +10 -10
- data/spec/datastax_rails/types/dynamic_set_spec.rb +22 -10
- data/spec/datastax_rails/validations/uniqueness_spec.rb +25 -25
- data/spec/datastax_rails/wide_storage_model_spec.rb +11 -0
- data/spec/datastax_rails_spec.rb +2 -2
- data/spec/dummy/config/application.rb +2 -3
- data/spec/dummy/config/boot.rb +1 -1
- data/spec/dummy/config/environments/development.rb +3 -3
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/config/initializers/session_store.rb +1 -1
- data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/spec/factories/audit_logs.rb +6 -0
- data/spec/factories/hobbies.rb +6 -0
- data/spec/factories/people.rb +5 -0
- data/spec/feature/dynamic_fields_spec.rb +4 -4
- data/spec/feature/overloaded_tables_spec.rb +11 -12
- data/spec/spec_helper.rb +17 -14
- data/spec/support/datastax_test_hook.rb +2 -2
- data/spec/support/default_consistency_shared_examples.rb +11 -11
- data/spec/support/models.rb +31 -32
- metadata +40 -6
- data/lib/datastax_rails/attribute_methods/before_type_cast.rb +0 -71
- data/lib/datastax_rails/log_subscriber.rb +0 -0
- data/spec/dummy/ks/migrate/20111117224534_models.rb +0 -20
@@ -1,56 +1,57 @@
|
|
1
|
-
module DatastaxRails::Associations::Builder
|
1
|
+
module DatastaxRails::Associations::Builder # rubocop:disable Style/ClassAndModuleChildren
|
2
2
|
class Association #:nodoc:
|
3
3
|
class_attribute :valid_options
|
4
4
|
self.valid_options = [:class_name, :foreign_key]
|
5
|
-
|
5
|
+
|
6
6
|
# Set by subclasses
|
7
7
|
class_attribute :macro
|
8
|
-
|
8
|
+
|
9
9
|
attr_reader :model, :name, :options, :reflection
|
10
|
-
|
10
|
+
|
11
11
|
def self.build(model, name, options)
|
12
12
|
new(model, name, options).build
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def initialize(model, name, options)
|
16
16
|
@model, @name, @options = model, name, options
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def build
|
20
20
|
validate_options
|
21
21
|
reflection = model.create_reflection(self.class.macro, name, options, model)
|
22
22
|
define_accessors
|
23
23
|
reflection
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def mixin
|
27
27
|
@model.generated_attribute_methods
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
private
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
31
|
+
|
32
|
+
def validate_options
|
33
|
+
options.assert_valid_keys(self.class.valid_options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def define_accessors
|
37
|
+
define_readers
|
38
|
+
define_writers
|
39
|
+
end
|
40
|
+
|
41
|
+
def define_readers
|
42
|
+
name = self.name
|
43
|
+
|
44
|
+
model.redefine_method(name) do |*params|
|
45
|
+
association(name).reader(*params)
|
46
46
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
end
|
48
|
+
|
49
|
+
def define_writers
|
50
|
+
name = self.name
|
51
|
+
|
52
|
+
model.redefine_method("#{name}=") do |value|
|
53
|
+
association(name).writer(value)
|
54
54
|
end
|
55
|
+
end
|
55
56
|
end
|
56
|
-
end
|
57
|
+
end
|
@@ -1,30 +1,29 @@
|
|
1
|
-
module DatastaxRails::Associations::Builder
|
1
|
+
module DatastaxRails::Associations::Builder # rubocop:disable Style/ClassAndModuleChildren
|
2
2
|
class BelongsTo < SingularAssociation
|
3
3
|
self.macro = :belongs_to
|
4
|
-
|
4
|
+
|
5
5
|
def build
|
6
6
|
reflection = super
|
7
7
|
configure_dependency
|
8
8
|
reflection
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
private
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
|
20
|
-
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
|
21
|
-
def #{method_name}
|
22
|
-
association = #{name}
|
23
|
-
association.#{options[:dependent]} if association
|
24
|
-
end
|
25
|
-
eoruby
|
26
|
-
model.after_destroy method_name
|
27
|
-
end
|
12
|
+
|
13
|
+
def configure_dependency
|
14
|
+
return unless options[:dependent]
|
15
|
+
unless options[:dependent].in?([:destroy, :delete])
|
16
|
+
fail ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
|
28
17
|
end
|
18
|
+
|
19
|
+
method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
|
20
|
+
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
|
21
|
+
def #{method_name}
|
22
|
+
association = #{name}
|
23
|
+
association.#{options[:dependent]} if association
|
24
|
+
end
|
25
|
+
eoruby
|
26
|
+
model.after_destroy method_name
|
27
|
+
end
|
29
28
|
end
|
30
|
-
end
|
29
|
+
end
|
@@ -1,48 +1,48 @@
|
|
1
|
-
module DatastaxRails::Associations::Builder
|
1
|
+
module DatastaxRails::Associations::Builder # rubocop:disable Style/ClassAndModuleChildren
|
2
2
|
class CollectionAssociation < Association #:nodoc:
|
3
3
|
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
|
4
|
-
|
4
|
+
|
5
5
|
self.valid_options += [:columm_family, :order, :uniq, :before_add, :before_remove, :after_add, :after_remove]
|
6
|
-
|
6
|
+
|
7
7
|
def self.build(model, name, options)
|
8
8
|
new(model, name, options).build
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def build
|
12
|
-
reflection = super
|
12
|
+
@reflection = super
|
13
13
|
CALLBACKS.each { |callback_name| define_callback(callback_name) }
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def writable?
|
17
17
|
true
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
protected
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
21
|
+
|
22
|
+
def define_callback(callback_name)
|
23
|
+
full_callback_name = "#{callback_name}_for_#{name}"
|
24
|
+
|
25
|
+
# XXX : why do i need method_defined? I think its because of the inheritance chain
|
26
|
+
model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name)
|
27
|
+
model.send("#{full_callback_name}=", Array.wrap(options[callback_name.to_sym]))
|
28
|
+
end
|
29
|
+
|
30
|
+
def define_readers
|
31
|
+
super
|
32
|
+
|
33
|
+
name = self.name
|
34
|
+
mixin.redefine_method("#{name.to_s.singularize}_ids") do
|
35
|
+
association(name).ids_reader
|
37
36
|
end
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
end
|
38
|
+
|
39
|
+
def define_writers
|
40
|
+
super
|
41
|
+
|
42
|
+
name = self.name
|
43
|
+
mixin.redefine_method("#{name.to_s.singularize}_ids=") do |ids|
|
44
|
+
association(name).ids_writer(ids)
|
46
45
|
end
|
46
|
+
end
|
47
47
|
end
|
48
|
-
end
|
48
|
+
end
|
@@ -1,36 +1,36 @@
|
|
1
|
-
module DatastaxRails::Associations::Builder
|
1
|
+
module DatastaxRails::Associations::Builder # rubocop:disable Style/ClassAndModuleChildren
|
2
2
|
class HasAndBelongsToMany < CollectionAssociation #:nodoc:
|
3
3
|
self.macro = :has_and_belongs_to_many
|
4
|
-
|
4
|
+
|
5
5
|
def build
|
6
6
|
reflection = super
|
7
7
|
define_destroy_hook
|
8
8
|
check_for_join_column_family
|
9
9
|
reflection
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
private
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
|
14
|
+
def define_destroy_hook
|
15
|
+
name = self.name
|
16
|
+
model.send(:include, Module.new do
|
17
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
17
18
|
def destroy_associations
|
18
19
|
association(#{name.to_sym.inspect}).delete_all_on_destroy
|
19
20
|
super
|
20
21
|
end
|
21
22
|
RUBY
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
23
|
+
end)
|
24
|
+
end
|
25
|
+
|
26
|
+
def check_for_join_column_family
|
27
|
+
return if DatastaxRails::Base.connection.column_families.key?('many_to_many_joins')
|
28
|
+
cf = Cassandra::ColumnFamily.new
|
29
|
+
cf.name = 'many_to_many_joins'
|
30
|
+
cf.keyspace = DatastaxRails::Base.connection.keyspace
|
31
|
+
cf.comparator_type = 'BytesType'
|
32
|
+
cf.column_type = 'Standard'
|
33
|
+
DatastaxRails::Base.connection.add_column_family(cf)
|
34
|
+
end
|
35
35
|
end
|
36
|
-
end
|
36
|
+
end
|
@@ -1,54 +1,53 @@
|
|
1
|
-
module DatastaxRails::Associations::Builder
|
1
|
+
module DatastaxRails::Associations::Builder # rubocop:disable Style/ClassAndModuleChildren
|
2
2
|
class HasMany < CollectionAssociation #:nodoc:
|
3
3
|
self.macro = :has_many
|
4
|
-
|
4
|
+
|
5
5
|
self.valid_options += [:primary_key, :dependent, :source_type]
|
6
|
-
|
6
|
+
|
7
7
|
def build
|
8
8
|
reflection = super
|
9
9
|
configure_dependency
|
10
10
|
reflection
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
private
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
send("define_#{options[:dependent]}_dependency_method")
|
22
|
-
model.before_destroy dependency_method_name
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def define_destroy_dependency_method
|
27
|
-
name = self.name
|
28
|
-
mixin.redefine_method(dependency_method_name) do
|
29
|
-
send(name).each do |o|
|
30
|
-
# No point in executing the counter update since we're going to destroy the parent anyway
|
31
|
-
counter_method = ('belongs_to_counter_cache_before_destroy_for_' + self.class.name.downcase).to_sym
|
32
|
-
if o.respond_to?(counter_method)
|
33
|
-
class << o
|
34
|
-
self
|
35
|
-
end.send(:define_method, counter_method, Proc.new {})
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
send(name).destroy_all
|
40
|
-
end
|
14
|
+
|
15
|
+
def configure_dependency
|
16
|
+
return unless options[:dependent]
|
17
|
+
unless options[:dependent].in?([:destroy]) # Only destroy and restrict supported for now
|
18
|
+
fail ArgumentError,
|
19
|
+
"The :dependent option only handles :destroy or :restrict for now (#{options[:dependent].inspect})"
|
41
20
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
21
|
+
|
22
|
+
send("define_#{options[:dependent]}_dependency_method")
|
23
|
+
model.before_destroy dependency_method_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def define_destroy_dependency_method
|
27
|
+
name = self.name
|
28
|
+
mixin.redefine_method(dependency_method_name) do
|
29
|
+
send(name).each do |o|
|
30
|
+
# No point in executing the counter update since we're going to destroy the parent anyway
|
31
|
+
counter_method = ('belongs_to_counter_cache_before_destroy_for_' + self.class.name.downcase).to_sym
|
32
|
+
next unless o.respond_to?(counter_method)
|
33
|
+
class << o
|
34
|
+
self
|
35
|
+
end.send(:define_method, counter_method, proc {})
|
47
36
|
end
|
37
|
+
|
38
|
+
send(name).destroy_all
|
48
39
|
end
|
49
|
-
|
50
|
-
|
51
|
-
|
40
|
+
end
|
41
|
+
|
42
|
+
def define_restrict_dependency_method
|
43
|
+
name = self.name
|
44
|
+
mixin.redefine_method(dependency_method_name) do
|
45
|
+
fail DatastaxRails::DeleteRestrictionError.new(name) unless send(name).empty?
|
52
46
|
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def dependency_method_name
|
50
|
+
"has_many_dependent_for_#{name}"
|
51
|
+
end
|
53
52
|
end
|
54
|
-
end
|
53
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module DatastaxRails::Associations::Builder
|
1
|
+
module DatastaxRails::Associations::Builder # rubocop:disable Style/ClassAndModuleChildren
|
2
2
|
class HasOne < SingularAssociation #:nodoc:
|
3
3
|
self.macro = :has_one
|
4
4
|
|
@@ -10,43 +10,42 @@ module DatastaxRails::Associations::Builder
|
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def configure_dependency
|
20
|
-
if options[:dependent]
|
21
|
-
unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
|
22
|
-
raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \
|
23
|
-
":nullify or :restrict (#{options[:dependent].inspect})"
|
24
|
-
end
|
13
|
+
def validate_options
|
14
|
+
valid_options = self.class.valid_options
|
15
|
+
valid_options += self.class.through_options if options[:through]
|
16
|
+
options.assert_valid_keys(valid_options)
|
17
|
+
end
|
25
18
|
|
26
|
-
|
27
|
-
|
28
|
-
|
19
|
+
def configure_dependency
|
20
|
+
return unless options[:dependent]
|
21
|
+
unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
|
22
|
+
fail ArgumentError, 'The :dependent option expects either :destroy, :delete, ' \
|
23
|
+
":nullify or :restrict (#{options[:dependent].inspect})"
|
29
24
|
end
|
30
25
|
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
send("define_#{options[:dependent]}_dependency_method")
|
27
|
+
model.before_destroy dependency_method_name
|
28
|
+
end
|
34
29
|
|
35
|
-
|
36
|
-
|
30
|
+
def dependency_method_name
|
31
|
+
"has_one_dependent_#{options[:dependent]}_for_#{name}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_destroy_dependency_method
|
35
|
+
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
|
37
36
|
def #{dependency_method_name}
|
38
37
|
association(#{name.to_sym.inspect}).delete
|
39
38
|
end
|
40
39
|
eoruby
|
40
|
+
end
|
41
|
+
alias_method :define_delete_dependency_method, :define_destroy_dependency_method
|
42
|
+
alias_method :define_nullify_dependency_method, :define_destroy_dependency_method
|
43
|
+
|
44
|
+
def define_restrict_dependency_method
|
45
|
+
name = self.name
|
46
|
+
model.redefine_method(dependency_method_name) do
|
47
|
+
fail DatastaxRails::DeleteRestrictionError.new(name) unless send(name).nil?
|
41
48
|
end
|
42
|
-
|
43
|
-
alias :define_nullify_dependency_method :define_destroy_dependency_method
|
44
|
-
|
45
|
-
def define_restrict_dependency_method
|
46
|
-
name = self.name
|
47
|
-
model.redefine_method(dependency_method_name) do
|
48
|
-
raise DatastaxRails::DeleteRestrictionError.new(name) unless send(name).nil?
|
49
|
-
end
|
50
|
-
end
|
49
|
+
end
|
51
50
|
end
|
52
|
-
end
|
51
|
+
end
|