25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/nifty/attachments/model_extension.rb', line 25
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
define_method "#{name}_delete" do
instance_variable_get("@#{name}_delete")
end
define_method "#{name}_delete=" do |delete|
instance_variable_set("@#{name}_delete", delete)
unless delete.blank?
@pending_attachment_deletions ||= []
@pending_attachment_deletions << name
end
end
end
|