Skip to content
Browse files

removed include code and cleaned up specs

  • Loading branch information...
1 parent 9fcd525 commit 67b5e01a9a1d1e26abc984db640fb283ca414383 @mynameisrufus committed Nov 20, 2011
View
44 README.rdoc
@@ -1,14 +1,17 @@
= sorted
-Sorted is a simple object that will take an sql order string and a url sort string to let you sort large datasets over many pages (using {will_paginate}[http://github.com/mislav/will_paginate]) without loosing state.
+Sorted is a simple object that will take an sql order string and a url
+sort string to let you sort large datasets over many pages (using
+{will_paginate}[http://github.com/mislav/will_paginatea] so
+{kaminari}[https://github.com/amatsuda/kaminari]) without loosing state.
-Using Rails master it will create a sorted scope and a two view helpers
+Using Rails 3x it will create a sorted scope and a two view helpers
== Example
=== Gemfile
- gem sorted
+ gem 'sorted' '~> 0.3.9'
=== View
@@ -26,19 +29,30 @@ or on the next page load when you then sort by something else....
This will initially sort by email ascending:
- @users = User.sorted(:order => "email ASC", :sort => params[:sort]).paginate(:page => params[:page])
+ @users = User.sorted(params[:sort], "email ASC").page(params[:page])
Or you can just clone the example app https://github.com/mynameisrufus/sorted_app.
If you want to sort by a belongs_to relationship, just provide sort order as "RELATIONS.COLUMN ASC|DESC" where
RELATIONS is the name of the relationship table and COLUMN is an attribute in that table. For example,
assuming the User model belongs_to a :company.
- @users = User.sorted(:order => "companies.name asc", :sort => params[:sort]).paginate(:page => params[:page])
+ @users = User.sorted(params[:sort], "companies.name ASC").page(params[:page])
+
+== Notes and typcasting and db function calls
+
+Do your type casting and function calls in your select statement with an
+alias to use Sorted, for example:
+
+ @users = User.select("income::BigInt as bg_income").sort(params[:sort], "bg_income ASC")
+
+or to call a db function:
+
+ @users = User.select("inet_aton(`ip_address`) AS in_ip").sort(params[:sort], "in_ip ASC")
== Presentation
-You might want to roll your own link_to_sorted method to use jQuery ui css classes for example, all you need is the sorted object.
+You might want to roll your own +link_to_sorted+ method to use jQuery ui css classes for example, all you need is the sorted object.
def link_to_sorted(name, order)
sorter = sorted(order)
@@ -53,26 +67,20 @@ You might want to roll your own link_to_sorted method to use jQuery ui css class
link_to(content_tag(:span, nil, {:class => css_class}) + name.to_s, sorter.params)
end
-Tables are best displayed with alternating shades for each row, so put this in your helper:
-
- def odd_even
- @odd_even ||= 0
- @odd_even += 1
- (@odd_even % 2) == 1 ? 'odd' : 'even'
- end
-
-Then in your table do something like this:
+Tables are best displayed with alternating shades for each row, so add
+an alternating class to you table rows using the rails +cycle+ method:
- <tr class="<%= odd_even %>">
+ <tr class="<%= cycle 'odd', 'even' %>">
== Rails 2.3.x
This gem works with rails 2.3.x but you will have to roll your own scope and view helper.
Here is the named scope for your model(s):
- named_scope :sorted, lambda { |params|
- { :order => Sorted::Sorter.new(params[:order], {:sort => params[:sort]}).to_sql }
+ named_scope :sorted, lambda { |sort, default_order|
+ { :order => Sorted::Sorter.new(sort, default_order).to_sql }
+ { :order => Sorted::Sorter.new(sort, default_order).to_sql }
}
and the the application helper methods:
View
41 lib/sorted/finders/active_record.rb
@@ -1,41 +0,0 @@
-require 'active_record'
-require 'sorted'
-
-module Sorted
- module Finders
- module ActiveRecord
- def self.enable!
- ::ActiveRecord::Base.class_eval do
- # Define a symbolic sort column. This allows mapping a simple column name
- # to a more complex sql order clause.
- # class Model < ActiveRecord::Base
- # symbolic_sort :ip_address, 'inet_aton(`ip_address`)'
- # end
- def self.symbolic_sort(key, sql)
- symbolic_sorts[key] = sql
- end
-
- def self.sorted(params)
- sorter = ::Sorted::Sorter.new(params[:order], :sort => params[:sort], :symbolic_sorts => symbolic_sorts)
- # Check if we parsed some relationship includes and apply them
- # before applying the order
- relation = self
- if sorter.includes.size > 0
- # Remove self.name from includes
- my_name = self.name.to_sym
- real_includes = sorter.includes.delete_if{|include_name| include_name == my_name}
- relation = includes(real_includes) if real_includes.size > 0
- end
- relation.order(sorter.to_sql)
- end
-
- private
-
- def self.symbolic_sorts
- @symbolic_sorts ||= {}
- end
- end
- end
- end
- end
-end
View
17 lib/sorted/orms/active_record.rb
@@ -0,0 +1,17 @@
+require 'active_record'
+require 'sorted'
+
+module Sorted
+ module Orms
+ module ActiveRecord
+ def self.enable!
+ ::ActiveRecord::Base.class_eval do
+ def self.sorted(sort, order = nil)
+ sorter = ::Sorted::Parser.new(sort, order)
+ order sorter.to_sql
+ end
+ end
+ end
+ end
+ end
+end
View
2 lib/sorted/railtie.rb
@@ -5,7 +5,7 @@ class Railtie < Rails::Railtie
initializer "sorted.active_record" do |app|
if defined? ::ActiveRecord
require 'sorted/finders/active_record'
- Sorted::Finders::ActiveRecord.enable!
+ Sorted::Orms::ActiveRecord.enable!
end
end
View
141 lib/sorted/sorter.rb
@@ -1,122 +1,87 @@
module Sorted
- class Sorter
- attr_reader :includes
- def initialize(order, params = nil)
- @includes = []
- @symbolic_sorts = {}
+ # call from a model with a defaut order:
+ #
+ # Sorted::Parser.new(sort, order)
+ # Sorted::Parser.new('name_asc!phone_desc', 'name ASC')
+ class Parser
+ attr_reader :sort, :order, :sorts, :orders
- if order.is_a?(String) || order.is_a?(Symbol)
- parse_order(order)
- end
- if params.is_a?(Hash)
- @params = params
- if @params[:sort].is_a?(String)
- parse_sort @params[:sort]
- end
- @symbolic_sorts = @params[:symbolic_sorts] || {}
- end
- end
-
- def parse_sort(sort_string)
- sort_string.split(/!/).each do |sort|
- if parsed = parse_query(sort)
- sorts << parsed
- end
- end
- end
-
- def parse_order(order_string_or_symbol)
- order_string_or_symbol.to_s.split(/,/).each do |order|
- if parsed = parse_sql(order)
- orders << parsed
- end
- end
- end
-
- def parse_query(sort)
- if m = sort.match(/([a-zA-Z0-9._]+)_(asc|desc)$/)
- parse_include(m[1])
- [m[1],m[2]]
- end
- end
-
- def parse_sql(order)
- if m = order.match(/(([a-zA-Z._:][a-zA-Z._:0-9]*)\s([asc|ASC|desc|DESC]+)|[a-zA-Z._:][a-zA-Z._:0-9]*)/)
- sort_column = (m[2].nil? ? m[1] : m[2])
- parse_include(sort_column)
- [sort_column,(m[3].nil? ? "asc" : m[3].downcase)]
- end
- end
+ # Regex to make sure we only get valid names and not injected code.
+ SORTED_QUERY_REGEX = /([a-zA-Z0-9._]+)_(asc|desc)$/
+ SQL_REGEX = /(([a-zA-Z._0-9]*)\s([asc|ASC|desc|DESC]+)|[a-zA-Z._0-9]*)/
- def parse_include(order)
- if match_data = /^([^\.]+)\..+/.match(order)
- include_name = match_data[1].singularize.to_sym
- @includes << include_name unless @includes.include?(include_name)
- end
+ def initialize(sort, order = nil)
+ @sort = sort
+ @order = order
+ @sorts = parse_sort
+ @orders = parse_order
end
- def toggle
- @array = Toggler.new(orders, sorts).to_a
- self
+ def parse_sort
+ sort.to_s.split(/!/).map do |sort_string|
+ if m = sort_string.match(SORTED_QUERY_REGEX)
+ [m[1], m[2]]
+ end
+ end.compact
end
- def reset
- @array = default
- self
+ def parse_order
+ order.to_s.split(/,/).map do |order_string|
+ if m = order_string.match(SQL_REGEX)
+ [m[2], (m[3].nil? ? "asc" : m[3].downcase)]
+ end
+ end.compact
end
-
+
def to_hash
array.inject({}){|h,a| h.merge(Hash[a[0],a[1]])}
end
def to_sql
- array.map do |a|
- column = @symbolic_sorts[a[0].to_sym] || a[0]
- "#{column} #{a[1].upcase}"
- end.join(', ')
+ array.map{|a| "#{a[0]} #{a[1].upcase}" }.join(', ')
end
def to_s
- array.map{|a| a.join('_')}.join('!')
+ array.map{|a| a.join('_') }.join('!')
end
def to_a
array
end
- def to_css
- if sorts.flatten.include?(orders[0][0])
- "sorted #{sorts.assoc(orders[0][0]).last}"
- else
- "sorted"
- end
+ # Toggle the the sort options.
+ #
+ # For example if the sort string is:
+ #
+ # 'name_asc'
+ #
+ # it will become:
+ #
+ # 'name_desc'
+ #
+ def toggle
+ @array = Toggler.new(orders, sorts).to_a
+ self
end
-
- def params
- @params ||= {}
- @params[:sort] = to_s
- @params
+
+ # Removes the toggle from the sort options
+ def reset
+ @array = default
+ self
end
- def orders
- @orders ||= []
- end
+ private
- def sorts
- @sorts ||= []
+ def array
+ @array ||= default
end
-
- private
+
def default
sorts_new = sorts.dup
- orders.each do |order|
- sorts_new << order unless sorts_new.flatten.include?(order[0])
+ orders.each do |o|
+ sorts_new << o unless sorts_new.flatten.include?(o[0])
end
sorts_new
end
-
- def array
- @array ||= default
- end
end
end
View
7 lib/sorted/toggler.rb
@@ -1,4 +1,11 @@
module Sorted
+ # takes parsed data and transposes it
+ #
+ # Sorted::Toggler.new(order, sort)
+ # Sorted::Toggler.new(
+ # [['name', 'asc'], ['phone', 'desc']],
+ # [['name', 'desc']]
+ # )
class Toggler
def initialize(orders, sorts)
@array = []
View
2 lib/sorted/version.rb
@@ -1,3 +1,3 @@
module Example
- VERSION = "0.3.8"
+ VERSION = "0.3.9"
end
View
25 lib/sorted/view_helpers/action_view.rb
@@ -4,12 +4,31 @@
module Sorted
module ViewHelpers
module ActionView
- def sorted(order)
- ::Sorted::Sorter.new(order, (request.get? && !params.nil?) ? params.dup : nil).toggle
+ class Sorted
+ attr_reader :params
+
+ def initialize(order, params)
+ sort = params.delete :sort
+ @order = order
+ @params = params
+ @parser = ::Sorted::Parser.new(sort, order).toggle
+ @params[:sort] = @parser.to_s
+ end
+
+
+
+
+ def to_css
+ if @parser.sorts.flatten.include? @parser.orders[0][0]
+ "sorted #{@parser.sorts.assoc(@parser.orders[0][0]).last}"
+ else
+ "sorted"
+ end
+ end
end
def link_to_sorted(name, order, options = {})
- sorter = sorted(order)
+ sorter = Sorted.new(order, ((request.get? && !params.nil?) ? params.dup : nil))
options[:class] = [options[:class], sorter.to_css].join(' ').strip
link_to(name.to_s, sorter.params, options)
end
View
2 sorted.gemspec
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = "sorted"
s.add_development_dependency "bundler", ">= 1.0.0"
- s.add_development_dependency "rails", ">= 3.0.0"
+ s.add_development_dependency "rails", ">= 3.1.2"
s.add_development_dependency "rspec", ">= 2.0.0"
View
12 spec/sorted/orms/active_record_spec.rb
@@ -0,0 +1,12 @@
+require 'spec_helper'
+require 'active_record'
+
+describe Sorted::Orms::ActiveRecord do
+ before(:each) do
+ Sorted::Orms::ActiveRecord.enable!
+ end
+
+ it "should integrate with ActiveRecord::Base" do
+ ActiveRecord::Base.should respond_to(:sorted)
+ end
+end
View
90 spec/sorted/sorter_spec.rb
@@ -0,0 +1,90 @@
+require 'spec_helper'
+
+describe Sorted::Parser, "params parsing" do
+ it "should not raise if pased nil arguments" do
+ lambda { Sorted::Parser.new(nil, nil).toggle }.should_not raise_error
+ end
+
+ it "should return a nice array from the order sql" do
+ sort = nil
+ order = "email ASC, phone ASC, name DESC"
+ result = [["email", "asc"], ["phone", "asc"], ["name", "desc"]]
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.orders.should eq result
+ end
+
+ it "should return a nice array from the sort params" do
+ sort = "email_desc!name_desc"
+ order = nil
+ result = [["email", "desc"], ["name", "desc"]]
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.sorts.should eq result
+ end
+
+ it "should combine sort and order params with sort params being of higer importance" do
+ sort = "email_desc!name_desc"
+ order = "email ASC, phone ASC, name DESC"
+ result = [["email", "desc"], ["name", "desc"], ["phone", "asc"]]
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.to_a.should eq result
+ end
+
+ it "should allow numbers, underscores and full stops in sort params" do
+ sort = "assessmentsTable.name_desc!users_300.name_5_desc"
+ order = nil
+ result = [["assessmentsTable.name", "desc"], ["users_300.name_5", "desc"]]
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.sorts.should eq result
+ end
+
+ it "should allow numbers, underscores and full stops in order params" do
+ sort = nil
+ order = "assessmentsTable.name ASC, users_300.name_5 ASC"
+ result = [["assessmentsTable.name", "asc"], ["users_300.name_5", "asc"]]
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.orders.should eq result
+ end
+
+ it "should default to asc if sort params order is ommited" do
+ sort = nil
+ order = :email
+ result = [["email", "asc"]]
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.orders.should eq result
+ end
+end
+
+describe Sorted::Parser, "return types" do
+ it "should return an sql sort string" do
+ sort = "email_desc!name_desc"
+ order = "email ASC, phone ASC, name DESC"
+ result = "email DESC, name DESC, phone ASC"
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.to_sql.should eq result
+ end
+
+ it "should return an hash" do
+ sort = "email_desc!name_desc"
+ order = "email ASC, phone ASC, name DESC"
+ result = {"email" => "desc", "name" => "desc", "phone" => "asc"}
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.to_hash.should eq result
+ end
+
+ it "should return an the encoded sort string" do
+ sort = "email_desc!name_desc"
+ order = "email ASC, phone ASC, name DESC"
+ result = "email_desc!name_desc!phone_asc"
+
+ sorter = Sorted::Parser.new(sort, order)
+ sorter.to_s.should eq result
+ end
+end
View
48 spec/sorted/toggler_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe Sorted::Toggler do
+ it "should bring phone to first order importance but not toggle ascendance" do
+ orders = [["email", "asc"], ["phone", "asc"]]
+ sorts = [["phone", "asc"]]
+ result = [["phone", "asc"], ["email", "asc"]]
+
+ toggler = Sorted::Toggler.new(orders, sorts)
+ toggler.to_a.should eq result
+ end
+
+ it "should toggle ascendance of email" do
+ orders = [["email", "asc"], ["phone", "asc"]]
+ sorts = [["email", "asc"]]
+ result = [["email", "desc"], ["phone", "asc"]]
+
+ toggler = Sorted::Toggler.new(orders, sorts)
+ toggler.to_a.should eq result
+ end
+
+ it "should return both order params un-toggled with no sort param" do
+ orders = [["email", "asc"], ["phone", "asc"]]
+ sorts = []
+ result = [["email", "asc"], ["phone", "asc"]]
+
+ toggler = Sorted::Toggler.new(orders, sorts)
+ toggler.to_a.should eq result
+ end
+
+ it "should toggle the email ascendance" do
+ orders = [["email", "asc"]]
+ sorts = [["email", "asc"], ["phone", "asc"]]
+ result = [["email", "desc"], ["phone", "asc"]]
+
+ toggler = Sorted::Toggler.new(orders, sorts)
+ toggler.to_a.should eq result
+ end
+
+ it "should toggle the email ascendance" do
+ orders = [["email", "desc"]]
+ sorts = [["email", "asc"], ["phone", "asc"]]
+ result = [["email", "desc"], ["phone", "asc"]]
+
+ toggler = Sorted::Toggler.new(orders, sorts)
+ toggler.to_a.should eq result
+ end
+end
View
101 spec/sorted_spec.rb → spec/sorted/view_helpers/action_view_spec.rb
@@ -1,82 +1,43 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'spec_helper'
require 'action_controller'
-require 'sorted'
-describe Sorted::Finders::ActiveRecord do
- before(:each) do
- Sorted::Finders::ActiveRecord.enable!
- end
-
- it "should integrate with ActiveRecord::Base" do
- ActiveRecord::Base.should respond_to(:sorted)
- end
-
- it "should define a symbolic_sorts method" do
- ActiveRecord::Base.should respond_to(:symbolic_sort)
- end
-
- it "should define symbolic_sorts" do
- a = Class.new(ActiveRecord::Base)
- b = Class.new(ActiveRecord::Base)
-
- a.symbolic_sort(:foo, 'foo')
- b.symbolic_sort(:bar, 'bar')
- a.instance_variable_get(:@symbolic_sorts).should == {:foo => 'foo'}
- b.instance_variable_get(:@symbolic_sorts).should == {:bar => 'bar'}
- end
-
- it "should add orders to the relation" do
- a = Class.new(ActiveRecord::Base)
- relation = a.sorted(:order => nil, :sort => 'a_asc')
- relation.order_values.should == ["a ASC"]
- end
+describe Sorted::ViewHelpers::ActionView do
+ class TestController
+ def params
+ @params ||= {}
+ end
- it "should add orders to the relation using symbolic_sorts" do
- a = Class.new(ActiveRecord::Base)
- a.symbolic_sort(:ip, 'inet_aton(`ip`)')
- relation = a.sorted(:order => nil, :sort => 'ip_asc')
- relation.order_values.should == ["inet_aton(`ip`) ASC"]
- end
-end
+ def params=(params)
+ @params = params
+ end
-describe Sorted::ViewHelpers::ActionView do
- before(:each) do
- class TestController
- def params
- @params ||= {}
- end
-
- def params=(params)
- @params = params
- end
-
- def request
- Request.new
- end
-
- def _prefixes
- end
+ def request
+ Request.new
end
- class Request
- def get?; true end
+
+ def _prefixes
end
- @controller = TestController.new
- ActionView::Base.send(:include, Sorted::ViewHelpers::ActionView)
end
-
- it "should integrate with ActionView::Base" do
- ActionView::Base.new.should respond_to(:sorted)
+ class Request
+ def get?; true end
end
+ ActionView::Base.send(:include, Sorted::ViewHelpers::ActionView)
+
it "should integrate with ActionView::Base" do
ActionView::Base.new.should respond_to(:link_to_sorted)
end
+end
+describe Sorted::ViewHelpers::ActionView::Sorted do
it "should not change the direction of name using view helper" do
- @controller.params = {:sort => "name_desc!email_asc"}
- sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
- sorter.toggle.to_a.should == [["email", "asc"], ["name", "desc"]]
+ order = :email
+ params = { :page => 10 }
+ result = { :page => 10, :sort => "email_asc" }
+
+ sorter = Sorted::ViewHelpers::ActionView::Sorted.new order, params
+ sorter.params.should eq result
end
it "should reverse email direction using view helper" do
@@ -120,3 +81,15 @@ def get?; true end
sorter.params.should == {:sort => "email_asc"}
end
end
+
+describe Sorted::Parser, "to_css" do
+ it "should return css base class for to_css if not in sort params" do
+ sorter = Sorted::Sorter.new(:email)
+ sorter.to_css.should == "sorted"
+ end
+
+ it "should return css class for to_css" do
+ sorter = Sorted::Sorter.new(:email, {:sort => "email_desc"})
+ sorter.to_css.should == "sorted desc"
+ end
+end
View
65 spec/sorter_spec.rb
@@ -1,65 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-require 'action_controller'
-require 'sorted'
-
-describe Sorted::Sorter, "parse methods" do
- it "should not die if pased dumb things" do
- lambda { Sorted::Sorter.new(false, Time.now).should_not }.should_not raise_error
- end
-
- it "should return a nice array from the order sql" do
- sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
- sorter.orders.should == [["email", "asc"], ["phone", "asc"], ["name", "desc"]]
- end
-
- it "should return a nice array from the sort params" do
- sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
- sorter.sorts.should == [["email", "desc"], ["name", "desc"]]
- end
-
- it "should allow numbers, underscores, full stops and colons in" do
- sorter = Sorted::Sorter.new('users.email ASC, users.phone_number DESC, assessments.name ASC, assessments.number_as_string::BigInt, assessments.column_with_number_123', {:sort => "users.email_desc!users.first_name_desc"})
- sorter.to_sql.should == "users.email DESC, users.first_name DESC, users.phone_number DESC, assessments.name ASC, assessments.number_as_string::BigInt ASC, assessments.column_with_number_123 ASC"
- sorter.includes.size.should == 2
- end
-end
-
-describe Sorted::Sorter, "logic:" do
- it "should not toggle the sort order and include any sql orders not in sort params" do
- sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
- sorter.to_a.should == [["email", "desc"], ["name", "desc"], ["phone", "asc"]]
- sorter.includes.size.should == 0
- end
-
- it "should return an sql sort string" do
- Sorted::Sorter.new(:email).to_sql.should == "email ASC"
- Sorted::Sorter.new(:email, {:sort => "name_desc!email_desc"}).to_sql.should == "name DESC, email DESC"
- end
-
- it "should handle a large initial order string" do
- sorter = Sorted::Sorter.new('email ASC, name DESC, phone ASC', {:sort => "email_desc!name_desc"})
- sorter.to_sql.should == "email DESC, name DESC, phone ASC"
- end
-
- it "should handle a large initial order string" do
- sorter = Sorted::Sorter.new('email ASC, phone DESC, name ASC', {:sort => "email_desc!name_desc"})
- sorter.to_sql.should == "email DESC, name DESC, phone DESC"
- end
-
- it "should substitute symbolic sorts" do
- sorter = Sorted::Sorter.new('ip_address', :symbolic_sorts => {:ip_address => 'inet_aton(`ip_address`)'})
- sorter.to_sql.should == "inet_aton(`ip_address`) ASC"
- end
-end
-
-describe Sorted::Sorter, "to_css" do
- it "should return css base class for to_css if not in sort params" do
- sorter = Sorted::Sorter.new(:email)
- sorter.to_css.should == "sorted"
- end
-
- it "should return css class for to_css" do
- sorter = Sorted::Sorter.new(:email, {:sort => "email_desc"})
- sorter.to_css.should == "sorted desc"
- end
-end
View
3 spec/spec_helper.rb
@@ -1,6 +1,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'sorted/finders/active_record'
+require 'sorted'
+require 'sorted/orms/active_record'
require 'sorted/view_helpers/action_view'
require 'rspec'
require 'rspec/autorun'
View
39 spec/toggler_spec.rb
@@ -1,39 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-require 'action_controller'
-require 'sorted'
-
-describe Sorted::Toggler, "toggle sorts" do
- it "should should fail becasue the sort order is incorect" do
- sorter = Sorted::Sorter.new(:jsci_complete, {:sort => "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"})
- sorter.toggle
- sorter.to_s.should_not == "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"
- end
-
- it "should toggle two order params at once" do
- first = Sorted::Sorter.new("email ASC, phone ASC")
- first.toggle.to_a.should == [["email", "asc"],["phone", "asc"]]
- second = Sorted::Sorter.new(:name, {:sort => first.to_s})
- second.toggle.to_a.should == [["name", "asc"], ["email", "asc"],["phone", "asc"]]
- third = Sorted::Sorter.new("email ASC, phone ASC", {:sort => second.to_s})
- third.toggle.to_a.should == [["email", "asc"],["phone", "asc"], ["name", "asc"]]
- end
-end
-
-describe Sorted::Toggler, "add remaining orders" do
- it "should toggle the sort order and include any sql orders not in sort params" do
- sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
- sorter.toggle.to_a.should == [["email", "desc"], ["name", "desc"], ["phone", "asc"]]
- end
-
- it "should toggle the sort order and include any sql orders not in sort params" do
- sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "mobile_asc!email_desc!phone_asc!name_desc"})
- sorter.toggle.to_a.should == [["email", "desc"], ["phone", "asc"], ["name", "desc"], ["mobile", "asc"]]
- end
-end
-
-describe Sorted::Toggler, "add remaining sorts" do
- it "should toggle the sort order and include any sort orders not in order params" do
- sorter = Sorted::Sorter.new("email DESC", {:sort => "email_desc!name_desc"})
- sorter.toggle.to_a.should == [["email", "asc"], ["name", "desc"]]
- end
-end

0 comments on commit 67b5e01

Please sign in to comment.
Something went wrong with that request. Please try again.