Last active
December 18, 2015 23:39
-
-
Save rdblakemore/5863417 to your computer and use it in GitHub Desktop.
On-demand image sizing for Comfy and S3 using ActiveSupport::Concern. On-demand concept/code by:
http://www.ryanalynporter.com/2012/06/07/resizing-thumbnails-on-demand-with-paperclip-and-rails/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------- | |
# file_concerns.rb | |
# ---------------- | |
require 'uri' | |
module Models | |
module ComfortableMexicanSofa | |
module FileConcerns | |
extend ActiveSupport::Concern | |
def styles | |
unless @dynamic_style_format.blank? | |
{ dynamic_style_format_symbol => @dynamic_style_format } | |
else | |
{ :cms_thumb => '80x60#' } | |
end | |
end | |
def dynamic_style_format_symbol | |
URI.escape(@dynamic_style_format).to_sym | |
end | |
def url_by_format(format=:original) | |
if(format.is_a?(Symbol)) | |
file.url(format) | |
else | |
@dynamic_style_format = format | |
file.reprocess!(dynamic_style_format_symbol) unless file.exists?(dynamic_style_format_symbol) | |
file.url(dynamic_style_format_symbol) | |
end | |
end | |
included do | |
# override implementation to support on-demand styles/sizing | |
has_attached_file :file, ::ComfortableMexicanSofa.config.upload_file_options.merge( | |
:styles => Proc.new { |attachment| attachment.instance.styles } | |
) | |
end | |
end | |
end | |
end | |
# --------------------------------------------------------------------- | |
# an initializer file...to extend Comfy's file.rb with file_concerns.rb | |
# --------------------------------------------------------------------- | |
Cms::File.send(:include, Models::ComfortableMexicanSofa::FileConcerns) | |
# ---------------------- | |
# some constant sizes... | |
# ---------------------- | |
CMS_IMAGE_SIZES = { | |
:banner => '1280x250#', | |
:home => '1280x500#', | |
:venue => '892x300#' | |
} | |
# ----------------------------------------------------------------------------- | |
# now get a file and request the image URL (image will be created if necessary) | |
# ----------------------------------------------------------------------------- | |
file.url_by_format(CMS_IMAGE_SIZES[:home]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment