Sha256: 8cb2cad1b1a58a30f22e6e06a496a9e01b76c90103246cdf7c2386d17a732aef

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

module Dragonfly
  module ActiveModelExtensions
    module ClassMethods

      include Validations

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

            # Prior to activerecord 3, adding before callbacks more than once does add it more than once
            before_save :save_attachments unless respond_to?(:before_save_callback_chain) && before_save_callback_chain.find(:save_attachments)
            before_destroy :destroy_attachments unless respond_to?(:before_destroy_callback_chain) && before_destroy_callback_chain.find(:destroy_attachments)
      
            # Register the new attribute
            dragonfly_apps_for_attributes[attribute] = app
            
            # Define the setter for the attribute
            define_method "#{attribute}=" do |value|
              attachments[attribute].assign(value)
            end
      
            # Define the getter for the attribute
            define_method attribute do
              attachments[attribute].to_value
            end
      
          end
    
        end
        app
      end
      
      def dragonfly_apps_for_attributes
        @dragonfly_apps_for_attributes ||= begin
          parent_class = ancestors.select{|a| a.is_a?(Class) }[1]
          parent_class.respond_to?(:dragonfly_apps_for_attributes) ? parent_class.dragonfly_apps_for_attributes.dup : {}
        end
      end
      
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
fog-dragonfly-0.8.2 lib/dragonfly/active_model_extensions/class_methods.rb
fog-dragonfly-0.8.1 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.8.1 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.8.0 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.7 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.6 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.5 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.4 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.3 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.2 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.1 lib/dragonfly/active_model_extensions/class_methods.rb
dragonfly-0.7.0 lib/dragonfly/active_model_extensions/class_methods.rb