Skip to content

Commit

Permalink
Fix errors on Ruby 1.8.7 where !~ and != can't be overridden, add…
Browse files Browse the repository at this point in the history
… it to the README file
  • Loading branch information
samleb committed Feb 18, 2013
1 parent 709fedc commit 4f15505
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ Here is a complete list of Arel method aliases:
* For predicates:
- `==`: `eq`
- `=~`: `matches`
- `!~`: `does_not_match`
- `!~`: `does_not_match` (won't work in Ruby 1.8)
- `>=`: `gteq`
- `>` : `gt`
- `<` : `lt`
- `<=`: `lteq`
- `!=`: `not_eq`

- `!=`: `not_eq` (won't work in Ruby 1.8)

* For combination
- `&`: `and`
Expand Down
11 changes: 9 additions & 2 deletions lib/sexy_scopes/arel/predicate_methods.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module SexyScopes
module Arel
module PredicateMethods
class << self
private
def ruby_19_alias(new_name, old_name)
class_eval "alias #{new_name} #{old_name}" if RUBY_VERSION >= "1.9"
end
end

def eq(other)
extend_predicate(super)
end
Expand All @@ -18,7 +25,7 @@ def matches(other)
def does_not_match(other)
extend_predicate(super)
end
alias !~ does_not_match
ruby_19_alias '!~', 'does_not_match'

def gteq(other)
extend_predicate(super)
Expand All @@ -43,7 +50,7 @@ def lteq(other)
def not_eq(other)
extend_predicate(super)
end
alias != not_eq
ruby_19_alias '!=', 'not_eq'
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions spec/predicate_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
@attribute = User.attribute(:score)
end

RUBY_19_METHODS = %w( != !~ )

METHODS = {
# Arel method => [ Ruby operator, SQL operator ]
:eq => [ '==', '= %s' ],
Expand All @@ -28,9 +30,11 @@

it { should convert_to_sql %{"users"."score" #{sql_operator % 42.0}} }

it "is aliased as `#{operator}`" do
@attribute.method(operator).should == @attribute.method(method)
end if operator
if operator && (!RUBY_19_METHODS.include?(operator) || ruby_19?)
it "is aliased as `#{operator}`" do
@attribute.method(operator).should == @attribute.method(method)
end
end
end
end
end
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
RSpec.configure do |config|
config.extend Module.new {
def ruby_19
yield if RUBY_VERSION >= "1.9"
yield if ruby_19?
end

def ruby_19?
RUBY_VERSION >= "1.9"
end
}
end
Expand Down

0 comments on commit 4f15505

Please sign in to comment.