Module: Nifty::Attachments::ModelExtension

Defined in:
lib/nifty/attachments/model_extension.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nifty/attachments/model_extension.rb', line 5

def self.included(base)
  base.extend ClassMethods
  base.after_save do
    if @pending_attachment_deletions
      self.nifty_attachments.where(:role => @pending_attachment_deletions).destroy_all
    end

    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