Skip to content
This repository
Newer
Older
100644 62 lines (50 sloc) 1.884 kb
a9f3c9da » sikachu
2011-04-11 Using Object#in? and Object#either? in various places
1 require 'active_support/core_ext/object/inclusion'
2
52f8e4b9 » jonleighton
2011-02-21 Use proper objects to do the work to build the associations (adding m…
3 module ActiveRecord::Associations::Builder
4 class HasOne < SingularAssociation #:nodoc:
5 self.macro = :has_one
6
7 self.valid_options += [:order, :as]
8
9 class_attribute :through_options
10 self.through_options = [:through, :source, :source_type]
11
12 def constructable?
13 !options[:through]
14 end
15
16 def build
17 reflection = super
18 configure_dependency unless options[:through]
19 reflection
20 end
21
22 private
23
24 def validate_options
25 valid_options = self.class.valid_options
26 valid_options += self.class.through_options if options[:through]
27 options.assert_valid_keys(valid_options)
28 end
29
30 def configure_dependency
31 if options[:dependent]
733bfa63 » sikachu
2011-04-13 Remove `#among?` from Active Support
32 unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
52f8e4b9 » jonleighton
2011-02-21 Use proper objects to do the work to build the associations (adding m…
33 raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \
34 ":nullify or :restrict (#{options[:dependent].inspect})"
35 end
36
37 send("define_#{options[:dependent]}_dependency_method")
38 model.before_destroy dependency_method_name
39 end
40 end
41
42 def dependency_method_name
43 "has_one_dependent_#{options[:dependent]}_for_#{name}"
44 end
45
46 def define_destroy_dependency_method
7cba6a37 » joshsusser
2011-11-14 association methods are now generated in modules
47 name = self.name
124c97fb » joshsusser
2011-11-27 avoid warnings
48 mixin.redefine_method(dependency_method_name) do
7cba6a37 » joshsusser
2011-11-14 association methods are now generated in modules
49 association(name).delete
50 end
52f8e4b9 » jonleighton
2011-02-21 Use proper objects to do the work to build the associations (adding m…
51 end
52 alias :define_delete_dependency_method :define_destroy_dependency_method
53 alias :define_nullify_dependency_method :define_destroy_dependency_method
54
55 def define_restrict_dependency_method
56 name = self.name
124c97fb » joshsusser
2011-11-27 avoid warnings
57 mixin.redefine_method(dependency_method_name) do
52f8e4b9 » jonleighton
2011-02-21 Use proper objects to do the work to build the associations (adding m…
58 raise ActiveRecord::DeleteRestrictionError.new(name) unless send(name).nil?
59 end
60 end
61 end
62 end
Something went wrong with that request. Please try again.