Sha256: 23ad4188969bc5e33b795531dd3035a33f8b5601a5a0a5fc3c424bae84a8051d

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

module Fleximage
  
  # Renders a .flexi template
  class View < ActionView::TemplateHandler #:nodoc:
    class TemplateDidNotReturnImage < RuntimeError #:nodoc:
    end
    
    def self.call(template)
      "Fleximage::View.new(self).render(template)"
    end

    def initialize(action_view)
      @view = action_view
    end
    
    def render(template)
      # process the view
      result = @view.instance_eval do
        
        # Shorthand color creation
        def color(*args)
          Fleximage::Operator::Base.color(*args)
        end
        
        #execute the template
        eval(template.source)
      end
      
      # Raise an error if object returned from template is not an image record
      unless result.class.include?(Fleximage::Model::InstanceMethods)
        raise TemplateDidNotReturnImage, 
                ".flexi template was expected to return a model instance that acts_as_fleximage, but got an instance of <#{result.class}> instead."
      end
      
      # Figure out the proper format
      requested_format = (@view.params[:format] || :jpg).to_sym
      unless [:jpg, :gif, :png].include?(requested_format)
        raise 'Image must be requested with an image type format.  jpg, gif and png only are supported.'
      end
      
      # Set proper content type
      @view.controller.response.content_type = Mime::Type.lookup_by_extension(requested_format.to_s).to_s
      
      # Set proper caching headers
      if defined?(Rails) && Rails.env == 'production'
        @view.controller.response.headers['Cache-Control'] = 'public, max-age=86400'
      end
      
      # return rendered result
      return result.output_image(:format => requested_format)
    ensure
    
      # ensure garbage collection happens after every flex image render
      GC.start
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
robinboening-fleximage-1.0.4 lib/fleximage/view.rb
fleximage-1.0.4 lib/fleximage/view.rb
fleximage-1.0.3 lib/fleximage/view.rb
fleximage-1.0.2 lib/fleximage/view.rb
fleximage-1.0.1 lib/fleximage/view.rb
fleximage-1.0.0 lib/fleximage/view.rb