Sha256: 6e47760a0717206a7376446f93a4e14266bcb05e79a9fae27c0ecb2d01ece69d

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

module DynamicPaperclip
  class HasAttachedFile < Paperclip::HasAttachedFile
    def initialize(klass, name, options)
      super

      add_route!
    end

    private

      # TODO: Are there any alternatives to literally copying this
      #       method from Paperclip::HasAttachedFile to get Ruby to find
      #       DynamicPaperclip::Attachment instead of Paperclip::Attachment?
      def define_instance_getter
        name = @name
        options = @options

        @klass.send :define_method, @name do |*args|
          ivar = "@attachment_#{name}"
          attachment = instance_variable_get(ivar)

          if attachment.nil?
            attachment = Attachment.new(name, self, options)
            instance_variable_set(ivar, attachment)
          end

          if args.length > 0
            attachment.to_s(args.first)
          else
            attachment
          end
        end
      end

      def add_route!
        url = (@options[:url] || Attachment.default_options[:url]).
              gsub(':id_partition', '*id_partition').
              gsub(':class'       , @klass.name.underscore.pluralize).
              gsub(':style'       , "dynamic_:definition")

        action = "generate_#{@klass.name.underscore}"
        default_attachment = @name.to_s.downcase.pluralize

        Rails.application.routes do
          get url,
            :to => "DynamicPaperclip::AttachmentStyles##{action}",
            :defaults => { :attachment => default_attachment }
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dynamic_paperclip-0.0.4 lib/dynamic_paperclip/has_attached_file.rb
dynamic_paperclip-0.0.3 lib/dynamic_paperclip/has_attached_file.rb
dynamic_paperclip-0.0.2 lib/dynamic_paperclip/has_attached_file.rb