Sha256: f25872d58006206b67ff7fa63a9ededc4268c71606d0f9b9a70c4451d5ed21bd
Contents?: true
Size: 1.57 KB
Versions: 2
Compression:
Stored size: 1.57 KB
Contents
module Nifty module Attachments module ModelExtension def self.included(base) base.extend ClassMethods base.before_save do if @pending_attachments @pending_attachments.each do |pa| old_attachments = self.nifty_attachments.where(:role => pa[:role]).pluck(:id) self.nifty_attachments.create(:uploaded_file => pa[:file], :role => pa[:role]) self.nifty_attachments.where(:id => old_attachments).destroy_all end @pending_attachments = nil end end end module ClassMethods def attachment(name) unless self.reflect_on_all_associations(:has_many).map(&:name).include?(:nifty_attachments) has_many :nifty_attachments, :as => :parent, :dependent => :destroy, :class_name => 'Nifty::Attachments::Attachment' end has_one name, -> { select(:id, :token, :digest, :parent_id, :parent_type, :file_name, :file_type).where(:role => name) }, :class_name => 'Nifty::Attachments::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 end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
nifty-attachments-1.0.1 | lib/nifty/attachments/model_extension.rb |
nifty-attachments-1.0.0 | lib/nifty/attachments/model_extension.rb |