Module: EchoUploads::TempFileSaving::ClassMethods

Defined in:
lib/echo_uploads/temp_file_saving.rb

Instance Method Summary collapse

Instance Method Details

#configure_temp_file_saving(attr, options) ⇒ Object

Wraps ActiveRecord’s persistence methods. We can’t use a callback for this. See the comment above for an explanation of why.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/echo_uploads/temp_file_saving.rb', line 66

def configure_temp_file_saving(attr, options)
  # Wrap the #save method. This also suffices for #create.
  define_method("save_with_#{attr}_temp_file") do |*args|
    @echo_uploads_saving ||= {}
    @echo_uploads_saving[attr] = true
    begin
      success = maybe_save_temp_file(attr, options) do
        send "save_without_#{attr}_temp_file", *args
      end
      success
    ensure
      @echo_uploads_saving.delete attr
    end
  end
  alias_method_chain :save, "#{attr}_temp_file".to_sym
  
  # Wrap the #update and #update_attributes methods.
  define_method("update_with_#{attr}_temp_file") do |*args|
    @echo_uploads_updating ||= {}
    @echo_uploads_updating[attr] = true
    begin
      success = maybe_save_temp_file(attr, options) do
        send "update_without_#{attr}_temp_file", *args
      end
      success
    ensure
      @echo_uploads_updating.delete attr
    end
  end
  alias_method_chain :update, "#{attr}_temp_file".to_sym
  alias_method :update_attributes, "update_with_#{attr}_temp_file".to_sym
end