module MorseAssetable::ModelHelpers

Private Instance Methods

active_asset_column_names() click to toggle source
# File lib/morse_assetable.rb, line 72
def active_asset_column_names
  self.class.active_asset_column_names
end
add_asset_data(a) click to toggle source
# File lib/morse_assetable.rb, line 76
def add_asset_data(a)
  return unless active_asset_column_names.any?
  active_asset_column_names.each do |col|
    add_asset_field a, col
  end
end
add_asset_field(a, col) click to toggle source
# File lib/morse_assetable.rb, line 83
def add_asset_field(a, col)
  n = "#{name}_attachment_#{col}".to_sym
  return unless send(n).present?
  a.send("#{col}=", send(n))
end
attachment_name(name) click to toggle source
# File lib/morse_assetable.rb, line 89
def attachment_name(name)
  "#{name}_attachment".to_sym
end
attachment_name_remove(name) click to toggle source
# File lib/morse_assetable.rb, line 93
def attachment_name_remove(name)
  "#{name}_attachment_remove".to_sym
end
attachment_names() click to toggle source
# File lib/morse_assetable.rb, line 97
def attachment_names
  self.class.attachment_names
end
multiple_attachment_names() click to toggle source
# File lib/morse_assetable.rb, line 101
def multiple_attachment_names
  self.class.multiple_attachment_names
end
process_attachment(name) click to toggle source
# File lib/morse_assetable.rb, line 105
def process_attachment(name)
  n = attachment_name(name)
  a = Asset.new(attachment: send(n), assetable: self)
  add_asset_data(a)
  if a.save
    send("#{name}=", a)
    send("#{n}=", nil)
  else
    errors.add(name, a.errors.full_messages.join(','))
    false
  end
end
process_attachment?(name) click to toggle source
# File lib/morse_assetable.rb, line 118
def process_attachment?(name)
  n = attachment_name(name)
  respond_to?(n) && send(n).present?
end
process_attachment_name(name) click to toggle source
# File lib/morse_assetable.rb, line 123
def process_attachment_name(name)
  process_attachment(name) if process_attachment?(name)
  remove_attachment(name) if remove_attachment?(name)
end
process_attachments() click to toggle source
# File lib/morse_assetable.rb, line 128
def process_attachments
  return if attachment_names.empty?
  attachment_names.map { |n| process_attachment_name n }
end
process_multiple_attachments() click to toggle source

rubocop:disable all

# File lib/morse_assetable.rb, line 149
def process_multiple_attachments
  return if multiple_attachment_names.empty?
  multiple_attachment_names.each do |thing|
    at = "#{thing.to_s.singularize}_attachment".to_sym
    alt = "#{thing.to_s.singularize}_attachment_alt".to_sym
    att = "#{thing.to_s.singularize}_attachment_title".to_sym
    atr = "#{thing.to_s.singularize}_attachment_remove".to_sym
    atu = "#{thing.to_s.singularize}_attachment_url".to_sym
    if respond_to?(thing) && respond_to?(at) && send(at).present?
      a = Asset.new(attachment: send(at), title: send(att), alt: send(alt), assetable_type: self.class.name, assetable_id: id, url: send(atu))
      if a.alt.present? && a.title.blank?
        a.title = a.alt
      elsif a.alt.blank? && a.title.present?
        a.alt = a.title
      end
      if a.save
        send("#{at}=", nil)
        send("#{alt}=", nil)
        send("#{att}=", nil)
        send("#{atr}=", nil)
        send("#{atu}=", nil)
        send("#{thing.to_s.pluralize.to_sym}=", send("#{thing.to_s.pluralize.to_sym}") + [a])
      else
        errors.add(thing, a.errors.full_messages.join(','))
        return false
      end
    elsif respond_to?(atr) && send(atr).present? && send(atr).to_i > 0 && send(thing).present?
      send(thing).destroy_all
    end
  end
end
remove_attachment(name) click to toggle source
# File lib/morse_assetable.rb, line 133
def remove_attachment(name)
  return unless send(name).destroy
  n = attachment_name_remove(name)
  send("#{name}=", nil)
  send("#{n}=", nil)
end
remove_attachment?(name) click to toggle source
# File lib/morse_assetable.rb, line 140
def remove_attachment?(name)
  n = attachment_name_remove(name)
  respond_to?(n) &&
    send(n).present? &&
    send(n).to_i > 0 &&
    send(name).present?
end