Module: Dragonfly::ActiveModelExtensions::ClassMethods

Defined in:
lib/dragonfly/active_model_extensions/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#dragonfly_attachment_classesObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dragonfly/active_model_extensions/class_methods.rb', line 76

def dragonfly_attachment_classes
  @dragonfly_attachment_classes ||= begin
    parent_class = ancestors.select{|a| a.is_a?(Class) }[1]
    if parent_class.respond_to?(:dragonfly_attachment_classes)
      parent_class.dragonfly_attachment_classes.map do |klass|
        new_dragonfly_attachment_class(klass.attribute, klass.app, klass.config_block)
      end
    else
      []
    end
  end
end

#new_dragonfly_attachment_class(attribute, app, config_block) ⇒ Object



89
90
91
# File 'lib/dragonfly/active_model_extensions/class_methods.rb', line 89

def new_dragonfly_attachment_class(attribute, app, config_block)
  Class.new(Attachment).init(self, attribute, app, config_block)
end

#register_dragonfly_app(macro_name, app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dragonfly/active_model_extensions/class_methods.rb', line 7

def register_dragonfly_app(macro_name, app)
  (class << self; self; end).class_eval do
    
    # Defines e.g. 'image_accessor' for any activemodel class body
    define_method macro_name do |attribute, &config_block|

      # Add callbacks
      before_save :save_dragonfly_attachments
      before_destroy :destroy_dragonfly_attachments

      # Register the new attribute
      dragonfly_attachment_classes << new_dragonfly_attachment_class(attribute, app, config_block)
      
      # Define the setter for the attribute
      define_method "#{attribute}=" do |value|
        dragonfly_attachments[attribute].assign(value)
      end

      # Define the getter for the attribute
      define_method attribute do
        dragonfly_attachments[attribute].to_value
      end

      # Define the URL setter
      define_method "#{attribute}_url=" do |url|
        unless url.blank?
          dragonfly_attachments[attribute].assign(app.fetch_url(url))
        end
      end

      # Define the URL getter
      define_method "#{attribute}_url" do
        nil
      end

      # Define the remove setter
      define_method "remove_#{attribute}=" do |value|
        unless [0, "0", false, "false", "", nil].include?(value)
          dragonfly_attachments[attribute].assign(nil)
          instance_variable_set("@remove_#{attribute}", true)
        end
      end

      # Define the remove getter
      attr_reader "remove_#{attribute}"

      # Define the retained setter
      define_method "retained_#{attribute}=" do |string|
        unless string.blank?
          begin
            dragonfly_attachments[attribute].retained_attrs = Serializer.marshal_decode(string)
          rescue Serializer::BadString => e
            app.log.warn("*** WARNING ***: couldn't update attachment with serialized retained_#{attribute} string #{string.inspect}")              
          end
        end
      end
      
      # Define the retained getter
      define_method "retained_#{attribute}" do
        attrs = dragonfly_attachments[attribute].retained_attrs
        Serializer.marshal_encode(attrs) if attrs
      end
      
    end
    
  end
  app
end