Module: HasAttachable::Processing

Extended by:
ActiveSupport::Concern
Defined in:
lib/has_attachable/processing.rb

Instance Method Summary collapse

Instance Method Details

#attachable_fieldObject



36
37
38
# File 'lib/has_attachable/processing.rb', line 36

def attachable_field
  get_changed_attachable || get_remove_attachable
end

#get_changed_attachableObject



49
50
51
52
53
54
55
# File 'lib/has_attachable/processing.rb', line 49

def get_changed_attachable
  changed_attachable = nil
  attachable_fields.each do |field|
    changed_attachable = field if changes.has_key?("#{field}_name")
  end
  changed_attachable
end

#get_remove_attachableObject



57
58
59
60
61
62
63
# File 'lib/has_attachable/processing.rb', line 57

def get_remove_attachable
  remove_attachable = nil
  attachable_fields.each do |field|
    remove_attachable = field if send("async_remove_#{field}")
  end
  remove_attachable
end

#process_attachable?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/has_attachable/processing.rb', line 5

def process_attachable?
  field = get_changed_attachable
  return false if field.nil?

  case attachable_options[field][:type]
  when :downloadable, :streamable then false
  else
    send("#{field}_name_changed?") and \
    send("#{field}_name").present? ? true : false
  end
end

#processing_optionsObject



66
67
68
69
70
71
72
# File 'lib/has_attachable/processing.rb', line 66

def processing_options
  {
    klass: self.class.name, 
    id: id, 
    context: get_changed_attachable || get_remove_attachable
  }
end

#remove_attachable?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/has_attachable/processing.rb', line 41

def remove_attachable?
  field = get_remove_attachable
  return false if field.nil? 

  send("async_remove_#{field}") and \
  send("#{field}_name").present? ? true : false
end

#run_process_attachableObject



17
18
19
20
# File 'lib/has_attachable/processing.rb', line 17

def run_process_attachable
  job_id = HasAttachable::Worker.perform_async('process', processing_options)
  track_job_id(job_id) if self.respond_to?("#{attachable_field}_job_id")
end

#run_remove_attachableObject



22
23
24
25
26
# File 'lib/has_attachable/processing.rb', line 22

def run_remove_attachable
  self.update_attribute("#{get_remove_attachable}_name", nil)
  job_id = HasAttachable::Worker.perform_async('remove', processing_options)
  track_job_id(job_id) if self.respond_to?("#{attachable_field}_job_id")
end

#track_job_id(job_id) ⇒ Object



28
29
30
# File 'lib/has_attachable/processing.rb', line 28

def track_job_id(job_id)
  self.update_column "#{attachable_field}_job_id", job_id
end

#untrack_job_id(context) ⇒ Object



32
33
34
# File 'lib/has_attachable/processing.rb', line 32

def untrack_job_id(context)
  self.update_column "#{context}_job_id", nil
end