lib/dependent_restrict.rb in dependent_restrict-0.1.1 vs lib/dependent_restrict.rb in dependent_restrict-0.2.0

- old
+ new

@@ -1,10 +1,10 @@ require 'active_record' require 'dependent_restrict/delete_restriction_error' module DependentRestrict - VERSION = '0.1.1' + VERSION = '0.2.0' def self.included(base) super base.extend(ClassMethods) @@ -22,31 +22,35 @@ # is private so we can't. We alias has_many instead trying to be as fair # as we can to the original behaviour. def has_one_with_restrict(*args) #:nodoc: reflection = if active_record_4? association_id, options, scope, extension = *args - create_reflection(:has_one, association_id, options || {}, scope || {}, self) + restrict_create_reflection(:has_one, association_id, options || {}, scope || {}, self) else association_id, options, extension = *args create_reflection(:has_one, association_id, options || {}, self) end add_dependency_callback!(reflection, options || {}) has_one_without_restrict(*args) #association_id, options, &extension) end def has_many_with_restrict(association_id, options = {}, &extension) #:nodoc: reflection = if active_record_4? - create_reflection(:has_many, association_id, options, scope ||= {}, self) + restrict_create_reflection(:has_many, association_id, options, scope ||= {}, self) else create_reflection(:has_many, association_id, options, self) end add_dependency_callback!(reflection, options) has_many_without_restrict(association_id, options, &extension) end def has_and_belongs_to_many_with_restrict(association_id, options = {}, &extension) - reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self) + reflection = if active_record_4? + restrict_create_reflection(:has_and_belongs_to_many, association_id, options, scope ||= {}, self) + else + create_reflection(:has_and_belongs_to_many, association_id, options, self) + end add_dependency_callback!(reflection, options) options.delete(:dependent) has_and_belongs_to_many_without_restrict(association_id, options, &extension) end @@ -77,9 +81,17 @@ end end def active_record_4? ::ActiveRecord::VERSION::MAJOR == 4 + end + + def restrict_create_reflection(*args) + if ActiveRecord::Reflection.respond_to? :create + ActiveRecord::Reflection.create *args + else + create_reflection(*args) + end end end end