ryana-inequal_opportunity 0.1.2 → 0.1.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/README +40 -22
- data/lib/inequal_opportunity.rb +0 -1
- data/test/inequal_opportunity_test.rb +18 -15
- metadata +1 -1
data/README
CHANGED
@@ -1,46 +1,64 @@
|
|
1
1
|
= Inequal Opportunity
|
2
2
|
|
3
|
-
|
4
|
-
for specifying SQL conditions:
|
3
|
+
inequal_opportunity exists because this does not work in ActiveRecord:
|
5
4
|
|
6
|
-
|
7
|
-
YourModel.all(:limit => 5, :order => :created_at, :conditions => {:user_id => 5})
|
5
|
+
Doctor.all(:joins => :patients, :conditions => {:patients => ['age > ?', 20]})
|
8
6
|
|
9
|
-
|
7
|
+
You will get an unknown column exception looking for `doctors`.`age`.
|
8
|
+
Instead, you need to write:
|
10
9
|
|
11
|
-
|
10
|
+
Doctor.all(:joins => :pateints, :conditions => ['`patients`.`age` > ?', 20])
|
12
11
|
|
13
|
-
|
12
|
+
Putting the string table name in the query annoyed me. On top of that,
|
13
|
+
I always wanted a way to eliminate strings from my named scopes and queries.
|
14
|
+
So now with inequal_opportunity you can write:
|
14
15
|
|
15
|
-
|
16
|
+
Doctor.all(:joins => :patients, :conditions => {:patients => {:age => gt(20)}})
|
16
17
|
|
17
|
-
|
18
|
+
Not only is it prettier (hashed), but ActiveRecord will keep track of
|
19
|
+
table names for you.
|
18
20
|
|
19
|
-
|
21
|
+
ActiveRecord looks for Array and Range types to decide whether to use
|
22
|
+
'IN' or 'BETWEEN' instead of the normal '=' as the comparison operator
|
23
|
+
when in generating SQL. inequal_opportunity extends that pattern by
|
24
|
+
wrapping the value in a series of ActiveRecord::Inequality::Base classes.
|
25
|
+
Just wrap the value in one of the following helper functions:
|
20
26
|
|
21
|
-
|
22
|
-
which is then used to insert the proper operator into the generated SQL.
|
23
|
-
|
24
|
-
Other supported inequalities are:
|
25
|
-
|
26
|
-
gte() => >=
|
27
|
+
gte() => >=
|
27
28
|
gt() => >
|
28
29
|
lte() => <=
|
29
30
|
le() => <
|
30
31
|
ne() => <>
|
31
32
|
ne(nil) => IS NOT
|
32
33
|
|
34
|
+
and the appropriate SQL will be generated. This works in finds, as shown
|
35
|
+
above, in counts:
|
36
|
+
|
37
|
+
People.count(:age => gt(20))
|
38
|
+
|
39
|
+
in named scopes:
|
40
|
+
|
41
|
+
class People < AR::B
|
42
|
+
named_scope :underage, :conditions => {:age => lte(18)}
|
43
|
+
end
|
44
|
+
|
45
|
+
in default scopes:
|
46
|
+
|
47
|
+
class Feedback < AR::B
|
48
|
+
default_scope :conditions => {:type => ne('spam')}
|
49
|
+
end
|
50
|
+
|
51
|
+
and pretty much everywhere else I've tested.
|
33
52
|
|
34
|
-
Test coverage is
|
35
|
-
on MySQL. I also am not completely satisfied with the way I
|
36
|
-
ActiveRecord.expand_range_bind_variables, but it works.
|
53
|
+
Test coverage is kind of sparse right now, and it's only been tested
|
54
|
+
on MySQL. I also am not completely satisfied with the way I alias
|
55
|
+
ActiveRecord::Base.expand_range_bind_variables, but it works.
|
37
56
|
|
38
57
|
== License
|
39
58
|
|
40
|
-
|
59
|
+
inequal_opportunity is released under the MIT license.
|
41
60
|
|
42
61
|
|
43
62
|
== Support
|
44
63
|
|
45
|
-
Just email me at ryan@angilly.com with questions, bugs,
|
46
|
-
or patches
|
64
|
+
Just email me at ryan@angilly.com with questions, bugs, or patches.
|
data/lib/inequal_opportunity.rb
CHANGED
@@ -1,27 +1,30 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
require File.join(File.dirname(__FILE__), 'db_setup')
|
3
3
|
|
4
|
-
|
4
|
+
METHOD_SYMBOLS = [:gt, :gte, :lt, :lte, :ne, :like]
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
belongs_to :seconds
|
10
|
-
named_scope :newer_than, lambda {|time| {:conditions => {:created_at => gte(time) }} }
|
11
|
-
|
12
|
-
METHOD_SYMBOLS.each do |s|
|
13
|
-
named_scope :"try_#{s}", lambda {|i| {:conditions => {:id => send(s, i)}} }
|
14
|
-
end
|
6
|
+
class Main < ActiveRecord::Base
|
7
|
+
belongs_to :seconds
|
8
|
+
named_scope :newer_than, lambda {|time| {:conditions => {:created_at => gte(time) }} }
|
15
9
|
|
10
|
+
METHOD_SYMBOLS.each do |s|
|
11
|
+
named_scope :"try_#{s}", lambda {|i| {:conditions => {:id => send(s, i)}} }
|
16
12
|
end
|
17
13
|
|
18
|
-
|
19
|
-
has_many :mains
|
20
|
-
end
|
14
|
+
end
|
21
15
|
|
22
|
-
|
16
|
+
class Second < ActiveRecord::Base
|
17
|
+
has_many :mains
|
18
|
+
end
|
23
19
|
|
24
20
|
class InequalOpportunityTest < Test::Unit::TestCase
|
21
|
+
|
22
|
+
def setup
|
23
|
+
TABLES.each do |t|
|
24
|
+
ActiveRecord::Base.connection.execute("DELETE FROM #{t};")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
25
28
|
context "a model" do
|
26
29
|
setup do
|
27
30
|
@model = Main
|
@@ -32,7 +35,7 @@ class InequalOpportunityTest < Test::Unit::TestCase
|
|
32
35
|
end
|
33
36
|
|
34
37
|
should "should work with a named_scope" do
|
35
|
-
assert_equal Main.newer_than(2.days.
|
38
|
+
assert_equal Main.newer_than(2.days.ago).all, []
|
36
39
|
end
|
37
40
|
|
38
41
|
should "generate proper sql for array" do
|