lib/refile/attachment.rb in refile-0.2.4 vs lib/refile/attachment.rb in refile-0.2.5

- old
+ new

@@ -2,10 +2,11 @@ module Attachment IMAGE_TYPES = %w[jpg jpeg gif png] class Attachment attr_reader :record, :name, :cache, :store, :cache_id, :options + attr_accessor :remove def initialize(record, name, **options) @record = record @name = name @options = options @@ -42,23 +43,42 @@ def cache_id=(id) @cache_id = id unless @cache_file end def store! - if cache_id and not cache_id == "" + if remove? + delete! + elsif cached? file = store.upload(cache.get(cache_id)) - cache.delete(cache_id) - store.delete(id) if id + delete! self.id = file.id + end + end + + def delete! + if cached? + cache.delete(cache_id) @cache_id = nil @cache_file = nil end + store.delete(id) if id + self.id = nil end + def remove? + remove.present? and remove !~ /\A0|false$\z/ + end + def errors @errors end + + private + + def cached? + cache_id and not cache_id == "" + end end def attachment(name, cache: :cache, store: :store, raise_errors: true) attachment = :"#{name}_attachment" @@ -81,9 +101,17 @@ send(attachment).cache_id = cache_id end define_method "#{name}_cache_id" do send(attachment).cache_id + end + + define_method "remove_#{name}=" do |remove| + send(attachment).remove = remove + end + + define_method "remove_#{name}" do + send(attachment).remove end end end end