7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/slices/has_attachments.rb', line 7
def has_attachments(embed_name = :attachments, options = {})
klass = if options.has_key?(:class_name)
options[:class_name].constantize
else
Attachment
end
default = options[:default] || options[:singular] ? nil : []
type = options[:singular] ? Hash : Array
if options[:singular]
define_method embed_name do
if embed = read_attribute(embed_name)
klass.new embed
end
end
else
define_method embed_name do
(read_attribute(embed_name) || []).collect do |embed|
klass.new embed
end
end
end
attachment_fields << embed_name
field embed_name, type: type, default: default
end
|