79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/attach/model_extension.rb', line 79
def attachment(name, options = {}, &block)
unless self.reflect_on_all_associations(:has_many).map(&:name).include?(:attachments)
has_many :attachments, :as => :owner, :dependent => :destroy, :class_name => 'Attach::Attachment'
end
if block_given?
Processor.register(self, name, &block)
end
define_method name do
instance_variable_get("@#{name}") || begin
attachment = self.attachments.where(:role => name, :parent_id => nil).first
instance_variable_set("@#{name}", attachment)
end
end
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, :options => options}
else
nil
end
end
define_method "#{name}_delete" do
instance_variable_get("@#{name}_delete")
end
define_method "#{name}_delete=" do |delete|
delete = delete.to_i
instance_variable_set("@#{name}_delete", delete)
if delete == 1
@pending_attachment_deletions ||= []
@pending_attachment_deletions << name
end
end
end
|