Skip to content

Commit

Permalink
set callbacks on has_grid_attachment to prevent after_save from runni…
Browse files Browse the repository at this point in the history
…ng if Grip was included but has_grid_attachment has not been called
  • Loading branch information
twoism committed Jan 15, 2010
1 parent bef6f3d commit fd67921
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/grip/has_attachment.rb
Expand Up @@ -4,7 +4,6 @@ module HasAttachment
def self.included(base)
base.extend ClassMethods
base.instance_eval do
after_save :save_attachments
many :attachments,
:as => :owner,
:class_name => "Grip::Attachment",
Expand All @@ -15,12 +14,16 @@ def self.included(base)
module ClassMethods

def has_grid_attachment name, opts={}
set_callbacks_once

write_inheritable_attribute(:uploaded_file_options, {}) if uploaded_file_options.nil?
uploaded_file_options[name] = opts
self.send(:include, InstanceMethods)

define_method(name) do
attachments.find(:first, :conditions=>{:name => name.to_s})
end

define_method("#{name}=") do |new_file|
raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
uploaded_files[name] ||= {}
Expand All @@ -32,6 +35,10 @@ def has_grid_attachment name, opts={}

end

def set_callbacks_once
after_save :save_attachments unless after_save.collect(&:method).include?(:save_attachments)
end

def uploaded_file_options
read_inheritable_attribute(:uploaded_file_options)
end
Expand Down

0 comments on commit fd67921

Please sign in to comment.