require 'spec_helper' describe WithOrder::ActionViewExtension do describe '#link_with_order(*args, &block)' do context 'the current field ordered on is "first_name"' do it 'reverses the existing direction if the field is "first_name"' do helper.stub!(:params).and_return({order: 'first_name-asc'}) npw = NobelPrizeWinner.with_order(helper.params) helper.link_with_order( (npw.current_order[:field] == :first_name and npw.current_order[:dir] == :asc) ? 'DESC' : 'ASC', npw, :first_name ).should == 'DESC' end it 'creates an "asc" order for fields other than "first_name"' do helper.stub!(:params).and_return({order: 'first_name-asc'}) npw = NobelPrizeWinner.with_order(helper.params) helper.link_with_order( (npw.current_order[:field] == :last_name and npw.current_order[:dir] == :asc) ? 'DESC' : 'ASC', npw, :last_name ).should == 'ASC' end it 'defaults to "desc" if no direction is given' do helper.stub!(:params).and_return({order: 'first_name'}) npw = NobelPrizeWinner.with_order(helper.params) helper.link_with_order( (npw.current_order[:field] == :first_name and npw.current_order[:dir] == :asc) ? 'DESC' : 'ASC', npw, :first_name ).should == 'DESC' end end context 'options' do context ':dir' do it 'locks in the direction of the param value' do helper.stub!(:params).and_return({order: 'first_name-asc'}) npw = NobelPrizeWinner.with_order(helper.params) helper.link_with_order( 'ASC', npw, :first_name, {dir: :asc} ).should == 'ASC' end end it 'passes other options to #link_to' do helper.stub!(:params).and_return({order: 'first_name-asc'}) npw = NobelPrizeWinner.with_order(helper.params) helper.link_with_order( 'ASC', npw, :first_name, {dir: :asc, remote: true} ).should == 'ASC' end end context '#current_order on the scope includes a :param_namespace' do it 'namespaces the :order param' do helper.stub!(:params).and_return({foo: {order: 'first_name-asc'}}) npw = NobelPrizeWinner.with_order(helper.params, {param_namespace: :foo}) helper.link_with_order( (npw.current_order[:field] == :first_name and npw.current_order[:dir] == :asc) ? 'DESC' : 'ASC', npw, :first_name ).should == 'DESC' end it 'works when no params are given' do helper.stub!(:params).and_return({}) npw = NobelPrizeWinner.with_order(helper.params, {param_namespace: :foo}) helper.link_with_order( (npw.current_order[:field] == :first_name and npw.current_order[:dir] == :asc) ? 'DESC' : 'ASC', npw, :first_name ).should == 'ASC' end end it 'accepts blocks' do helper.stub!(:params).and_return({order: 'first_name-asc'}) npw = NobelPrizeWinner.with_order(helper.params) output = helper.link_with_order(npw, :first_name, {remote: true}) do (npw.current_order[:field] == :first_name and npw.current_order[:dir] == :asc) ? 'DESC' : 'ASC' end output.should == 'DESC' end end end