From ff0c8370570532da35b7fc08c089f105d7145fe9 Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sat, 5 May 2012 00:34:51 +0400 Subject: [PATCH] Fix for #1436 - helper methods .._image and custom image styles Merges #1495. Fixes #1436. --- core/app/helpers/spree/base_helper.rb | 55 +++++++++++++++++---------- core/spec/helpers/base_helper_spec.rb | 19 +++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb index 2fdd24002ba..6a0f4b42816 100644 --- a/core/app/helpers/spree/base_helper.rb +++ b/core/app/helpers/spree/base_helper.rb @@ -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] } @@ -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 @@ -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 diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb index 977b8caaa2b..e2179822a4f 100644 --- a/core/spec/helpers/base_helper_spec.rb +++ b/core/spec/helpers/base_helper_spec.rb @@ -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