Class: Uploader::FileuploadGlue

Inherits:
Object
  • Object
show all
Defined in:
lib/uploader/fileupload_glue.rb

Constant Summary collapse

TARGET_TYPE =
'_type'
TARGET_ID =
'_id'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ FileuploadGlue

Returns a new instance of FileuploadGlue.



13
14
15
16
17
18
# File 'lib/uploader/fileupload_glue.rb', line 13

def initialize(record)
  @record = record
  @record_klass = record.class

  @associations = {}
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'lib/uploader/fileupload_glue.rb', line 5

def record
  @record
end

#record_klassObject (readonly)

Returns the value of attribute record_klass.



5
6
7
# File 'lib/uploader/fileupload_glue.rb', line 5

def record_klass
  @record_klass
end

Instance Method Details

#asset(method_name) ⇒ Object



39
40
41
42
43
# File 'lib/uploader/fileupload_glue.rb', line 39

def asset(method_name)
  return unless available_fileuploads.include?(method_name.to_sym)

  find_asset_by_fileupload_guid(method_name, fileupload_guid) || build_asset(method_name)
end

#association(method_name) ⇒ Object



45
46
47
48
# File 'lib/uploader/fileupload_glue.rb', line 45

def association(method_name)
  name = method_name.to_sym
  @associations[name] ||= reflect_on_association(name)
end

#join!Object



20
21
22
23
24
25
26
27
28
# File 'lib/uploader/fileupload_glue.rb', line 20

def join!
  available_fileuploads.each do |method_name|
    target_name = target_column_name(method_name).to_s + TARGET_ID
    guid_name = guid_column_name(method_name)

    scope_by_fileupload_guid(method_name, fileupload_guid).update_all(target_name => @record.id,
                                                                      guid_name => nil)
  end
end

#klass(method_name) ⇒ Object

Find class by reflection



51
52
53
54
55
# File 'lib/uploader/fileupload_glue.rb', line 51

def klass(method_name)
  return if association(method_name).nil?

  association(method_name).klass
end

#multiple?(method_name) ⇒ Boolean

many? for Mongoid and collection? for AR

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/uploader/fileupload_glue.rb', line 58

def multiple?(method_name)
  return false if association(method_name).nil?

  name = association(method_name).respond_to?(:many?) ? :many? : :collection?
  association(method_name).send(name)
end

#params(method_name) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/uploader/fileupload_glue.rb', line 30

def params(method_name)
  {
    guid: fileupload_guid,
    assetable_type: record_klass_type,
    assetable_id: @record.id,
    klass: klass(method_name)
  }
end