Module: Attachinary::Extension

Includes:
Base
Defined in:
lib/attachinary/orm/base_extension.rb,
lib/attachinary/orm/mongoid/extension.rb,
lib/attachinary/orm/active_record/extension.rb

Defined Under Namespace

Modules: Base

Instance Method Summary collapse

Methods included from Base

#has_attachment, #has_attachments

Instance Method Details

#attachinary_orm_definition(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
35
36
37
38
39
40
41
# File 'lib/attachinary/orm/mongoid/extension.rb', line 7

def attachinary_orm_definition(options)
  if options[:single]
    # embeds_on :photo, ...
    embeds_one :"#{options[:scope]}",
      as: :attachinariable,
      class_name: '::Attachinary::File',
      cascade_callbacks: true
  else
    # embeds_many :images, ...
    embeds_many :"#{options[:scope]}",
      as: :attachinariable,
      class_name: '::Attachinary::File',
      cascade_callbacks: true
  end

  # alias_method "orig_photo=", "photo="
  # def photo=(input)
  #   input = Attachinary::Utils.process_input(input)
  #   if input.nil?
  #     super(nil)
  #   else
  #     files = [input].flatten
  #     super(options[:single] ? files[0] : files)
  #   end
  # end
  alias_method "orig_#{options[:scope]}=", "#{options[:scope]}="
  define_method "#{options[:scope]}=" do |input|
    input = Attachinary::Utils.process_input(input)
    if !input.nil?
      input = [input].flatten
      input = (options[:single] ? input[0] : input)
    end
    send("orig_#{options[:scope]}=", input)
  end
end