Module: DependentRestrict::ClassMethods

Defined in:
lib/dependent_restrict.rb

Constant Summary collapse

VALID_DEPENDENTS =
[:rollback, :restrict_with_error, :restrict, :restrict_with_exception]

Instance Method Summary collapse

Instance Method Details

#has_and_belongs_to_many(*args, &extension) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dependent_restrict.rb', line 51

def has_and_belongs_to_many(*args, &extension)
  options = args.extract_options! || {}
  if VALID_DEPENDENTS.include?(options[:dependent].try(:to_sym))
    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
      association_id, scope = *args
      restrict_create_reflection(:has_and_belongs_to_many, association_id, scope || {}, options, self)
    else
      association_id = args.first
      create_reflection(:has_and_belongs_to_many, association_id, options, self)
    end
    add_dependency_callback!(reflection, options)
    options.delete(:dependent)
  end
  args << options
  super(*args, &extension)
end

#has_many(*args, &extension) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dependent_restrict.rb', line 35

def has_many(*args, &extension)
  options = args.extract_options! || {}
  if VALID_DEPENDENTS.include?(options[:dependent].try(:to_sym))
    reflection = if active_record_4?
      association_id, scope = *args
      restrict_create_reflection(:has_many, association_id, scope || {}, options, self)
    else
      association_id = args.first
      create_reflection(:has_many, association_id, options, self)
    end
    add_dependency_callback!(reflection, options)
  end
  args << options
  super(*args, &extension)
end

#has_one(*args, &extension) ⇒ 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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dependent_restrict.rb', line 19

def has_one(*args, &extension)
  options = args.extract_options! || {}
  if VALID_DEPENDENTS.include?(options[:dependent].try(:to_sym))
    reflection = if active_record_4?
      association_id, scope = *args
      restrict_create_reflection(:has_one, association_id, scope || {}, options, self)
    else
      association_id = args[0]
      create_reflection(:has_one, association_id, options, self)
    end
    add_dependency_callback!(reflection, options)
  end
  args << options
  super(*args, &extension)
end