everywhere 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,8 +28,15 @@ And where + "not like" as well.
28
28
 
29
29
  == Syntaxes
30
30
 
31
- "+Everywhere+" supports 4 syntaxes. Note that you can use only one syntax at a time, and others will be disabled.
32
- The +hash_value+ syntax will be enabled by default.
31
+ "+Everywhere+" supports 5 syntaxes. Note that you can use only one syntax at a time, and others will be disabled.
32
+ The +chain+ syntax will be enabled by default.
33
+
34
+ * chain
35
+ +Model.where+ with no args can be chained with +not+, +like+, and +not_like+ methods.
36
+ This syntax was proposed by Jeremy Kemper: https://github.com/rails/rails/pull/5950#issuecomment-5591330
37
+
38
+ User.where.not(:name => 'foo')
39
+ => SELECT "users".* FROM "users" WHERE ("users"."name" != 'foo')
33
40
 
34
41
  * hash_value
35
42
  Push the value into a Hash indexed by +:not+.
@@ -61,7 +68,7 @@ See specs for more details.
61
68
 
62
69
  == Supported versions
63
70
 
64
- ActiveRecord 3.0.x, 3.1.x, and 3.2 (edge)
71
+ ActiveRecord 3.0.x, 3.1.x, 3.2.x, and 4.0 (edge)
65
72
 
66
73
 
67
74
  == Usage
@@ -74,9 +81,20 @@ Bundle 'everywhere' gem.
74
81
  You can choose one from four syntaxes listed above.
75
82
  For example, if you prefer the symbol syntax, put the following line in your config file.
76
83
 
77
- config.active_record.where_syntax = :symbol
84
+ config.active_record.where_syntax = :hash_value
85
+
86
+ The default value is +:chain+.
87
+
88
+ * for users of previous versions
89
+ Note that the default behaviour has been changed since 2.0 release if you've not explicitly configured the syntax.
90
+
91
+
92
+ == Running specs
93
+
94
+ There is spec file for each syntax but there is no Rake task for running all the specs at once, because there's no way to load these freedom-patches without interfering each other.
95
+ So, please run the +rspec+ command specifying one spec file.
78
96
 
79
- The default value is +:hash_value+.
97
+ % bundle e rspec spec/chain_spec.rb
80
98
 
81
99
 
82
100
  == Contributing to Everywhere
@@ -0,0 +1,58 @@
1
+ require 'everywhere/util'
2
+
3
+ module ActiveRecord
4
+ module ChainRelation
5
+ module NotBuilder
6
+ include Everywhere::Util
7
+
8
+ def build_where(opts, other = [])
9
+ super.map {|r| negate r}
10
+ end
11
+ end
12
+
13
+ module LikeBuilder
14
+ def build_where(opts, other = [])
15
+ super.map {|r| Arel::Nodes::Matches.new r.left, r.right}
16
+ end
17
+ end
18
+
19
+ module NotLikeBuilder
20
+ def build_where(opts, other = [])
21
+ super.map {|r| Arel::Nodes::DoesNotMatch.new r.left, r.right}
22
+ end
23
+ end
24
+
25
+ def not(opts, *rest)
26
+ extend(NotBuilder).where(opts, *rest).dup
27
+ end
28
+
29
+ def like(opts, *rest)
30
+ extend(LikeBuilder).where(opts, *rest).dup
31
+ end
32
+
33
+ def not_like(opts, *rest)
34
+ extend(NotLikeBuilder).where(opts, *rest).dup
35
+ end
36
+ end
37
+
38
+ module QueryMethods
39
+ if ActiveRecord::VERSION::STRING > '4'
40
+ def where_with_not_and_like_and_not_like(opts = nil, *rest)
41
+ if opts.nil?
42
+ spawn.extend(ChainRelation)
43
+ else
44
+ where_without_not_and_like_and_not_like(opts, *rest)
45
+ end
46
+ end
47
+ else
48
+ def where_with_not_and_like_and_not_like(opts = nil, *rest)
49
+ if opts.nil?
50
+ clone.extend(ChainRelation)
51
+ else
52
+ where_without_not_and_like_and_not_like(opts, *rest)
53
+ end
54
+ end
55
+ end
56
+ alias_method_chain :where, :not_and_like_and_not_like
57
+ end
58
+ end
@@ -10,7 +10,7 @@ module Everywhere
10
10
  class Railtie < ::Rails::Railtie #:nodoc:
11
11
  initializer 'everywhere' do |app|
12
12
  ActiveSupport.on_load(:active_record) do
13
- require "everywhere/#{app.config.active_record.where_syntax || 'hash_value'}"
13
+ require "everywhere/#{app.config.active_record.where_syntax || 'chain'}"
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Everywhere
2
- VERSION = '1.0.1'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+ require 'everywhere/chain'
3
+
4
+ describe 'normal query' do
5
+ before do
6
+ @where = Post.where(:title => 'hello').where_values
7
+ end
8
+ specify { @where.should have(1).item }
9
+ subject { @where.first }
10
+ its(:to_sql) { should == %q["posts"."title" = 'hello'] }
11
+ end
12
+
13
+ describe 'not' do
14
+ describe 'not eq' do
15
+ before do
16
+ @where = Post.where.not(:title => 'hello').where_values
17
+ end
18
+ specify { @where.should have(1).item }
19
+ subject { @where.first }
20
+ its(:to_sql) { should == %q["posts"."title" != 'hello'] }
21
+ end
22
+
23
+ describe 'not null' do
24
+ before do
25
+ @where = Post.where.not(:created_at => nil).where_values
26
+ end
27
+ specify { @where.should have(1).item }
28
+ subject { @where.first }
29
+ its(:to_sql) { should == %q["posts"."created_at" IS NOT NULL] }
30
+ end
31
+
32
+ describe 'not in' do
33
+ before do
34
+ @where = Post.where.not(:title => %w[hello goodbye]).where_values
35
+ end
36
+ specify { @where.should have(1).item }
37
+ subject { @where.first }
38
+ its(:to_sql) { should == %q["posts"."title" NOT IN ('hello', 'goodbye')] }
39
+ end
40
+
41
+ describe 'association' do
42
+ before do
43
+ @where = Post.joins(:comments).where.not(:comments => {:body => 'foo'}).where_values
44
+ end
45
+ specify { @where.should have(1).item }
46
+ subject { @where.first }
47
+ its(:to_sql) { should == %q["comments"."body" != 'foo'] }
48
+ end
49
+
50
+ describe 'with preceding where' do
51
+ before do
52
+ @where = Post.where(:title => 'hello').where.not(:title => 'world').where_values
53
+ end
54
+ specify { @where.should have(2).items }
55
+ it 'should work' do
56
+ @where.last.to_sql.should == %q["posts"."title" != 'world']
57
+ end
58
+ describe 'preceding where' do
59
+ it 'should not be affected' do
60
+ @where.first.to_sql.should == %q["posts"."title" = 'hello']
61
+ end
62
+ end
63
+ end
64
+
65
+ describe 'with succeeding where' do
66
+ before do
67
+ @where = Post.where.not(:title => 'hello').where(:title => 'world').where_values
68
+ end
69
+ specify { @where.should have(2).items }
70
+ it 'should work' do
71
+ @where.first.to_sql.should == %q["posts"."title" != 'hello']
72
+ end
73
+ describe 'succeeding where' do
74
+ it 'should not be affected' do
75
+ @where.last.to_sql.should == %q["posts"."title" = 'world']
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ describe 'like' do
82
+ describe 'like match' do
83
+ before do
84
+ @where = Post.where.like(:title => 'he%').where_values
85
+ end
86
+ specify { @where.should have(1).item }
87
+ subject { @where.first }
88
+ its(:to_sql) { should == %q["posts"."title" LIKE 'he%'] }
89
+ end
90
+ end
91
+
92
+ describe 'not like' do
93
+ describe 'not like match' do
94
+ before do
95
+ @where = Post.where.not_like(:title => 'he%').where_values
96
+ end
97
+ specify { @where.should have(1).item }
98
+ subject { @where.first }
99
+ its(:to_sql) { should == %q["posts"."title" NOT LIKE 'he%'] }
100
+ end
101
+ end
102
+
103
+ describe 'chaining multiple extended queries' do
104
+ before do
105
+ @where = Post.where.like(:title => '%hell%').where.not(:title => 'world').where.not_like(:title => 'heaven and %').where_values
106
+ end
107
+ specify { @where.should have(3).items }
108
+ describe 'not' do
109
+ subject { @where[1] }
110
+ its(:to_sql) { should == %q["posts"."title" != 'world'] }
111
+ end
112
+ describe 'like' do
113
+ subject { @where.first }
114
+ its(:to_sql) { should == %q["posts"."title" LIKE '%hell%'] }
115
+ end
116
+ describe 'not like' do
117
+ subject { @where.last }
118
+ its(:to_sql) { should == %q["posts"."title" NOT LIKE 'heaven and %'] }
119
+ end
120
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: everywhere
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-15 00:00:00.000000000 Z
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -74,6 +74,7 @@ files:
74
74
  - Rakefile
75
75
  - everywhere.gemspec
76
76
  - lib/everywhere.rb
77
+ - lib/everywhere/chain.rb
77
78
  - lib/everywhere/hash_key.rb
78
79
  - lib/everywhere/hash_value.rb
79
80
  - lib/everywhere/method.rb
@@ -81,6 +82,7 @@ files:
81
82
  - lib/everywhere/symbol.rb
82
83
  - lib/everywhere/util.rb
83
84
  - lib/everywhere/version.rb
85
+ - spec/chain_spec.rb
84
86
  - spec/hash_key_spec.rb
85
87
  - spec/hash_value_spec.rb
86
88
  - spec/method_spec.rb
@@ -111,6 +113,7 @@ signing_key:
111
113
  specification_version: 3
112
114
  summary: Hash condition syntax for AR query everywhere!
113
115
  test_files:
116
+ - spec/chain_spec.rb
114
117
  - spec/hash_key_spec.rb
115
118
  - spec/hash_value_spec.rb
116
119
  - spec/method_spec.rb