Class: NestedRecord::Methods::One
Instance Method Summary
collapse
#attributes_writer_method_name, #define, #define_attributes_writer_method, #reader_method_name, #rewrite_attributes_method_name, #upsert_attributes_method_name, #writer_method_name
Constructor Details
#initialize(setup) ⇒ One
Returns a new instance of One.
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/nested_record/methods/one.rb', line 3
def initialize(setup)
super
define :writer
define :build
alias_method rewrite_attributes_method_name, build_method_name
define :bang
define :upsert_attributes
define :validation
define_attributes_writer_method
end
|
Instance Method Details
#bang_method_body ⇒ Object
41
42
43
44
45
|
# File 'lib/nested_record/methods/one.rb', line 41
def bang_method_body
<<~RUBY
#{@setup.name} || #{build_method_name}
RUBY
end
|
#bang_method_name ⇒ Object
37
38
39
|
# File 'lib/nested_record/methods/one.rb', line 37
def bang_method_name
:"#{@setup.name}!"
end
|
#build_method_body ⇒ Object
28
29
30
31
32
33
34
35
|
# File 'lib/nested_record/methods/one.rb', line 28
def build_method_body
setup = @setup
writer_method_name = self.writer_method_name
proc do |attributes = {}|
record = setup.record_class.new(attributes)
public_send(writer_method_name, record)
end
end
|
#build_method_name ⇒ Object
24
25
26
|
# File 'lib/nested_record/methods/one.rb', line 24
def build_method_name
:"build_#{@setup.name}"
end
|
#upsert_attributes_method_body ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/nested_record/methods/one.rb', line 47
def upsert_attributes_method_body
build_method_name = self.build_method_name
name = @setup.name
proc do |attributes|
if (record = public_send(name))
record.assign_attributes(attributes)
else
public_send(build_method_name, attributes)
end
end
end
|
#validation_method_body ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/nested_record/methods/one.rb', line 63
def validation_method_body
name = @setup.name
if ActiveModel::VERSION::MAJOR < 6 || (ActiveModel::VERSION::MAJOR == 6 && ActiveModel::VERSION::MINOR < 1)
proc do
record = public_send(name)
return true unless record
return true if record.valid?
record.errors.each do |attribute, message|
error_attribute = "#{name}.#{attribute}"
errors[error_attribute] << message
errors[error_attribute].uniq!
end
record.errors.details.each_key do |attribute|
error_attribute = "#{name}.#{attribute}"
record.errors.details[attribute].each do |error|
errors.details[error_attribute] << error
errors.details[error_attribute].uniq!
end
end
false
end
else
proc do
record = public_send(name)
return true unless record
return true if record.valid?
record.errors.group_by_attribute.each do |attribute, errors|
error_attribute = "#{name}.#{attribute}"
errors.each do |error|
self.errors.import(error, attribute: error_attribute)
end
end
false
end
end
end
|
#validation_method_name ⇒ Object
59
60
61
|
# File 'lib/nested_record/methods/one.rb', line 59
def validation_method_name
:"validate_associated_record_for_#{@setup.name}"
end
|
#writer_method_body ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/nested_record/methods/one.rb', line 14
def writer_method_body
setup = @setup
proc do |record|
unless record.nil? || record.kind_of?(setup.record_class)
raise NestedRecord::TypeMismatchError, "#{record.inspect} should be a #{setup.record_class}"
end
super(record)
end
end
|