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 CHANGED
@@ -1,46 +1,64 @@
1
1
  = Inequal Opportunity
2
2
 
3
- ActiveRecord is a ruby ORM released with Ruby on Rails. It has a hash syntax
4
- for specifying SQL conditions:
3
+ inequal_opportunity exists because this does not work in ActiveRecord:
5
4
 
6
- YourModel.count(:conditions => {:id => 5})
7
- YourModel.all(:limit => 5, :order => :created_at, :conditions => {:user_id => 5})
5
+ Doctor.all(:joins => :patients, :conditions => {:patients => ['age > ?', 20]})
8
6
 
9
- But this hash format lacks inequalities. In order to generate:
7
+ You will get an unknown column exception looking for `doctors`.`age`.
8
+ Instead, you need to write:
10
9
 
11
- SELECT * from your_models where id <> '42';
10
+ Doctor.all(:joins => :pateints, :conditions => ['`patients`.`age` > ?', 20])
12
11
 
13
- You have to write:
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
- YourModel.count(:conditions => ['id <> ?', 42])
16
+ Doctor.all(:joins => :patients, :conditions => {:patients => {:age => gt(20)}})
16
17
 
17
- With Inequal Opportunity, you can write:
18
+ Not only is it prettier (hashed), but ActiveRecord will keep track of
19
+ table names for you.
18
20
 
19
- YourModel.count(:conditions => {:id => ne(42)})
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
- Where Object#ne wraps 42 in a ActiveRecord::Inequality::NotEqual class,
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 real sparse right now. And it's only been tested
35
- on MySQL. I also am not completely satisfied with the way I overwrite
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
- Inequal Opportunity is released under the MIT license.
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.
@@ -124,7 +124,6 @@ module ActiveRecord
124
124
  end
125
125
  end
126
126
 
127
- # Copied this from AR. Not ideal.
128
127
  alias expand_range_bind_variables_orig expand_range_bind_variables
129
128
  def expand_range_bind_variables(bind_vars)
130
129
  expanded = []
@@ -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
- require 'ruby-debug'
4
+ METHOD_SYMBOLS = [:gt, :gte, :lt, :lte, :ne, :like]
5
5
 
6
- METHOD_SYMBOLS = [:gt, :gte, :lt, :lte, :ne, :like]
7
-
8
- class Main < ActiveRecord::Base
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
- class Second < ActiveRecord::Base
19
- has_many :mains
20
- end
14
+ end
21
15
 
22
- ActiveRecord::Base.logger = Logger.new(STDERR)
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.from_now).all, []
38
+ assert_equal Main.newer_than(2.days.ago).all, []
36
39
  end
37
40
 
38
41
  should "generate proper sql for array" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ryana-inequal_opportunity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Angilly