3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/association_accessors/singular_association.rb', line 3
def self.define_association_methods mixin, reflection, attribute
association = reflection.name
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{association}_#{attribute}
#{association}&.#{attribute}
end
def #{association}_#{attribute}= value
association_class = association(#{association.inspect}).klass
send :#{association}=, value && association_class.find_by!(#{attribute}: value)
value
end
CODE
end
|