Module: PhotoSyncable

Defined in:
lib/generators/harmonia/templates/concerns/photo_syncable.rb

Instance Method Summary collapse

Instance Method Details

#sync_photo_to_filemaker(attachment, fm_record, options: {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/generators/harmonia/templates/concerns/photo_syncable.rb', line 20

def sync_photo_to_filemaker(attachment, fm_record, options: {})
  container_field = options[:container_field] || 'foto'
  return unless attachment.blank?

  attachment.open do |tempfile|
    fm_record.upload(container_name: container_field, file: tempfile)
  end
end

#sync_photos(pg_records, fm_records, options: {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/generators/harmonia/templates/concerns/photo_syncable.rb', line 4

def sync_photos(pg_records, fm_records, options: {})
  attachment     = options[:attachment]      || 'photo'
  container_field = options[:container_field] || 'foto'
  filename_field  = options[:filename_field]  || 'foto_filename'
  purge_existing  = options[:purge_existing]

  pg_records.each do |record|
    fm_record = fm_records.find { |r| r.id == record.filemaker_id }
    record.send(attachment).purge if record.send(attachment).attached? && purge_existing
    next unless fm_record.send(container_field).present?

    downloaded_image = URI.parse(fm_record.send(container_field)).open
    record.send(attachment).attach(io: downloaded_image, filename: fm_record.send(filename_field))
  end
end