sort_order 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ Scenario: Sort Order
2
+ Given the following tokens: users.first_name, users.last_name asc, users.email 0
3
+
4
+ When I output each as sql
5
+ Then I should get the following list: users.first_name ASC, users.last_name ASC, users.email DESC
6
+
7
+ When I output the columns as sql
8
+ Then I should get the following string: users.first_name ASC, users.last_name ASC, users.email DESC
9
+
10
+ When I output the columns as sql (current only)
11
+ Then I should get the following string: users.first_name ASC
12
+
13
+ When I output each as a parameter
14
+ Then I should get the following list: first_name 1, last_name 1, email 0
15
+
16
+ When I output the columns as parameters
17
+ Then I should get the following hash: {:order=>["first_name 1", "last_name 1", "email 0"]}
18
+
19
+ When I output the columns as parameters (current only)
20
+ Then I should get the following hash: {:order=>"first_name 1"}
21
+
22
+ Scenario: Flipping Sort Orders!!!!
23
+ Given the following tokens: users.first_name, users.last_name asc, users.email 0
24
+
25
+ When I flip the current column
26
+ And I output the columns as sql (current only)
27
+ Then I should get the following string: users.first_name DESC
28
+
29
+ When I set the current column to: last_name 0
30
+ And I output the columns as sql (current only)
31
+ Then I should get the following string: users.last_name DESC
32
+
33
+
34
+
35
+
36
+
37
+
@@ -0,0 +1,50 @@
1
+ Given /^the following tokens: (.+)$/ do |tokens|
2
+ @sort_order = SortOrder.new(*tokens.split(', '))
3
+ end
4
+
5
+
6
+ When /^I output (.+) as (.+)$/ do |item, format|
7
+ @result = case [item, format]
8
+ when ['each', 'a parameter']
9
+ @sort_order.map(&:to_param)
10
+ when ['each', 'sql']
11
+ @sort_order.map(&:to_sql)
12
+ when ['the columns', 'sql']
13
+ @sort_order.to_sql
14
+ when ['the columns', 'sql (current only)']
15
+ @sort_order.to_sql(:current => true)
16
+ when ['the columns', 'parameters']
17
+ @sort_order.to_params
18
+ when ['the columns', 'parameters (current only)']
19
+ @sort_order.to_params(:current => true)
20
+ end
21
+ raise "No list set!" if @result.blank?
22
+ end
23
+
24
+ When /^I flip the current column$/ do
25
+ @sort_order.current.flip!
26
+ end
27
+
28
+
29
+ When /^I set the current column to: (.+)$/ do |token|
30
+ @sort_order.current = token
31
+ end
32
+
33
+
34
+
35
+ Then /^I should get the following list: (.+)$/ do |tokens|
36
+ @result.should == tokens.split(', ')
37
+ end
38
+
39
+ Then /^I should get the following string: (.+)$/ do |tokens|
40
+ @result.should == tokens
41
+ end
42
+
43
+ Then /^I should get the following hash$/ do |tokens|
44
+ @result.should == tokens.split(', ')
45
+ end
46
+
47
+ Then /^I should get the following hash: (.+)$/ do |hash|
48
+ @result.inspect.should == hash
49
+ end
50
+
@@ -0,0 +1 @@
1
+ require File.expand_path( File.join( File.dirname(__FILE__), '..', '..', 'init' ) )
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ $:.unshift(File.expand_path('lib', File.dirname(__FILE__)))
2
+ $:.unshift(File.dirname(__FILE__))
3
+ require 'active_support'
4
+ require 'sort_order'
@@ -0,0 +1,31 @@
1
+ module SortOrder
2
+ class Column
3
+ class Direction < String
4
+
5
+ def self.parse(value)
6
+ new((value.to_s.upcase == 'DESC' || value.to_s == '0') ? 'DESC' : 'ASC')
7
+ end
8
+
9
+ def label
10
+ "#{titleize}ending"
11
+ end
12
+
13
+ def asc?
14
+ self == 'ASC'
15
+ end
16
+
17
+ def desc?
18
+ !asc?
19
+ end
20
+
21
+ def to_param(flip=false)
22
+ (flip ? desc? : asc?) ? '1' : '0'
23
+ end
24
+
25
+ def to_sql(flip=false)
26
+ (flip ? desc? : asc?) ? 'ASC' : 'DESC'
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module SortOrder
2
+ class Column
3
+ class Field < String
4
+
5
+ def table
6
+ split('.').first || ''
7
+ end
8
+
9
+ def name
10
+ split('.').last
11
+ end
12
+
13
+ def label
14
+ name.titleize
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ module SortOrder
2
+ class Column
3
+
4
+ attr_accessor :field, :direction
5
+
6
+ def self.parse(token)
7
+ token.is_a?(SortOrder::Column) ? token : new(*token.to_s.split(' '))
8
+ end
9
+
10
+ def initialize(field, direction=nil)
11
+ @field, @direction = Field.new(field), Direction.parse(direction)
12
+ end
13
+
14
+ def label
15
+ field.label
16
+ end
17
+
18
+ def name
19
+ field.name
20
+ end
21
+
22
+ def to_sql(options = {})
23
+ "#{field} #{direction.to_sql(options[:flip])}"
24
+ end
25
+
26
+ def to_param(options = {})
27
+ "#{field.name} #{direction.to_param(options[:flip])}"
28
+ end
29
+
30
+ def direction=(value)
31
+ @direction = Direction.parse(value)
32
+ end
33
+
34
+ def flip!
35
+ self.direction = direction.to_param(true)
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,62 @@
1
+ module SortOrder
2
+ class Columns < Array
3
+
4
+ def initialize(*defaults)
5
+ super defaults.map { |d| Column.parse(d) }
6
+ raise SortOrder::NoDefaultColumnsError, "Please supply some default columns e.g. 'name', 'email desc', etc." if empty?
7
+ end
8
+
9
+ def to_sql(options={})
10
+ options.delete(:current) ? current.to_sql(options) : map {|c| c.to_sql(options) }.join(', ')
11
+ end
12
+
13
+ def to_params(options={})
14
+ { :order => options.delete(:current) ? current.to_param(options) : map {|c| c.to_param(options) } }
15
+ end
16
+
17
+ def current
18
+ @current ||= first
19
+ end
20
+
21
+ def current=(token)
22
+ @current = merge(token)
23
+ end
24
+
25
+ def current?(other)
26
+ current == other
27
+ end
28
+
29
+ def column(token)
30
+ SortOrder::Column.parse(token)
31
+ end
32
+
33
+ def fields
34
+ map(&:field)
35
+ end
36
+
37
+ def <<(token)
38
+ merge token
39
+ return self
40
+ end
41
+
42
+ def merge(token)
43
+ new_col = column(token)
44
+ if old_col = find(new_col.name)
45
+ old_col.direction = new_col.direction
46
+ return old_col
47
+ else
48
+ self << new_col
49
+ return new_col
50
+ end
51
+ end
52
+
53
+ def find(name)
54
+ super() {|c| c.name == name }
55
+ end
56
+
57
+ def include?(name)
58
+ find(name).present?
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,32 @@
1
+ module SortOrder
2
+
3
+ module ControllerHelper
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def default_sort_order(*args)
10
+ if args.any?
11
+ @@default_sort_order ||= {}
12
+ @@default_sort_order[name] = args
13
+ include InstanceMethods unless included_modules.include?(SortOrder::ControllerHelper::InstanceMethods)
14
+ helper_method :sort_order
15
+ end
16
+ @@default_sort_order[name]
17
+ end
18
+ end
19
+
20
+ module InstanceMethods
21
+
22
+ def sort_order
23
+ @sort_order ||= SortOrder.new *self.class.default_sort_order
24
+ end
25
+
26
+ private :sort_order
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ ActionController::Base.send :include, SortOrder::ControllerHelper
@@ -0,0 +1,15 @@
1
+ module SortOrder
2
+ module ViewHelper
3
+
4
+ def link_to_order_by(name, label=nil)
5
+ return "No call to default_sort_order in #{controller.controller_name} " unless respond_to?(:sort_order)
6
+ column = sort_order.find(name.to_s)
7
+ column = sort_order.current if sort_order.current?(column)
8
+ label ||= column.label
9
+ link_to label, {:order => column.to_param(:flip => true)}
10
+ end
11
+
12
+ end
13
+ end
14
+
15
+ ActionView::Base.send :include, SortOrder::ViewHelper
data/lib/sort_order.rb ADDED
@@ -0,0 +1,17 @@
1
+ module SortOrder
2
+ VERSION = '0.1.1'
3
+
4
+ def self.new *cols
5
+ Columns.new *cols
6
+ end
7
+
8
+ class NoDefaultColumnsError < StandardError; end
9
+
10
+ end
11
+
12
+ require 'sort_order/column'
13
+ require 'sort_order/columns'
14
+ require 'sort_order/column/field'
15
+ require 'sort_order/column/direction'
16
+ require 'sort_order/view_helper' if defined?(ActionView::Base)
17
+ require 'sort_order/controller_helper' if defined?(ActionController::Base)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../init')
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sort_order
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Alex Neill
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-21 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Manipulate column sort orders...
22
+ email: alex@rawnet.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - features/sort_order.feature
31
+ - features/step_definitions/sort_order_steps.rb
32
+ - features/support/env.rb
33
+ - init.rb
34
+ - lib/sort_order.rb
35
+ - lib/sort_order/column.rb
36
+ - lib/sort_order/column/direction.rb
37
+ - lib/sort_order/column/field.rb
38
+ - lib/sort_order/columns.rb
39
+ - lib/sort_order/controller_helper.rb
40
+ - lib/sort_order/view_helper.rb
41
+ - rails/init.rb
42
+ has_rdoc: true
43
+ homepage:
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: sort_order
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Manipulate column sort orders.
72
+ test_files: []
73
+