asc_desc 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -1
- data/README.md +21 -3
- data/asc_desc.gemspec +3 -1
- data/lib/asc_desc/model_additions.rb +19 -2
- data/lib/asc_desc/version.rb +2 -2
- data/spec/asc_desc/model_additions_spec.rb +64 -4
- data/spec/asc_desc_spec.rb +2 -2
- metadata +24 -13
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -17,7 +17,7 @@ It was conducted on the basis of the excellent tutorials from Ryan Bates, namely
|
|
17
17
|
Add to your Gemfile and run the `bundle` command to install it.
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
gem 'asc_desc'
|
20
|
+
gem 'asc_desc'
|
21
21
|
```
|
22
22
|
|
23
23
|
|
@@ -29,6 +29,8 @@ Add to your Gemfile and run the `bundle` command to install it.
|
|
29
29
|
|
30
30
|
## Usage
|
31
31
|
|
32
|
+
### asc
|
33
|
+
|
32
34
|
Call `asc` on an ActiveRecord object or on an ActiveRecord::Relation object to sort the column(s) in an ascending way.
|
33
35
|
|
34
36
|
```ruby
|
@@ -41,12 +43,16 @@ Call `asc` on an ActiveRecord object or on an ActiveRecord::Relation object to s
|
|
41
43
|
# using multiple parameters to specify more than one column for the sort clause
|
42
44
|
Candy.where(:sugar => true).asc(:classification, :name)
|
43
45
|
|
44
|
-
# using an array to pass mutliple
|
46
|
+
# using an array to pass mutliple arguments
|
45
47
|
Candy.where(:sugar => true).asc([:classification, :name])
|
46
48
|
|
47
49
|
# using a string to specify more than one column for the sort clause
|
48
50
|
Candy.where(:sugar => true).asc('classification, name')
|
49
51
|
|
52
|
+
# without argument, the method generates a sort by id (custom primary key supported)
|
53
|
+
# SELECT "candies".* FROM "candies" ORDER BY id ASC
|
54
|
+
Candy.where(:sugar => true).asc
|
55
|
+
|
50
56
|
# the method is chainable
|
51
57
|
Candy.where(:sugar => true).asc(:classification).asc(:name)
|
52
58
|
```
|
@@ -54,6 +60,8 @@ Call `asc` on an ActiveRecord object or on an ActiveRecord::Relation object to s
|
|
54
60
|
`ascending` and `ascending_order` are aliases of `asc`.
|
55
61
|
|
56
62
|
|
63
|
+
### desc
|
64
|
+
|
57
65
|
Call `desc` on an ActiveRecord object or on an ActiveRecord::Relation object to sort the column(s) in a descending way.
|
58
66
|
|
59
67
|
```ruby
|
@@ -66,12 +74,16 @@ Call `desc` on an ActiveRecord object or on an ActiveRecord::Relation object to
|
|
66
74
|
# using multiple parameters to specify more than one column for the sort clause
|
67
75
|
Candy.where(:sugar => true).desc(:classification, :name)
|
68
76
|
|
69
|
-
# using an array to pass mutliple
|
77
|
+
# using an array to pass mutliple arguments
|
70
78
|
Candy.where(:sugar => true).desc([:classification, :name])
|
71
79
|
|
72
80
|
# using a string to specify more than one column for the sort clause
|
73
81
|
Candy.where(:sugar => true).desc('classification, name')
|
74
82
|
|
83
|
+
# without argument, the method generates a sort by id (custom primary key supported)
|
84
|
+
# SELECT "candies".* FROM "candies" ORDER BY id DESC
|
85
|
+
Candy.where(:sugar => true).desc
|
86
|
+
|
75
87
|
# the method is chainable
|
76
88
|
Candy.where(:sugar => true).desc(:classification).desc(:name)
|
77
89
|
```
|
@@ -79,6 +91,12 @@ Call `desc` on an ActiveRecord object or on an ActiveRecord::Relation object to
|
|
79
91
|
`descending` and `descending_order` are aliases of `desc`.
|
80
92
|
|
81
93
|
|
94
|
+
### order_by
|
95
|
+
|
96
|
+
The current version of this gem introduces a new method called `order_by`. At this time, it's just an alias of the
|
97
|
+
built-in `order` method but will be extended in future versions of the library.
|
98
|
+
|
99
|
+
|
82
100
|
## Development
|
83
101
|
|
84
102
|
Questions or problems? Please post them on the [issue tracker](https://github.com/dwerlen/asc_desc/issues).
|
data/asc_desc.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ['werlen@icare.ch']
|
10
10
|
s.homepage = 'http://github.com/dwerlen/asc_desc'
|
11
11
|
s.summary = %q{add "asc" and "desc" methods to ActiveRecord}
|
12
|
-
s.description = %q{This gem adds two new methods to ActiveRecord (and ActiveRecord::Relation) that
|
12
|
+
s.description = %q{This gem adds two new methods to ActiveRecord (and ActiveRecord::Relation) that allows to sort SQL queries without using the "ASC" and "DESC" SQL keywords.}
|
13
13
|
|
14
14
|
s.rubyforge_project = 'asc_desc'
|
15
15
|
|
@@ -18,6 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ['lib']
|
20
20
|
|
21
|
+
s.add_runtime_dependency 'activerecord', '>= 3.0.0'
|
22
|
+
|
21
23
|
s.add_development_dependency 'rake'
|
22
24
|
s.add_development_dependency 'rspec'
|
23
25
|
s.add_development_dependency 'with_model'
|
@@ -19,11 +19,15 @@ module AscDesc
|
|
19
19
|
# using a string to specify more than one column for the sort clause
|
20
20
|
# Candy.where(:sugar => true).asc('classification, name')
|
21
21
|
#
|
22
|
+
# without argument, the method generates a sort by id (custom primary key supported)
|
23
|
+
# SELECT "candies".* FROM "candies" ORDER BY id ASC
|
24
|
+
# Candy.where(:sugar => true).asc
|
25
|
+
#
|
22
26
|
# the method is chainable
|
23
27
|
# Candy.where(:sugar => true).asc(:classification).asc(:name)
|
24
28
|
#
|
25
29
|
def asc(*args)
|
26
|
-
self.order AscDesc.format_order_clause(*args << AscDesc::ASC)
|
30
|
+
self.order AscDesc.format_order_clause(*(args.presence || [self.primary_key]) << AscDesc::ASC)
|
27
31
|
end
|
28
32
|
alias :ascending :asc
|
29
33
|
alias :ascending_order :asc
|
@@ -47,14 +51,27 @@ module AscDesc
|
|
47
51
|
# using a string to specify more than one column for the sort clause
|
48
52
|
# Candy.where(:sugar => true).desc('classification, name')
|
49
53
|
#
|
54
|
+
# without argument, the method generates a sort by id (custom primary key supported)
|
55
|
+
# SELECT "candies".* FROM "candies" ORDER BY id DESC
|
56
|
+
# Candy.where(:sugar => true).desc
|
57
|
+
#
|
50
58
|
# the method is chainable
|
51
59
|
# Candy.where(:sugar => true).desc(:classification).desc(:name)
|
52
60
|
#
|
53
61
|
def desc(*args)
|
54
|
-
self.order AscDesc.format_order_clause(*args << AscDesc::DESC)
|
62
|
+
self.order AscDesc.format_order_clause(*(args.presence || [self.primary_key]) << AscDesc::DESC)
|
55
63
|
end
|
56
64
|
alias :descending :desc
|
57
65
|
alias :descending_order :desc
|
58
66
|
|
67
|
+
|
68
|
+
# alias of the built-in `order` method.
|
69
|
+
#
|
70
|
+
# Will be extended in future versions of the library.
|
71
|
+
#
|
72
|
+
def order_by(*args)
|
73
|
+
self.order(*args)
|
74
|
+
end
|
75
|
+
|
59
76
|
end
|
60
77
|
end
|
data/lib/asc_desc/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module AscDesc
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
2
|
+
VERSION = '0.0.2'
|
3
|
+
end
|
@@ -16,6 +16,21 @@ describe AscDesc::ModelAdditions do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
with_model :Bonbon do
|
20
|
+
# The table block works just like a migration.
|
21
|
+
table :primary_key => :id_bonbon do |t|
|
22
|
+
t.string :name
|
23
|
+
t.string :classification
|
24
|
+
t.boolean :sugar
|
25
|
+
t.timestamps
|
26
|
+
end
|
27
|
+
# The model block works just like the class definition.
|
28
|
+
model do
|
29
|
+
extend AscDesc::ModelAdditions
|
30
|
+
self.primary_key = :id_bonbon
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
19
34
|
it 'respond to a class method named "asc"' do
|
20
35
|
Candy.respond_to?(:asc).should be_true
|
21
36
|
Candy.respond_to?(:ascending).should be_true
|
@@ -28,6 +43,10 @@ describe AscDesc::ModelAdditions do
|
|
28
43
|
Candy.respond_to?(:descending_order).should be_true
|
29
44
|
end
|
30
45
|
|
46
|
+
it 'respond to a class method named "order_by"' do
|
47
|
+
Candy.respond_to?(:order_by).should be_true
|
48
|
+
end
|
49
|
+
|
31
50
|
describe '.asc' do
|
32
51
|
|
33
52
|
it 'should return an ActiveRecord Relation' do
|
@@ -53,7 +72,7 @@ describe AscDesc::ModelAdditions do
|
|
53
72
|
candies.to_sql.should be_end_with('ORDER BY classification ASC, name ASC')
|
54
73
|
end
|
55
74
|
|
56
|
-
it 'can format an array as mutliple
|
75
|
+
it 'can format an array as mutliple arguments' do
|
57
76
|
candies = Candy.asc([:classification, :name])
|
58
77
|
candies.to_sql.should be_end_with('ORDER BY classification ASC, name ASC')
|
59
78
|
candies = Candy.asc([:classification, :name], :sugar)
|
@@ -73,11 +92,22 @@ describe AscDesc::ModelAdditions do
|
|
73
92
|
candies.to_sql.should be_end_with('ORDER BY classification ASC, name ASC, sugar ASC')
|
74
93
|
end
|
75
94
|
|
95
|
+
it 'generates a sort by id when no argument is passed' do
|
96
|
+
candies = Candy.asc
|
97
|
+
candies.to_sql.should be_end_with('ORDER BY id ASC')
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'generates a sort by id when no argument is passed even with a custom primary key' do
|
101
|
+
bonbons = Bonbon.asc
|
102
|
+
bonbons.to_sql.should be_end_with('ORDER BY id_bonbon ASC')
|
103
|
+
end
|
104
|
+
|
76
105
|
it 'should be chainable' do
|
77
106
|
candies = Candy.asc(:name)
|
107
|
+
candies = candies.asc
|
78
108
|
candies = candies.asc(:sugar)
|
79
109
|
candies.should be_an_instance_of(ActiveRecord::Relation)
|
80
|
-
candies.to_sql.should be_end_with('ORDER BY name ASC, sugar ASC')
|
110
|
+
candies.to_sql.should be_end_with('ORDER BY name ASC, id ASC, sugar ASC')
|
81
111
|
end
|
82
112
|
|
83
113
|
end
|
@@ -107,7 +137,7 @@ describe AscDesc::ModelAdditions do
|
|
107
137
|
candies.to_sql.should be_end_with('ORDER BY classification DESC, name DESC')
|
108
138
|
end
|
109
139
|
|
110
|
-
it 'can format an array as mutliple
|
140
|
+
it 'can format an array as mutliple arguments' do
|
111
141
|
candies = Candy.desc([:classification, :name])
|
112
142
|
candies.to_sql.should be_end_with('ORDER BY classification DESC, name DESC')
|
113
143
|
candies = Candy.desc([:classification, :name], :sugar)
|
@@ -127,11 +157,41 @@ describe AscDesc::ModelAdditions do
|
|
127
157
|
candies.to_sql.should be_end_with('ORDER BY classification DESC, name DESC, sugar DESC')
|
128
158
|
end
|
129
159
|
|
160
|
+
it 'generates a sort by id when no argument is passed' do
|
161
|
+
candies = Candy.desc
|
162
|
+
candies.to_sql.should be_end_with('ORDER BY id DESC')
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'generates a sort by id when no argument is passed even with a custom primary key' do
|
166
|
+
bonbons = Bonbon.desc
|
167
|
+
bonbons.to_sql.should be_end_with('ORDER BY id_bonbon DESC')
|
168
|
+
end
|
169
|
+
|
130
170
|
it 'should be chainable' do
|
131
171
|
candies = Candy.desc(:name)
|
172
|
+
candies = candies.desc
|
132
173
|
candies = candies.desc(:sugar)
|
133
174
|
candies.should be_an_instance_of(ActiveRecord::Relation)
|
134
|
-
candies.to_sql.should be_end_with('ORDER BY name DESC, sugar DESC')
|
175
|
+
candies.to_sql.should be_end_with('ORDER BY name DESC, id DESC, sugar DESC')
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
|
180
|
+
describe '.order_by' do
|
181
|
+
|
182
|
+
it 'should return an ActiveRecord Relation' do
|
183
|
+
candies = Candy.order_by(:name)
|
184
|
+
candies.should be_an_instance_of(ActiveRecord::Relation)
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'should define the "ORDER BY" clause of the underlying SQL query' do
|
188
|
+
candies = Candy.order_by('classification ASC, name DESC')
|
189
|
+
candies.to_sql.should be_end_with('ORDER BY classification ASC, name DESC')
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should accept multiple parameters' do
|
193
|
+
candies = Candy.order_by('classification DESC', 'name ASC')
|
194
|
+
candies.to_sql.should be_end_with('ORDER BY classification DESC, name ASC')
|
135
195
|
end
|
136
196
|
|
137
197
|
end
|
data/spec/asc_desc_spec.rb
CHANGED
@@ -35,7 +35,7 @@ describe AscDesc do
|
|
35
35
|
AscDesc.format_order_clause('classification', 'name', AscDesc::DESC).should eq('classification DESC, name DESC')
|
36
36
|
end
|
37
37
|
|
38
|
-
it 'can format an array as mutliple
|
38
|
+
it 'can format an array as mutliple arguments' do
|
39
39
|
AscDesc.format_order_clause([:classification, :name], AscDesc::ASC).should eq('classification ASC, name ASC')
|
40
40
|
AscDesc.format_order_clause([:classification, :name], AscDesc::DESC).should eq('classification DESC, name DESC')
|
41
41
|
AscDesc.format_order_clause([:classification, :name], :sugar, AscDesc::DESC).should eq('classification DESC, name DESC, sugar DESC')
|
@@ -44,7 +44,7 @@ describe AscDesc do
|
|
44
44
|
AscDesc.format_order_clause(['classification', 'name'], 'sugar', AscDesc::DESC).should eq('classification DESC, name DESC, sugar DESC')
|
45
45
|
end
|
46
46
|
|
47
|
-
it 'can
|
47
|
+
it 'can format a string to specify more than one column for the sort clause' do
|
48
48
|
AscDesc.format_order_clause('classification, name', :sugar, AscDesc::ASC).should eq('classification ASC, name ASC, sugar ASC')
|
49
49
|
AscDesc.format_order_clause(['classification, name', :sugar], AscDesc::ASC).should eq('classification ASC, name ASC, sugar ASC')
|
50
50
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asc_desc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-27 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70194618974440 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70194618974440
|
14
25
|
- !ruby/object:Gem::Dependency
|
15
26
|
name: rake
|
16
|
-
requirement: &
|
27
|
+
requirement: &70194618974020 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ! '>='
|
@@ -21,10 +32,10 @@ dependencies:
|
|
21
32
|
version: '0'
|
22
33
|
type: :development
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *70194618974020
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rspec
|
27
|
-
requirement: &
|
38
|
+
requirement: &70194618973560 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70194618973560
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: with_model
|
38
|
-
requirement: &
|
49
|
+
requirement: &70194618973140 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70194618973140
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: activerecord
|
49
|
-
requirement: &
|
60
|
+
requirement: &70194618972640 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ~>
|
@@ -54,10 +65,10 @@ dependencies:
|
|
54
65
|
version: 3.2.1
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70194618972640
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: sqlite3
|
60
|
-
requirement: &
|
71
|
+
requirement: &70194618972140 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ~>
|
@@ -65,9 +76,9 @@ dependencies:
|
|
65
76
|
version: 1.3.5
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70194618972140
|
69
80
|
description: This gem adds two new methods to ActiveRecord (and ActiveRecord::Relation)
|
70
|
-
that
|
81
|
+
that allows to sort SQL queries without using the "ASC" and "DESC" SQL keywords.
|
71
82
|
email:
|
72
83
|
- werlen@icare.ch
|
73
84
|
executables: []
|