Class: Uploader::FileuploadGlue

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

Constant Summary collapse

TARGET_TYPE =
'_type'.freeze
TARGET_ID =
'_id'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ FileuploadGlue

Returns a new instance of FileuploadGlue.



11
12
13
14
15
16
# File 'lib/uploader/fileupload_glue.rb', line 11

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

  @associations = {}
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/uploader/fileupload_glue.rb', line 3

def record
  @record
end

#record_klassObject (readonly)

Returns the value of attribute record_klass.



3
4
5
# File 'lib/uploader/fileupload_glue.rb', line 3

def record_klass
  @record_klass
end

Instance Method Details

#asset(method_name) ⇒ Object



37
38
39
40
# File 'lib/uploader/fileupload_glue.rb', line 37

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



42
43
44
45
# File 'lib/uploader/fileupload_glue.rb', line 42

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

#join!Object



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

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



48
49
50
51
# File 'lib/uploader/fileupload_glue.rb', line 48

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)


54
55
56
57
58
59
# File 'lib/uploader/fileupload_glue.rb', line 54

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



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

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