Sha256: 82edf8cd763b62e010b53f3d08dde063d625c30c379e0499e28af11dd5391c72

Contents?: true

Size: 1.17 KB

Versions: 19

Compression:

Stored size: 1.17 KB

Contents

module Dragonfly
  module ActiveRecordExtensions
    module ClassMethods

      include Validations

      def register_dragonfly_app(accessor_prefix, app)
        metaclass.class_eval do
    
          # Defines e.g. 'image_accessor' for any activerecord class body
          define_method "#{accessor_prefix}_accessor" do |attribute|
      
            before_save :save_attached_files unless before_save_callback_chain.find(:save_attached_files)
            before_destroy :destroy_attached_files unless before_destroy_callback_chain.find(:destroy_attached_files)
      
            # 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 ||= {}
      end

    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dragonfly-0.4.4 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.4.3 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.4.2 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.4.1 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.4.0 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.8 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.7 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.6 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.5 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.4 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.3 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.2 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.3.0 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.2.1 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.1.6 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.1.5 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.1.4 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.1.1 lib/dragonfly/active_record_extensions/class_methods.rb
dragonfly-0.1.0 lib/dragonfly/active_record_extensions/class_methods.rb