ninjudd-model_set 0.10.2 → 0.10.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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 2
2
+ :patch: 3
3
3
  :major: 0
4
4
  :minor: 10
@@ -34,23 +34,25 @@ class ModelSet
34
34
  add_conditions!( ids_clause(ids, field) )
35
35
  end
36
36
 
37
- def order_by!(order, joins = nil)
38
- @sort_order = order ? order.to_s : nil
39
- @sort_joins = joins
37
+ def order_by!(*args)
38
+ opts = args.last.kind_of?(Hash) ? args.pop : {}
39
+
40
+ @sort_join = sanitize_condition(opts[:join])
41
+ @sort_order = args
40
42
  clear_cache!
41
43
  end
42
44
 
43
45
  def reverse!
44
46
  if @sort_order
45
- @sort_order = @sort_order.split(/\s*,\s*/).collect do |sub_order|
47
+ @sort_order.collect! do |sub_order|
46
48
  if sub_order =~ / DESC$/i
47
49
  sub_order.slice(0..-6)
48
50
  else
49
51
  "#{sub_order} DESC"
50
52
  end
51
- end.join(', ')
53
+ end
52
54
  else
53
- @sort_order = "#{id_field_with_prefix} DESC"
55
+ @sort_order = ["#{id_field_with_prefix} DESC"]
54
56
  end
55
57
  clear_cache!
56
58
  end
@@ -76,14 +78,14 @@ class ModelSet
76
78
  def order_clause
77
79
  return unless @sort_order
78
80
  # Prevent SQL injection attacks.
79
- "ORDER BY #{@sort_order.gsub(/[^\w_, \.\(\)'\"]/, '')}"
81
+ "ORDER BY #{@sort_order.join(', ').gsub(/[^\w_, \.\(\)'\"]/, '')}"
80
82
  end
81
83
 
82
84
  def join_clause
83
- return unless @joins or @sort_joins
85
+ return unless @joins or @sort_join
84
86
  joins = []
85
87
  joins << @joins if @joins
86
- joins << @sort_joins if @sort_joins
88
+ joins << @sort_join if @sort_join
87
89
  joins.join(' ')
88
90
  end
89
91
  end
@@ -116,7 +116,7 @@ class ModelSetTest < Test::Unit::TestCase
116
116
  ids = [captain.id, ironman.id, phoenix.id, spidey.id, wolverine.id]
117
117
  set = HeroSet.with_universe('Marvel')
118
118
 
119
- set.order_by!('name')
119
+ set.order_by!('name')
120
120
  assert_equal ids, set.ids
121
121
 
122
122
  set.reverse!
@@ -127,6 +127,13 @@ class ModelSetTest < Test::Unit::TestCase
127
127
 
128
128
  set.reverse!
129
129
  assert_equal ids, set.ids
130
+
131
+ # Make sure that a comma in a function call works.
132
+ set.order_by!("lower(ltrim(name, 'C'))")
133
+ assert_equal ids, set.ids
134
+
135
+ set.reverse!
136
+ assert_equal ids.reverse, set.ids
130
137
  end
131
138
 
132
139
  should "have missing ids" do
@@ -171,7 +178,6 @@ class ModelSetTest < Test::Unit::TestCase
171
178
  assert_equal [invisibility.id, flying.id], set.ids
172
179
  end
173
180
 
174
-
175
181
  should "allow set extensions" do
176
182
  hero = Hero.create(:name => 'Mr. Invisible')
177
183
  mighty_mouse = Superpet.create(:name => 'Mighty Mouse', :owner_id => hero.id, :species => 'mouse')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninjudd-model_set
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.10.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Balthrop