Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove method missing helper #1623

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions core/app/helpers/spree/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,6 @@ def pretty_time(time)
time.strftime("%l:%M %p")].join(" ")
end

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

def link_to_tracking(shipment, options = {})
return unless shipment.tracking && shipment.shipping_method

Expand All @@ -157,37 +148,5 @@ def plural_resource_name(resource_class)
resource_class.model_name.human(count: Spree::I18N_GENERIC_PLURAL)
end

private

# Returns style of image or nil
def image_style_from_method_name(method_name)
if method_name.to_s.match(/_image$/) && 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 create_product_image_tag(image, product, options, style)
options.reverse_merge! alt: image.alt.blank? ? product.name : image.alt
image_tag image.attachment.url(style), options
end

def define_image_method(style)
self.class.send :define_method, "#{style}_image" do |product, *options|
Spree::Deprecation.warn "Spree image helpers will be deprecated in the near future. Use the provided resource to access the intendend image directly.", caller
options = options.first || {}
if product.images.empty?
if !product.is_a?(Spree::Variant) && !product.variant_images.empty?
create_product_image_tag(product.variant_images.first, product, options, style)
elsif product.is_a?(Variant) && !product.product.variant_images.empty?
create_product_image_tag(product.product.variant_images.first, product, options, style)
else
image_tag "noimage/#{style}.png", options
end
else
create_product_image_tag(product.images.first, product, options, style)
end
end
end
end
end
40 changes: 0 additions & 40 deletions core/spec/helpers/base_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,6 @@
end
end

# Regression test for https://github.com/spree/spree/issues/1436
context "defining custom image helpers" do
let(:product) { mock_model(Spree::Product, images: [], variant_images: []) }
before do
Spree::Image.class_eval do
attachment_definitions[:attachment][:styles][:very_strange] = '1x1'
end
end

it "should not raise errors when style exists" do
Spree::Deprecation.silence do
very_strange_image(product)
end
end

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

# Regression test for https://github.com/spree/spree/issues/2034
context "flash_message" do
let(:flash) { { "notice" => "ok", "foo" => "foo", "bar" => "bar" } }
Expand Down Expand Up @@ -142,26 +122,6 @@ def link_to_tracking_html(options = {})
end
end

# Regression test for https://github.com/spree/spree/issues/5384
context "custom image helpers conflict with inproper statements" do
let(:product) { mock_model(Spree::Product, images: [], variant_images: []) }
before do
Spree::Image.class_eval do
attachment_definitions[:attachment][:styles][:foobar] = '1x1'
end
end

it "should not raise errors when helper method called" do
Spree::Deprecation.silence do
foobar_image(product)
end
end

it "should raise NoMethodError when statement with name equal to style name called" do
expect { foobar(product) }.to raise_error(NoMethodError)
end
end

context "pretty_time" do
it "prints in a format" do
expect(pretty_time(DateTime.new(2012, 5, 6, 13, 33))).to eq "May 06, 2012 1:33 PM"
Expand Down