Skip to content

Instantly share code, notes, and snippets.

@ChrisLusted
Forked from rdblakemore/gist:5863417
Created November 13, 2013 16:59
Show Gist options
  • Save ChrisLusted/7452480 to your computer and use it in GitHub Desktop.
Save ChrisLusted/7452480 to your computer and use it in GitHub Desktop.
# ----------------
# 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