Module: Versions::Attachment::ClassMethods

Defined in:
lib/versions/attachment.rb

Instance Method Summary collapse

Instance Method Details

#store_attachments_in(accessor, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/versions/attachment.rb', line 19

def store_attachments_in(accessor, options = {})
  attachment_class = options[:attachment_class] || '::Versions::SharedAttachment'

  if accessor.nil? || accessor == self
    belongs_to :attachment,
               :class_name  => attachment_class,
               :foreign_key => 'attachment_id'
    include Owner
  else
    klass = (options[:class_name] || accessor.to_s.capitalize).constantize
    klass.class_eval do
      belongs_to :attachment,
                 :class_name  => attachment_class,
                 :foreign_key => 'attachment_id'
      include Owner
    end

    line = __LINE__
    definitions = <<-EOF
      def file=(file)
        #{accessor}.file = file
      end

      def file
        #{accessor}.file
      end
    EOF

    methods_module = Module.new
    methods_module.class_eval(definitions, __FILE__, line + 2)
    include methods_module
  end

end