Skip to content

Instantly share code, notes, and snippets.

@FineLineAutomation
Last active August 29, 2015 13:56
Show Gist options
  • Save FineLineAutomation/8978601 to your computer and use it in GitHub Desktop.
Save FineLineAutomation/8978601 to your computer and use it in GitHub Desktop.
Scoping based on whether we are in spree's admin backend or not.
#Make sure this appears somewhere in your application.rb file.
config.to_prepare do
# Load application's model / class decorators
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
# Load application's view overrides
Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
#put in app/controllers/spree/base_controller_decorator.rb
Spree::BaseController.class_eval do
before_filter :store_path_in_thread
def store_path_in_thread
Thread.current[:path] = request.original_url
end
end
Spree::Taxon.class_eval do
scope :with_children, :conditions => "parent_id is null OR #{self.table_name}.id IN (SELECT taxon_id FROM spree_products_taxons)"
def self.scope_based_on_path
path = Thread.current[:path]
if path.nil? or path.include? "admin"
order("#{self.table_name}.position")
else
#binding.pry
with_children
#order("#{self.table_name}.position")
end
end
scope :base_on_path, lambda { scope_based_on_path }
default_scope -> { base_on_path }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment