Skip to content

Commit

Permalink
Fix for #1436 - helper methods .._image and custom image styles
Browse files Browse the repository at this point in the history
Merges #1495. Fixes #1436.
  • Loading branch information
ademidov authored and radar committed May 4, 2012
1 parent ec36726 commit ff0c837
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
55 changes: 35 additions & 20 deletions core/app/helpers/spree/base_helper.rb
Expand Up @@ -63,24 +63,6 @@ def variant_options(v, allow_back_orders = Spree::Config[:allow_backorders], inc
list
end

Spree::Image.attachment_definitions[:attachment][:styles].each do |style, v|
# Defines these methods by default:
# def mini_image
# def small_image
# def product_image
# def large_image
define_method "#{style}_image" do |product, *options|
options = options.first || {}
if product.images.empty?
image_tag "noimage/#{style}.png", options
else
image = product.images.first
options.reverse_merge! :alt => image.alt.blank? ? product.name : image.alt
image_tag image.attachment.url(style), options
end
end
end

def meta_data_tags
object = instance_variable_get('@'+controller_name.singularize)
meta = { :keywords => Spree::Config[:default_meta_keywords], :description => Spree::Config[:default_meta_description] }
Expand Down Expand Up @@ -141,9 +123,9 @@ def taxons_tree(root_taxon, current_taxon, max_level = 1)

def available_countries
countries = Zone.find_by_name(Spree::Config[:checkout_zone]).try(:country_list) || Country.all
countries.collect do |c|
countries.collect do |c|
c.name = I18n.t(c.iso, :scope => 'countries', :default => c.name)
c
c
end.sort{ |a,b| a.name <=> b.name }
end

Expand Down Expand Up @@ -180,5 +162,38 @@ def gem_available?(name)
rescue
Gem.available?(name)
end

def method_missing(method_name, *args, &block)
if image_style = image_style_from_method_name(method_name)
define_image_method(image_style)
self.send(method_name, *args)
else
super
end
end

private

# Returns style of image or nil
def image_style_from_method_name(method_name)
if style = method_name.to_s.sub(/_image$/, '')
possible_styles = Spree::Image.attachment_definitions[:attachment][:styles]
style if style.in? possible_styles.with_indifferent_access
end
end

def define_image_method(style)
self.class.send :define_method, "#{style}_image" do |product, *options|
options = options.first || {}
if product.images.empty?
image_tag "noimage/#{style}.png", options
else
image = product.images.first
options.reverse_merge! :alt => image.alt.blank? ? product.name : image.alt
image_tag image.attachment.url(style), options
end
end
end

end
end
19 changes: 19 additions & 0 deletions core/spec/helpers/base_helper_spec.rb
Expand Up @@ -10,4 +10,23 @@
seo_url(taxon).should == "/t/bam"
end
end

# Regression test for #1436
context "defining custom image helpers" do
let(:product) { mock_model(Spree::Product, :images => []) }
before do
Spree::Image.class_eval do
attachment_definitions[:attachment][:styles].merge!({:very_strange => '1x1'})
end
end

it "should not raise errors when style exists" do
lambda { very_strange_image(product) }.should_not raise_error
end

it "should raise NoMethodError when style is not exists" do
lambda { another_strange_image(product) }.should raise_error(NoMethodError)
end

end
end

0 comments on commit ff0c837

Please sign in to comment.