Module: Defile::Attachment

Included in:
Defile::ActiveRecord::Attachment
Defined in:
lib/defile/attachment.rb

Defined Under Namespace

Classes: Attachment

Constant Summary collapse

IMAGE_TYPES =
%w[jpg jpeg gif png]

Instance Method Summary collapse

Instance Method Details

#attachment(name, cache: :cache, store: :store, raise_errors: true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/defile/attachment.rb', line 62

def attachment(name, cache: :cache, store: :store, raise_errors: true)
  attachment = :"#{name}_attachment"

  define_method attachment do
    ivar = :"@#{attachment}"
    instance_variable_get(ivar) or begin
      instance_variable_set(ivar, Attachment.new(self, name, cache: cache, store: store, raise_errors: raise_errors))
    end
  end

  define_method "#{name}=" do |uploadable|
    send(attachment).file = uploadable
  end

  define_method name do
    send(attachment).file
  end

  define_method "#{name}_cache_id=" do |cache_id|
    send(attachment).cache_id = cache_id
  end

  define_method "#{name}_cache_id" do
    send(attachment).cache_id
  end
end