Sha256: 0d42cf72b909c026c86993a0ad0d641581d46d2c39249630795a04fe68510b96

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

module DynamicPaperclip
  class AttachmentStylesController < ApplicationController
    def action_missing(name, *args, &block)
      if name =~ /^generate_(.+)$/
        send :generate, $1
      end
    end

    private

      def generate(class_name)
        klass = class_name.camelize.constantize
        attachment_name = params[:attachment].singularize.to_sym

        # Ensure that we have a valid attachment name and an ID
        raise Errors::UndefinedAttachment unless klass.attachment_definitions[attachment_name]
        raise Errors::MissingID unless params[:id] || params[:id_partition]

        id = params[:id] || id_from_partition(params[:id_partition])

        attachment = klass.find(id).send(attachment_name)
        style_name = StyleNaming.dynamic_style_name_from_definition(params[:definition])

        # Validate URL hash against requested style name
        raise Errors::InvalidHash unless DynamicPaperclip::UrlSecurity.valid_hash?(params[:s], style_name)

        # Only process style if it doesn't exist,
        # otherwise we may just be fielding a request for
        # an existing style (i.e. serve_static_assets is true)
        attachment.process_dynamic_style params[:definition] unless attachment.exists?(style_name)

        send_file attachment.path(style_name), :disposition => 'inline', :type => attachment.content_type
      end

      def id_from_partition(partition)
        partition.gsub('/', '').to_i
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynamic_paperclip-0.0.3 app/controllers/dynamic_paperclip/attachment_styles_controller.rb
dynamic_paperclip-0.0.2 app/controllers/dynamic_paperclip/attachment_styles_controller.rb