Skip to content

Instantly share code, notes, and snippets.

@mamhoff
Last active October 24, 2016 15:16
Show Gist options
  • Save mamhoff/56fc8afc00d2850d38da6fcf5b0134de to your computer and use it in GitHub Desktop.
Save mamhoff/56fc8afc00d2850d38da6fcf5b0134de to your computer and use it in GitHub Desktop.
Products Controller with sort by price
module Spree
ProductsController.class_eval do
helper_method :sorting_param
alias_method :old_index, :index
def index
old_index # Like calling super: http://stackoverflow.com/a/13806783/73673
@products = @products.includes(:prices).send(sorting_scope)
end
def sorting_param
params[:sorting].try(:to_sym) || default_sorting
end
private
def sorting_scope
allowed_sortings.include?(sorting_param) ? sorting_param : default_sorting
end
def default_sorting
:ascend_by_updated_at
end
def allowed_sortings
[:descend_by_master_price, :ascend_by_master_price, :ascend_by_updated_at]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment