Permalink
Browse files

amalgamation of style rules is less obscure, normalisation more consi…

…stent, some bugs chased out
  • Loading branch information...
1 parent a1e7a08 commit 2b69e846ce75bf0723b4cf954ac51a58b0ef3774 @will-r will-r committed Sep 7, 2011
View
@@ -12,7 +12,7 @@ unless defined? RADIANT_ROOT
end
require 'rake'
-require 'rake/rdoctask'
+require 'rdoc/task'
require 'rake/testtask'
rspec_base = File.expand_path(RADIANT_ROOT + '/vendor/plugins/rspec/lib')
@@ -97,7 +97,7 @@ namespace :spec do
end
desc 'Generate documentation for the clipped extension.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
+RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ClippedExtension'
rdoc.options << '--line-numbers' << '--inline-source'
View
@@ -1,14 +1,13 @@
class Asset < ActiveRecord::Base
-
has_many :page_attachments, :dependent => :destroy
has_many :pages, :through => :page_attachments
has_site if respond_to? :has_site
belongs_to :created_by, :class_name => 'User'
belongs_to :updated_by, :class_name => 'User'
-
+
default_scope :order => "created_at DESC"
-
+
named_scope :latest, lambda { |limit|
{ :order => "created_at DESC", :limit => limit }
}
@@ -61,6 +60,8 @@ class Asset < ActiveRecord::Base
end
validates_attachment_size :asset, :less_than => ( Radiant.config["assets.max_asset_size"] || 5 ).to_i.megabytes
+ @geometry ||= {}
+
def asset_type
AssetType.for(asset)
end
@@ -103,10 +104,12 @@ def original_geometry
end
def geometry(style_name='original')
- if style_name == 'original'
+ @geometry[style_name] ||= if style_name.to_s == 'original'
original_geometry
- elsif style = asset.styles[style_name.to_sym] # asset.styles is normalised, but self.paperclip_styles is not
+ elsif style = self.asset.styles[style_name.to_sym] # self.asset.styles holds Style objects, where self.paperclip_styles is still just rule hashes
original_geometry.transformed_by(style.geometry)
+ else
+ raise PaperclipError, "Requested style #{style_name} is not defined."
end
end
View
@@ -23,6 +23,7 @@ def initialize(name, options = {})
@icon_name = options[:icon] || name
@processors = options[:processors] || []
@styles = options[:styles] || {}
+ @styles = standard_styles if @styles == :standard
@default_radius_tag = options[:default_radius_tag] || 'link'
@extensions = options[:extensions] || []
@extensions.each { |ext| @@extension_lookup[ext] ||= self }
@@ -46,15 +47,15 @@ def plural
end
def icon(style_name='icon')
- if File.exist?("#{RAILS_ROOT}/public/images/admin/assets/#{icon_name}_#{style_name.to_s}.png")
+ if File.exist?(Rails.root + "public/images/admin/assets/#{icon_name}_#{style_name.to_s}.png")
return "/images/admin/assets/#{icon_name}_#{style_name.to_s}.png"
else
return "/images/admin/assets/#{icon_name}_icon.png"
end
end
def icon_path(style_name='icon')
- "#{RAILS_ROOT}/public#{icon(style_name)}"
+ Rails.root + "public#{icon(style_name)}"
end
def condition
@@ -89,29 +90,67 @@ def paperclip_processors
Radiant.config["assets.create_#{name}_thumbnails?"] ? processors : []
end
+ # Parses and combines the various ways in which paperclip styles can be defined, and normalises them into
+ # the format that paperclip expects. Note that :styles => :standard has already been replaced with the
+ # results of a call to standard_styles.
+ # Styles are passed to paperclip as a hash and arbitrary keys can be passed through from configuration.
+ #
def paperclip_styles
- if paperclip_processors.any?
- #TODO: define permitted options for each asset type and pass through that subset of the style-definition hash
- @paperclip_styles ||= styles.reverse_merge(configured_styles.inject({}) {|h, (k, v)| h[k] = v[:format].blank? ? v[:size] : [v[:size], v[:format].to_sym]; h})
+ # Styles are not relevant if processors are not defined.
+ # TODO: should this default to an icon set?
+ @paperclip_styles ||= if paperclip_processors.any?
+ normalize_style_rules(configured_styles.merge(styles))
else
{}
end
+ @paperclip_styles
+ end
+
+ # Takes a motley collection of differently-defined styles and renders them into the standard hash-of-hashes format.
+ # Solitary strings are assumed to be
+ #TODO: define permitted and/or expected options for the asset type and pass through that subset of the style-definition hash
+ #
+ def normalize_style_rules(styles={})
+ styles.each_pair do |name, rule|
+ unless rule.is_a? Hash
+ if rule =~ /\=/
+ parameters = rule.split(',').collect{ |parameter| parameter.split('=') } # array of pairs
+ rule = Hash[parameters].symbolize_keys # into hash of :first => last
+ else
+ rule = {:geometry => rule} # simplest case: name:geom|name:geom
+ end
+ end
+ rule[:geometry] ||= rule.delete(:size)
+ styles[name.to_sym] = rule
+ end
+ styles
end
def standard_styles
{
- :native => { :geometry => nil, :format => :jpg },
+ :native => { :geometry => "", :format => :jpg },
:icon => { :geometry => '42x42#', :format => :png },
:thumbnail => { :geometry => '100x100#', :format => :png }
}
end
+ # Paperclip styles are defined in the config entry `assets.thumbnails.asset_type`, with the format:
+ # foo:key-x,key=y,key=z|bar:key-x,key=y,key=z
+ # where 'key' can be any parameter understood by your paperclip processors. Usually they include :geometry and :format.
+ # A typical entry would be:
+ #
+ # standard:geometry=640x640>,format=jpg
+ #
+ # This method parses that string and returns the defined styles as a hash of style-defining strings that will later be normalized into hashes.
+ #
def configured_styles
- if style_definitions = Radiant.config["assets.thumbnails.#{name}"]
- @configured_styles ||= style_definitions.to_s.gsub(/\s/,'').split('|').each_with_object({}) do |styles, definition|
+ @configured_styles ||= if style_definitions = Radiant.config["assets.thumbnails.#{name}"]
+ style_definitions.gsub(/\s/,'').split('|').each_with_object({}) do |definition, styles|
name, rule = definition.split(':')
- styles[name.to_sym] = rule.split(',').collect{|option| option.split('=')}.inject({}) {|h, (k, v)| h[k.to_sym] = v; h}
+ styles[name.to_sym] = rule
end
+ else
+ {}
end
end
@@ -121,7 +160,7 @@ def legacy_styles
def style_dimensions(style_name)
if style = paperclip_styles[style_name.to_sym]
- style.is_a?(Array) ? style.first : style
+ style[:size]
end
end
@@ -6,7 +6,7 @@
%p
= t("clipped_extension.no_assets")
- else
- - if with_pagination && assets.respond_to?(:previous_page) && assets.previous_page
+ - if with_pagination && assets.respond_to?(:total_pages) && assets.total_pages > 1
= pagination_for(assets, pagination_parameters.merge(:depaginate => false, :params => {:controller => 'admin/assets', :action => 'index', :id => nil, :format => 'js'}))
%ul
@@ -26,5 +26,5 @@
%li= link_to t('clipped_extension.edit'), admin_asset_path(asset), :class => 'edit'
%li= link_to t("clipped_extension.remove"), remove_admin_asset_path(asset), :class => "delete"
- - if with_pagination && assets.respond_to?(:next_page) && assets.next_page
+ - if with_pagination && assets.respond_to?(:total_pages) && assets.total_pages > 1
= pagination_for(assets, pagination_parameters.merge(:depaginate => false, :param_name => 'p', :params => {:controller => 'admin/assets', :action => 'index', :id => nil, :format => 'js', :page_id => (page && page.id), :pp => assets.per_page }))
View
@@ -136,7 +136,7 @@ class TagError < StandardError; end
raise TagError, "'container' attribute required" unless options['container']
size = options['size'] ? options.delete('size') : 'icon'
container = options.delete('container')
- (container.to_i - asset.height(size).to_i)/2
+ ((container.to_i - asset.height(size).to_i)/2).to_s
end
['height','width'].each do |dimension|
@@ -1,5 +1,5 @@
module RadiantClippedExtension
- VERSION = "1.0.6"
+ VERSION = "1.0.7"
SUMMARY = %q{Assets for Radiant CMS}
DESCRIPTION = %q{Asset-management derived from Keith Bingman's Paperclipped extension.}
URL = "http://radiantcms.org"
@@ -27,6 +27,7 @@
context "rendering tag" do
before do
Radiant.config['assets.create_image_thumbnails?'] = true
+ Radiant.config['assets.thumbnails.image'] = 'normal:size=640x640>|small:size=320x320>'
end
it "assets:each" do
@@ -37,7 +38,7 @@
page.should render('<r:assets:first><r:asset:id /></r:assets:first>').as( "#{asset_id(:test2)}" )
end
- it "should retreive an asset by name" do
+ it "should retrieve an asset by name" do
page.should render('<r:asset:id name="video" />').as( "#{asset_id(:video)}" )
end
@@ -27,26 +27,29 @@ def make
its(:paperclip_processors) { should be_empty}
its(:paperclip_styles) { should be_empty}
its(:icon) { should == "/images/admin/assets/simple_icon.png"}
- its(:icon_path) { should == "#{RAILS_ROOT}/public/images/admin/assets/simple_icon.png"}
+ its(:icon_path) { should == Rails.root + "public/images/admin/assets/simple_icon.png"}
end
context 'with initialized thumbnail sizes' do
before { Radiant.config["assets.create_complex_thumbnails?"] = true }
subject{ AssetType.find(:complex) }
its(:paperclip_processors) { should == [:dummy] }
its(:paperclip_styles) { should_not be_empty }
- its(:paperclip_styles) { should == {:something => "99x99>"} }
+ its(:paperclip_styles) { should == {:something => {:geometry => "99x99>"}} }
its(:icon) { should == "/images/admin/assets/document_icon.png"}
end
context 'with configured thumbnail sizes' do
before {
Radiant.config["assets.create_configured_thumbnails?"] = true
- Radiant.config["assets.thumbnails.configured"] = "special:size=800x800>,format=jpg|tiny:size=#10x10,format=png"
+ Radiant.config["assets.thumbnails.configured"] = "special:size=800x800>,format=jpg|tiny:size=10x10#,format=png"
}
subject{ AssetType.find(:configured) }
its(:paperclip_processors) { should == [:dummy] }
- its(:paperclip_styles) { should == {:special => ["800x800>", :jpg], :tiny => ["#10x10", :png]} }
+ its(:paperclip_styles) { should == {
+ :special => {:geometry => "800x800>", :format => 'jpg'},
+ :tiny => {:geometry => "10x10#", :format => 'png'}
+ }}
end
context 'AssetType class methods' do
@@ -15,6 +15,7 @@
describe "on assigning a file to an asset" do
before do
Radiant.config["assets.create_image_thumbnails?"] = true
+ Radiant.config["assets.thumbnails.configured"] = "special:size=800x800>,format=jpg|tiny:size=#10x10,format=png"
end
it "should have saved the asset" do

0 comments on commit 2b69e84

Please sign in to comment.