Allows blocks in has_orders concern

This commit is contained in:
kikito
2016-06-03 18:30:47 +02:00
parent fa6f34bf5c
commit 9f3b99f0a6
2 changed files with 12 additions and 1 deletions

View File

@@ -3,7 +3,8 @@ module HasOrders
class_methods do
def has_orders(valid_orders, *args)
before_action(*args) do
before_action(*args) do |c|
valid_orders = valid_orders.call(c) if valid_orders.respond_to?(:call)
@valid_orders = valid_orders
@current_order = @valid_orders.include?(params[:order]) ? params[:order] : @valid_orders.first
end

View File

@@ -7,10 +7,15 @@ describe 'HasOrders' do
controller(FakeController) do
include HasOrders
has_orders ['created_at', 'votes_count', 'flags_count'], only: :index
has_orders ->{ ['votes_count', 'flags_count'] }, only: :new
def index
render text: "#{@current_order} (#{@valid_orders.join(' ')})"
end
def new
render text: "#{@current_order} (#{@valid_orders.join(' ')})"
end
end
it "has the valid orders set up" do
@@ -18,6 +23,11 @@ describe 'HasOrders' do
expect(response.body).to eq('created_at (created_at votes_count flags_count)')
end
it "allows specifying the orders via a lambda" do
get :new
expect(response.body).to eq('votes_count (votes_count flags_count)')
end
describe "the current order" do
it "defaults to the first one on the list" do
get :index