asc_desc 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,3 +6,8 @@
6
6
 
7
7
  * generates a sort by id when no argument is passed (custom primary key supported)
8
8
  * adds "order_by" as an alias for the built-in "order" method (for future development)
9
+
10
+ ## v0.0.3
11
+
12
+ * adds dynamic order_by mechanism
13
+
data/README.md CHANGED
@@ -3,14 +3,6 @@
3
3
  This gem adds two new methods to ActiveRecord (and ActiveRecord::Relation) that allows to sort SQL queries without using
4
4
  the "ASC" and "DESC" SQL keywords.
5
5
 
6
- ## Remark
7
-
8
- `asc_desc` was developed in order to experiment the creation of a ruby gem.
9
- It was conducted on the basis of the excellent tutorials from Ryan Bates, namely:
10
-
11
- * ["#301 Extracting a Ruby Gem"](http://railscasts.com/episodes/301-extracting-a-ruby-gem)
12
- * ["#303 Publishing a Gem"](http://railscasts.com/episodes/303-publishing-a-gem)
13
-
14
6
 
15
7
  ## Installation
16
8
 
@@ -91,11 +83,35 @@ Call `desc` on an ActiveRecord object or on an ActiveRecord::Relation object to
91
83
  `descending` and `descending_order` are aliases of `desc`.
92
84
 
93
85
 
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
-
86
+ ### dynamic order_by
87
+
88
+ The **dynamic order_by mechanism** is built the same way as the dynamic finders present from the beginning of
89
+ Ruby On Rails and allows to easely write the order clause for ActiveRecord queries.
90
+
91
+ ````ruby
92
+ # a call to order_by just acts like the standard built-in "order" method (alias)
93
+ Candy.where(:sugar => true).order_by('classification ASC, name ASC')
94
+
95
+ # using a column as part of the method name
96
+ Candy.where(:sugar => true).order_by_name
97
+
98
+ # using a column and a sort direction (asc|desc) as part of the method name
99
+ Candy.where(:sugar => true).order_by_name_asc
100
+ Candy.where(:sugar => true).order_by_name_desc
101
+
102
+ # using multiple columns as part of the method name
103
+ Candy.where(:sugar => true).order_by_classification_and_name
104
+ Candy.where(:sugar => true).order_by_classification_and_name_and_sugar
105
+
106
+ # using multiple columns and sort directions (asc|desc) as part of the method name
107
+ Candy.where(:sugar => true).order_by_classification_asc_and_name_desc
108
+ Candy.where(:sugar => true).order_by_classification_desc_and_name_asc
109
+ Candy.where(:sugar => true).order_by_classification_asc_and_name_desc_and_sugar_asc
110
+
111
+ # using multiple columns with or without sort direction (asc|desc) as part of the method name
112
+ Candy.where(:sugar => true).order_by_classification_and_name_desc
113
+ Candy.where(:sugar => true).order_by_classification_desc_and_name
114
+ ```
99
115
 
100
116
  ## Development
101
117
 
@@ -104,3 +120,13 @@ You can contribute changes by forking the project and submitting a pull request.
104
120
  You can ensure the tests passing by running `bundle` and `rake`.
105
121
 
106
122
  This gem is created by David Werlen and is under the MIT License.
123
+
124
+
125
+ ## Remark
126
+
127
+ `asc_desc` was developed in order to experiment the creation of a ruby gem.
128
+ It was conducted on the basis of the excellent tutorials from Ryan Bates, namely:
129
+
130
+ * ["#301 Extracting a Ruby Gem"](http://railscasts.com/episodes/301-extracting-a-ruby-gem)
131
+ * ["#303 Publishing a Gem"](http://railscasts.com/episodes/303-publishing-a-gem)
132
+
@@ -65,13 +65,56 @@ module AscDesc
65
65
  alias :descending_order :desc
66
66
 
67
67
 
68
- # alias of the built-in `order` method.
69
- #
70
- # Will be extended in future versions of the library.
68
+ # a call to <tt>order_by</tt> just acts like the standard built-in "order" method (alias)
69
+ # Candy.where(:sugar => true).order_by('classification ASC, name ASC')
71
70
  #
72
71
  def order_by(*args)
73
72
  self.order(*args)
74
73
  end
75
74
 
75
+
76
+ private
77
+
78
+ # The **dynamic order_by mechanism** is built the same way as the dynamic finders present from the beginning of
79
+ # Ruby On Rails and allows to easely write the order clause for ActiveRecord queries.
80
+ #
81
+ # using a column as part of the method name
82
+ # Candy.where(:sugar => true).order_by_name
83
+ #
84
+ # using a column and a sort direction (asc|desc) as part of the method name
85
+ # Candy.where(:sugar => true).order_by_name_asc
86
+ # Candy.where(:sugar => true).order_by_name_desc
87
+ #
88
+ # using multiple columns as part of the method name
89
+ # Candy.where(:sugar => true).order_by_classification_and_name
90
+ # Candy.where(:sugar => true).order_by_classification_and_name_and_sugar
91
+ #
92
+ # using multiple columns and sort directions (asc|desc) as part of the method name
93
+ # Candy.where(:sugar => true).order_by_classification_asc_and_name_desc
94
+ # Candy.where(:sugar => true).order_by_classification_desc_and_name_asc
95
+ # Candy.where(:sugar => true).order_by_classification_asc_and_name_desc_and_sugar_asc
96
+ #
97
+ # using multiple columns with or without sort direction (asc|desc) as part of the method name
98
+ # Candy.where(:sugar => true).order_by_classification_and_name_desc
99
+ # Candy.where(:sugar => true).order_by_classification_desc_and_name
100
+ #
101
+ def method_missing(method_sym, *arguments, &block)
102
+ # the first argument is a Symbol, so we need to_s it if you want to match the pattern
103
+ if method_sym.to_s =~ /^order_by_([_a-zA-Z]\w*)$/
104
+ columns = $1.split('_and_').map do |column|
105
+ if column.ends_with?('_asc')
106
+ "#{column[0..-5]} ASC"
107
+ elsif column.ends_with?('_desc')
108
+ "#{column[0..-6]} DESC"
109
+ else
110
+ "#{column} ASC" # always use explicit sort direction
111
+ end
112
+ end
113
+ self.order(columns)
114
+ else
115
+ super
116
+ end
117
+ end
118
+
76
119
  end
77
120
  end
@@ -4,6 +4,8 @@ module AscDesc
4
4
  ActiveSupport.on_load :active_record do
5
5
  extend ModelAdditions
6
6
  end
7
+ # This is required to add <tt>method_missing</tt>.
8
+ ActiveRecord::Relation.send(:include, ModelAdditions)
7
9
  end
8
10
  end
9
11
  end
@@ -1,3 +1,3 @@
1
1
  module AscDesc
2
- VERSION = '0.0.2'
3
- end
2
+ VERSION = '0.0.3'
3
+ end
@@ -196,4 +196,53 @@ describe AscDesc::ModelAdditions do
196
196
 
197
197
  end
198
198
 
199
+ describe 'dynamic order_by' do
200
+
201
+ it 'should return an ActiveRecord Relation' do
202
+ candies = Candy.order_by_name
203
+ candies.should be_an_instance_of(ActiveRecord::Relation)
204
+ end
205
+
206
+ it 'should generate an order clause using a column as part of the method name' do
207
+ candies = Candy.order_by_name
208
+ candies.to_sql.should be_end_with('ORDER BY name ASC')
209
+ end
210
+
211
+ it 'should generate an order clause using a column and a sort direction (asc/desc) as part of the method name' do
212
+ candies = Candy.order_by_name_asc
213
+ candies.to_sql.should be_end_with('ORDER BY name ASC')
214
+
215
+ candies = Candy.order_by_name_desc
216
+ candies.to_sql.should be_end_with('ORDER BY name DESC')
217
+ end
218
+
219
+ it 'should generate an order clause using multiple columns as part of the method name' do
220
+ candies = Candy.order_by_classification_and_name
221
+ candies.to_sql.should be_end_with('ORDER BY classification ASC, name ASC')
222
+
223
+ candies = Candy.order_by_classification_and_name_and_sugar
224
+ candies.to_sql.should be_end_with('ORDER BY classification ASC, name ASC, sugar ASC')
225
+ end
226
+
227
+ it 'should generate an order clause using multiple columns and sort directions (asc/desc) as part of the method name' do
228
+ candies = Candy.order_by_classification_asc_and_name_desc
229
+ candies.to_sql.should be_end_with('ORDER BY classification ASC, name DESC')
230
+
231
+ candies = Candy.order_by_classification_desc_and_name_asc
232
+ candies.to_sql.should be_end_with('ORDER BY classification DESC, name ASC')
233
+
234
+ candies = Candy.order_by_classification_asc_and_name_desc_and_sugar_asc
235
+ candies.to_sql.should be_end_with('ORDER BY classification ASC, name DESC, sugar ASC')
236
+ end
237
+
238
+ it 'should generate an order clause using multiple columns with or without sort direction (asc/desc) as part of the method name' do
239
+ candies = Candy.order_by_classification_and_name_desc
240
+ candies.to_sql.should be_end_with('ORDER BY classification ASC, name DESC')
241
+
242
+ candies = Candy.order_by_classification_desc_and_name
243
+ candies.to_sql.should be_end_with('ORDER BY classification DESC, name ASC')
244
+ end
245
+
246
+ end
247
+
199
248
  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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-27 00:00:00.000000000Z
12
+ date: 2012-03-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70194618974440 !ruby/object:Gem::Requirement
16
+ requirement: &70324219073340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70194618974440
24
+ version_requirements: *70324219073340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70194618974020 !ruby/object:Gem::Requirement
27
+ requirement: &70324219072920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70194618974020
35
+ version_requirements: *70324219072920
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70194618973560 !ruby/object:Gem::Requirement
38
+ requirement: &70324219072460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70194618973560
46
+ version_requirements: *70324219072460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: with_model
49
- requirement: &70194618973140 !ruby/object:Gem::Requirement
49
+ requirement: &70324219072040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70194618973140
57
+ version_requirements: *70324219072040
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activerecord
60
- requirement: &70194618972640 !ruby/object:Gem::Requirement
60
+ requirement: &70324219071540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 3.2.1
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70194618972640
68
+ version_requirements: *70324219071540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &70194618972140 !ruby/object:Gem::Requirement
71
+ requirement: &70324219071040 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: 1.3.5
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70194618972140
79
+ version_requirements: *70324219071040
80
80
  description: This gem adds two new methods to ActiveRecord (and ActiveRecord::Relation)
81
81
  that allows to sort SQL queries without using the "ASC" and "DESC" SQL keywords.
82
82
  email: