module Shoppe::ModelExtension::ClassMethods

Public Instance Methods

attachment(name) click to toggle source
# File lib/shoppe/model_extension.rb, line 20
def attachment(name)
  unless self.reflect_on_all_associations(:has_many).map(&:name).include?(:attachments)
    has_many :attachments, :as => :parent, :dependent => :destroy, :class_name => 'Shoppe::Attachment'
  end
  
  has_one name, -> { select(:id, :token, :parent_id, :parent_type, :file_name, :file_type, :file_size).where(:role => name) }, :class_name => 'Shoppe::Attachment', :as => :parent

  define_method "#{name}_file" do
    instance_variable_get("@#{name}_file")
  end

  define_method "#{name}_file=" do |file|
    instance_variable_set("@#{name}_file", file)
    if file.is_a?(ActionDispatch::Http::UploadedFile)
      @pending_attachments ||= []
      @pending_attachments << {:role => name, :file => file}
    else
      nil
    end
  end
end