sortable_table_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. require 'spec_helper'
  2. describe SortableTable do
  3. class SortableTestController
  4. attr_accessor :params
  5. def self.helper(foo)
  6. end
  7. include SortableTable
  8. public :set_table_sort
  9. public :table_sort
  10. end
  11. describe "#set_table_sort" do
  12. let(:controller) { SortableTestController.new }
  13. let(:default) { { column2: :asc }}
  14. let(:options) { { sorts: %w[column1 column2], default: default } }
  15. it "uses a default when no sort is given" do
  16. controller.params = {}
  17. controller.set_table_sort options
  18. controller.table_sort.should == default
  19. end
  20. it "applies the given sort when one is passed in" do
  21. controller.params = { sort: "column1.desc" }
  22. controller.set_table_sort options
  23. controller.table_sort.should == { column1: :desc }
  24. controller.params = { sort: "column1.asc" }
  25. controller.set_table_sort options
  26. controller.table_sort.should == { column1: :asc }
  27. controller.params = { sort: "column2.desc" }
  28. controller.set_table_sort options
  29. controller.table_sort.should == { column2: :desc }
  30. end
  31. it "ignores unknown directions" do
  32. controller.params = { sort: "column1.foo" }
  33. controller.set_table_sort options
  34. controller.table_sort.should == { column1: :asc }
  35. controller.params = { sort: "column1.foo drop tables" }
  36. controller.set_table_sort options
  37. controller.table_sort.should == { column1: :asc }
  38. end
  39. it "ignores unknown columns" do
  40. controller.params = { sort: "foo.asc" }
  41. controller.set_table_sort options
  42. controller.table_sort.should == default
  43. controller.params = { sort: ";drop table;.asc" }
  44. controller.set_table_sort options
  45. controller.table_sort.should == default
  46. end
  47. end
  48. end