Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Use class_eval to override link_to_cart in Solidus Demo
Browse files Browse the repository at this point in the history
Goal
----

As a Solidus Demo contributor

I would like to use `class_eval` to override link_to_cart

So that we can ensure that the `Spree::BaseHelper` engine module is
reopened by Solidus Demo, which according to
https://guides.rubyonrails.org/engines.html#improving-engine-functionality,
is necessary for overriding the module.

Basis
-----

From
https://guides.rubyonrails.org/engines.html#improving-engine-functionality:

> It is very important that the override reopens the class or module.
Using the class or module keywords would define them if they were not
already in memory, which would be incorrect because the definition lives
in the engine. Using `class_eval` as shown above ensures you are
reopening.
  • Loading branch information
gsmendoza committed Jan 12, 2022
1 parent 4069e83 commit 4f74958
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions app/monkey_patches/spree/base_helper_monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
module Spree
module BaseHelper
def link_to_cart(text = nil)
text = text ? h(text) : t('spree.cart')
css_class = nil
Spree::BaseHelper.class_eval do
def link_to_cart(text = nil)
text = text ? h(text) : t('spree.cart')
css_class = nil

if current_order.nil? || current_order.item_count.zero?
text = ""
css_class = 'empty'
else
text = "<div class='link-text'>#{current_order.item_count}</div>"\
#"<span class='amount'>#{current_order.display_total.to_html}</span>"
css_class = 'full'
end
link_to text.html_safe, spree.cart_path, class: "cart-info #{css_class}"
if current_order.nil? || current_order.item_count.zero?
text = ""
css_class = 'empty'
else
text = "<div class='link-text'>#{current_order.item_count}</div>"\
#"<span class='amount'>#{current_order.display_total.to_html}</span>"
css_class = 'full'
end
link_to text.html_safe, spree.cart_path, class: "cart-info #{css_class}"
end
end

0 comments on commit 4f74958

Please sign in to comment.