Class: Grandstand::Image

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/grandstand/image.rb

Class Method Summary collapse

Class Method Details

.sizesObject

Returns the image sizes available for embedding in the public-facing pages. Sorts by potential pixel dimensions, so icon sizes (75x75) return as smaller than page-width sizes (500x500 or whatever). Also adds a fun description for the WYSIWYG editor, so ‘1024x768#’ becomes ‘1024 x 768 (cropped)’ in the event that the user is unhappy with whatever goddamn sizes they’re intended to accept.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/grandstand/image.rb', line 29

def sizes
  return @sizes if defined? @sizes
  sorted_sizes = ActiveSupport::OrderedHash.new
  Grandstand::Image.attachment_definitions[:file][:styles].reject {|style, dimensions| style.to_s =~ /^grandstand_/ }.inject({}) do |sizes, style_definition|
    style, dimensions = style_definition
    width, height = dimensions.gsub(/[^0-9x]/, '').split('x').map(&:to_i)
    width ||= 0
    height ||= 0
    if width.zero?
      description = "#{height} pixel#{'s' unless height == 1} tall; width to scale"
    elsif height.zero?
      description = "#{width} pixel#{'s' unless width == 1} wide; height to scale"
    else
      description = "#{width} x #{height}"
    end
    additional = []
    clean_dimensions = dimensions.gsub(/[0-9x]/, '')
    if clean_dimensions =~ /\#$/
      additional.push('cropped')
    end
    description << " (#{additional.join(', ')})" unless additional.empty?
    sizes[style.to_sym] = {:description => description, :dimensions => dimensions, :size => (width.zero? ? height : width) * (height.zero? ? width : height)}
    sizes
  end.sort {|a, b| a[1][:size] <=> b[1][:size] }.each do |key, value|
    sorted_sizes[key] = value
  end
  sorted_sizes
end