How to access Spree link_to_cart function from main rails app

I am creating a surplus store in a Rails application and I need to access link_to_cart

outside of the Spree engine.

link_to_cart

can be found here: spree/core/app/helpers/spree/base_helper.rb

Since I changed the style in link_to_cart

, I also created:

  #helpers/spree/base_helper_decorator.rb
  module Spree
    module BaseHelper
      def link_to_cart(text = nil)
      text = text ? h(text) : Spree.t('cart')
      css_class = nil

      if simple_current_order.nil? or simple_current_order.item_count.zero?
        text = "#{text}: (#{Spree.t('empty')})"
        css_class = 'empty'
      else
        text = "<i class='fa fa-shopping-cart'></i> #{text}: (#{simple_current_order.item_count})  <span class='amount'>#{simple_current_order.display_total.to_html}</span>".html_safe
        css_class = 'full'
      end

      link_to text.html_safe, spree.cart_path, :class => "cart-info #{css_class} btn btn-small btn-success pull-right", style: "margin-left:10px;"
    end
  end
end

      

I've tried doing things like Spree :: BaseHelper.link_to_cart outside of the engine, but I keep getting undefined local variable or method 'link_to_cart'

I found this on a StackOverflow question and it seems promising, but I'm not sure how to change it for my needs:

module MyEngine
  class Engine < Rails::Engine
    initializer 'my_engine.action_controller' do |app|
      ActiveSupport.on_load :action_controller do
        helper MyEngine::ImportantHelper
      end
    end
  end
end

      

+3


source to share


5 answers


Okay, thanks Ben for getting me on the right track. Here is my solution:

# class ApplicationController < ActionController::Base
   include Spree::Core::ControllerHelpers::Order
   include Spree::Core::ControllerHelpers::Auth
   helper Spree::BaseHelper
   helper Spree::StoreHelper

      

Update



I ran into the problem when it current_store

was undefined outside the mechanism. I'm not sure how to solve this correctly, but in the meantime I just added the following to stop the spree from calling current_store:

module Spree
  module Core
    module ControllerHelpers
      module Order
        def current_order_params
          { currency: current_currency, guest_token: cookies.signed[:guest_token], store_id: Spree::Store.first, user_id: try_spree_current_user.try(:id) }
        end
      end
    end
  end
end

      

In addition, you helper Spree::StoreHelper

no longer need to display the cart button and current orders.

+6


source


You need to do two things

  • Create your override
  • Make your override available within the main application

Step 1: The My Spree overrides are located in the overrides folder in the main application. The override should be called module_eval

in the module you are decorating.



#overrides/base_helper_decorator.rb
Spree::BaseHelper.module_eval do

  def link_to_cart(text = nil)
    #Your customizations here
  end

end

      

Step 2. Add one of the lines below to the main ApplicationController to access your decorated helper.

helper_method :link_to_cart # Add only the link_to_cart method 

helper 'spree/base'         # Add all methods (your decoration plus the default methods) 
                            # from the Spree BaseHelper module to your main app

      

+1


source


There have been some updates since Abram left his answer. This got me on the right track, but I had a few hiccups. The main one was current_currency

undefined.

application_controller.rb

class ApplicationController < ActionController::Base
  include Spree::Core::ControllerHelpers::Order
  include Spree::Core::ControllerHelpers::Auth
  include Spree::Core::ControllerHelpers::Store
  include Spree::Core::ControllerHelpers::Common
  helper Spree::BaseHelper

      

I tried the Spree navbar and used it for my main application.

It started working when I added include Spree::Core::ControllerHelpers::Common

. Unfortunately this displays all views in the layout spree_application.html.erb

. You may need to redefine and slightly change this view.

Also, all the css comes from a rampage at this stage. You will need to move your own css to the spree namespace and @import it.

+1


source


You can add this to your main_app for cart access.

<%= link_to "Cart", spree.cart_path %>

0


source


You will also need to include the correct helpers in the main application outside of the engine. In the upcoming 3.2.0 spree release, I just needed to add:

# class ApplicationController < ActionController::Base
  include Spree::Core::ControllerHelpers::Store
  include Spree::Core::ControllerHelpers::Order
  include Spree::Core::ControllerHelpers::Auth

      

to my ApplicationController to make it link_to_cart

work all over the place. You need to include more than one helper class because for example current_store

(used by other helpers) is defined in Spree::Core::ControllerHelpers::Store

, so add that to get rid of any errors.

0


source







All Articles