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

Overriding whitelisted_ransackable_scopes doesn't work #10799

Open
elisoncampos opened this issue Feb 22, 2021 · 3 comments
Open

Overriding whitelisted_ransackable_scopes doesn't work #10799

elisoncampos opened this issue Feb 22, 2021 · 3 comments

Comments

@elisoncampos
Copy link

When trying to implement a new ransacker for searching orders I couldn't find a way to add my ransacker method to the scopes whitelisted in the model

Context

In product_decorator.rb:

module Neospree
  module Spree
    module ProductDecorator
      def self.prepended(base)
        base.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name line_items_contain_base_product] 
      end  

      def line_items_contain_base_product(query)
        # my logic here...
      end
    end
  end
end

if Spree::Product.included_modules.exclude?(Neospree::Spree::ProductDecorator)
  Spree::Product.prepend Neospree::Spree::ProductDecorator
end

Then in my view I've added it to the search form:

<div class="col-12 col-lg-3">
          <div class="form-group">
            <%= label_tag :q_line_items_contain_base_product, "Produto Base" %>
            <%= f.select :line_items_contain_base_product, base_products_select_list, {include_blank: true}, class: 'select2 js-filterable' %>
          </div>
        </div>

Expected Behavior

I expected that my field would be rendered and the method was found as a ransackable

Actual Behavior

An undefined method `line_items_contain_base_product' for #Ransack::Search:0x00007faddbdb45f8 occurs.

Your Environment

  • Ruby 2.7.0
  • Rails 6.0.3
  • Spree 4.1.11
@damianlegawiec
Copy link
Member

Please try

::Spree::Product.whitelisted_ransackable_scopes  = %w[not_discontinued search_by_name line_items_contain_base_product]

or https://stackoverflow.com/questions/18683750/how-to-prepend-classmethods

@elisoncampos
Copy link
Author

@damianlegawiec sorry but what do you mean by trying

::Spree::Product.whitelisted_ransackable_scopes  = %w[not_discontinued search_by_name line_items_contain_base_product]

where? I'm gonna have a look in the stackoverflow link.

@pcwiek
Copy link

pcwiek commented May 25, 2022

I encountered this ticket after more than a year with the exact same issue. We actually made it work. Not sure if it follows the 'best practices', but it's better than nothing.

Anyway, I'm not sure how to use aforementioned approach:

::Spree::Product.whitelisted_ransackable_scopes  = %w[not_discontinued search_by_name line_items_contain_base_product]

And that's why we opted for a more explicit way. The following approach that's based on the link that @damianlegawiec provided above:

module Neospree
  module Spree
    module ProductDecorator
      module ScopeFilters
        def line_items_contain_base_product(query)
          # my logic here...
        end
      end

      def self.prepended(base)
        base.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name line_items_contain_base_product] 
        base.extend(ScopeFilters)
      end  
    end
  end
end

if Spree::Product.included_modules.exclude?(Neospree::Spree::ProductDecorator)
  Spree::Product.prepend Neospree::Spree::ProductDecorator
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants