Module: Slices::HasAttachments::ClassMethods

Defined in:
lib/slices/has_attachments.rb

Instance Method Summary collapse

Instance Method Details

#attachment_fieldsObject



36
37
38
39
40
41
42
# File 'lib/slices/has_attachments.rb', line 36

def attachment_fields
  @attachment_fields ||= if superclass.respond_to?(:attachment_fields)
                           superclass.attachment_fields.dup
                         else
                           []
                         end
end

#has_attachments(embed_name = :attachments, options = {}) ⇒ Object



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