Module: DependentRestrict::ClassMethods

Defined in:
lib/dependent_restrict.rb

Instance Method Summary collapse

Instance Method Details

#has_and_belongs_to_many_with_restrict(association_id, options = {}, &extension) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dependent_restrict.rb', line 46

def has_and_belongs_to_many_with_restrict(association_id, options = {}, &extension)
  reflection = if active_record_4?
    raise ArgumentError, "dependent_restrict doesn't work with has_and_belongs_to_many. Use equivalent rails 4.1 has_many :through" if ActiveRecord::Reflection.respond_to? :create
    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

#has_many_with_restrict(association_id, options = {}, &extension) ⇒ Object

:nodoc:



36
37
38
39
40
41
42
43
44
# File 'lib/dependent_restrict.rb', line 36

def has_many_with_restrict(association_id, options = {}, &extension) #:nodoc:
  reflection = if active_record_4?
    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

#has_one_with_restrict(*args) ⇒ Object

We should be aliasing configure_dependency_for_has_many but that method is private so we can’t. We alias has_many instead trying to be as fair as we can to the original behaviour.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dependent_restrict.rb', line 24

def has_one_with_restrict(*args) #:nodoc:
  reflection = if active_record_4?
    association_id, options, scope, extension = *args
    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