lib/paperclip/storage/database.rb in paperclip_database-0.1.0 vs lib/paperclip/storage/database.rb in paperclip_database-0.2.0

- old
+ new

@@ -24,12 +24,12 @@ # t.integer :user_id # t.timestamps # end # execute 'ALTER TABLE avatars ADD COLUMN file_contents LONGBLOB' # - # You can optionally specify any storage table name you want as follows: - # has_attached_file :avatar, :storage => :database, :database_table => 'avatar_files' + # You can optionally specify any storage table name you want and whether or not deletion is done by cascading or not as follows: + # has_attached_file :avatar, :storage => :database, :database_table => 'avatar_files', :cascade_deletion => true # # 3. By default, URLs will be set to this pattern: # /:relative_root/:class/:attachment/:id?style=:style # # Example: @@ -82,22 +82,22 @@ def setup_paperclip_files_model #TODO: This fails when your model is in a namespace. @paperclip_files = "#{instance.class.name.underscore}_#{name.to_s}_paperclip_files" if !Object.const_defined?(@paperclip_files.classify) @paperclip_file = Object.const_set(@paperclip_files.classify, Class.new(::ActiveRecord::Base)) + @paperclip_file.set_table_name @options[:database_table] || name.to_s.pluralize + @paperclip_file.validates_uniqueness_of :style, :scope => instance.class.table_name.classify.underscore + '_id' + case Rails::VERSION::STRING + when /^2/ + @paperclip_file.named_scope :file_for, lambda {|style| { :conditions => ['style = ?', style] }} + else # 3.x + @paperclip_file.scope :file_for, lambda {|style| { :conditions => ['style = ?', style] }} + end else @paperclip_file = Object.const_get(@paperclip_files.classify) end - @database_table = @options[:database_table] || name.to_s.pluralize - @paperclip_file.set_table_name @database_table - @paperclip_file.validates_uniqueness_of :style, :scope => instance.class.table_name.classify.underscore + '_id' - case Rails::VERSION::STRING - when /^2/ - @paperclip_file.named_scope :file_for, lambda {|style| { :conditions => ['style = ?', style] }} - else # 3.x - @paperclip_file.scope :file_for, lambda {|style| { :conditions => ['style = ?', style] }} - end + @database_table = @paperclip_file.table_name #FIXME: This fails when using set_table_name "<myname>" in your model #FIXME: This should be fixed in ActiveRecord... instance.class.has_many @paperclip_files, :foreign_key => instance.class.table_name.classify.underscore + '_id' end @@ -172,10 +172,14 @@ def flush_deletes #:nodoc: ActiveRecord::Base.logger.info("[paperclip] Deleting files for #{name}") @queued_for_delete.each do |path| /id=([0-9]+)/.match(path) - @paperclip_file.destroy $1 + if @options[:cascade_deletion] && !instance.class.exists?(instance.id) + raise RuntimeError, "Deletion has not been done by through cascading." if @paperclip_file.exists?($1) + else + @paperclip_file.destroy $1 + end end @queued_for_delete = [] end module ControllerClassMethods