activerecord-blockwhere 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6188f0b42749735cf271522b4553caa3824e8ce8
4
+ data.tar.gz: 5dac29b2a8de101982f52039e628810e5a57bb01
5
+ SHA512:
6
+ metadata.gz: f664af9db26928765b748e3044df7c80cfc0c2fef681e77d5479cd8b3f9697e43151241148a3ad71dfc4aa573675959dfc4cb7f1694c4ab4780abe161411448b
7
+ data.tar.gz: 1719c1e6a3565f88b3eb0596382d24041c542deac61d9f15798e6652305f0e01f2d0c664a50d1b5ca0416eb0eda9952f844ce5be922f59305d06e0ca2bec42a6
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .bundle
20
+ vendor/bundle
21
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-blockwhere.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,31 @@
1
+ Copyright (c) 2013, Synergy Marketing, Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+
8
+ Redistributions of source code must retain the above copyright notice,
9
+ this list of conditions and the following disclaimer.
10
+
11
+ Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ Neither the name of the Synergy Marketing, Inc. nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
+ COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30
+ DAMAGE.
31
+
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Activerecord::Blockwhere
2
+
3
+ Simply conditions DSL for ActiveRecord.
4
+ It helps using Arel Predications in the block.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'activerecord-blockwhere'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install activerecord-blockwhere
19
+
20
+ ## Usage
21
+
22
+ # * person has many entries.
23
+ # Person(id: integer, name: string)
24
+ # Entry(id: integer, person_id: integer, name: string)
25
+
26
+ Person.where { id.eq(1) }.to_sql
27
+ # => SELECT "people".* FROM "people" WHERE "people"."id" = 1
28
+
29
+ Person.where { !id.eq(1) }.to_sql
30
+ # => SELECT "people".* FROM "people" WHERE (NOT ("people"."id" = 1))
31
+
32
+ Person.where { id.eq(1) & name.matches('%alice%') }.to_sql
33
+ # => SELECT "people".* FROM "people" WHERE ("people"."id" = 1 AND "people"."name" LIKE '%alice%')
34
+
35
+ Person.where { id.in([1,2,3]) & name.not_eq('bob') }.to_sql
36
+ # => SELECT "people".* FROM "people" WHERE ("people"."id" IN (1, 2, 3) AND "people"."name" != 'bob')
37
+
38
+ Person.where { name.eq('alice') | name.eq('bob') }.to_sql
39
+ # => SELECT "people".* FROM "people" WHERE (("people"."name" = 'alice' OR "people"."name" = 'bob'))
40
+
41
+ # join association
42
+ Person.where { name.eq('alice') & entries.name.matches('%hello%') }.to_sql
43
+ # => SELECT "people".* FROM "people"
44
+ # INNER JOIN "entries" ON "entries"."person_id" = "people"."id"
45
+ # WHERE ("people"."name" = 'alice' AND "entries"."name" LIKE '%hello%')
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require "bundler/gem_tasks"
4
+
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+ namespace :spec do
11
+ desc "Create rspec coverage"
12
+ task :coverage do
13
+ ENV['COVERAGE'] = 'true'
14
+ Rake::Task["spec"].execute
15
+ end
16
+
17
+ desc "boot console with spec helpers"
18
+ task :console do
19
+ require File.expand_path('../spec/spec_helper.rb', __FILE__)
20
+ require 'irb'
21
+ ARGV.clear
22
+ IRB.start
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/blockwhere/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-blockwhere"
8
+ spec.version = ActiveRecord::Blockwhere::VERSION
9
+ spec.authors = ["yuki teraoka"]
10
+ spec.email = ["info@techscore.com"]
11
+ spec.description = %q{Simply conditions DSL for ActiveRecord }
12
+ spec.summary = %q{Simply conditions DSL for ActiveRecord. It helps using Arel Predications in the block.}
13
+ spec.homepage = "https://github.com/techscore/activerecord-blockwhere"
14
+ spec.license = "BSD"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ spec.add_development_dependency "activerecord", ">= 3.0.0"
26
+ spec.add_development_dependency "sqlite3-ruby"
27
+ spec.add_development_dependency "database_cleaner"
28
+ spec.add_runtime_dependency "activerecord", ">= 3.0.0"
29
+ spec.add_runtime_dependency "arel", ">= 3.0.0"
30
+ end
@@ -0,0 +1,17 @@
1
+ require "active_record/blockwhere/where_proxy"
2
+ require "active_record/blockwhere/arel_node_operations"
3
+
4
+ module ActiveRecord
5
+ module Blockwhere
6
+ def where(*args, &block)
7
+ relation = args.empty? ? self : super(*args)
8
+ relation = WhereProxy.where(relation, block.binding.eval('self'), &block) if block
9
+ relation
10
+ end
11
+ end
12
+ end
13
+
14
+ ActiveSupport.on_load(:active_record) do
15
+ ActiveRecord::Relation.send :include, ActiveRecord::Blockwhere
16
+ Arel::Nodes::Node.send :include, ActiveRecord::Blockwhere::ArelNodeOperations
17
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveRecord
2
+ module Blockwhere
3
+ module ArelNodeOperations
4
+ def &(other)
5
+ self.and(other)
6
+ end
7
+
8
+ def |(other)
9
+ self.or(other)
10
+ end
11
+
12
+ def !
13
+ self.not
14
+ end
15
+
16
+ def empty?
17
+ false
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module Blockwhere
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_support'
2
+
3
+ module ActiveRecord
4
+ module Blockwhere
5
+ class WhereProxy < ActiveSupport::BasicObject
6
+ attr_reader :relation, :context
7
+
8
+ def self.where(relation, context = nil, &block)
9
+ proxy = new(relation, context)
10
+ condition = proxy.instance_eval(&block)
11
+ condition ? proxy.relation.where(condition) : proxy.relation
12
+ end
13
+
14
+ def initialize(relation, context = nil)
15
+ @relation = relation
16
+ @context = context
17
+ end
18
+
19
+ def method_missing(name, *args, &block)
20
+ if @relation.columns_hash.key?(name.to_s)
21
+ return @relation.arel_table[name]
22
+ end
23
+ reflection = @relation.reflections[name]
24
+ if ::ActiveRecord::Reflection::AssociationReflection === reflection
25
+ @relation = @relation.joins(name) unless @relation.joins_values.include?(name)
26
+ return WhereProxy.new(reflection.klass.scoped, @context)
27
+ end
28
+ if @context && @context.respond_to?(name)
29
+ return @context.__send__(name, *args, &block)
30
+ end
31
+ @relation.__send__(name, *args, &block)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe ActiveRecord::Blockwhere do
4
+ before(:each) do
5
+ @alice = Person.create!({:id => 1, :name => "Alice" }, :without_protection => true)
6
+ @bob = Person.create!({:id => 2, :name => "Bob" }, :without_protection => true)
7
+ @carol = Person.create!({:id => 3, :name => "Carol" }, :without_protection => true)
8
+ @entry1 = @alice.entries.create!({:id => 1}, :without_protection => true)
9
+ @entry2 = @alice.entries.create!({:id => 2}, :without_protection => true)
10
+ @entry3 = @bob.entries.create!({:id => 3}, :without_protection => true)
11
+ end
12
+
13
+ context 'simple where' do
14
+ it { expect(Person.where{ id.eq(1)}.count ).to eq 1 }
15
+ it { expect(Person.where{ id.eq(1)}.first ).to eq @alice }
16
+ it { expect(Person.where{ id.eq(1) | id.eq(2)}.map(&:name).sort ).to eq %w(Alice Bob ) }
17
+ it { expect(Person.where{ id.in([1, 3]) }.map(&:name).sort ).to eq %w(Alice Carol) }
18
+ it { expect(Person.where{ ! id.eq(1) }.map(&:name).sort ).to eq %w( Bob Carol) }
19
+ end
20
+
21
+ context 'where with association' do
22
+ it { expect(Person.where{ entries.id.not_eq(0) }.map(&:name).sort.uniq ).to eq %w(Alice Bob ) }
23
+ it { expect(Person.where{ entries.id.eq(1) }.map(&:name).sort ).to eq %w(Alice ) }
24
+ end
25
+
26
+ context 'where with context' do
27
+ before(:each) do
28
+ @context = Struct.new(:params).new({})
29
+ end
30
+
31
+ it do
32
+ @context.params[:id] = 2
33
+ expect(@context.instance_eval { Person.where{ id.eq(params[:id]) }.first } ).to eq @bob
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ add_filter '/vendor/'
6
+ end
7
+ end
8
+
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ require 'rspec'
12
+ require 'active_record/blockwhere'
13
+
14
+ # Requires supporting files with custom matchers and macros, etc,
15
+ # in ./support/ and its subdirectories.
16
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].each {|f| require f}
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+ require 'database_cleaner'
3
+
4
+ DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
5
+
6
+ RSpec.configure do |config|
7
+ config.before :suite do
8
+ DatabaseCleaner.clean_with :truncation
9
+ end
10
+ config.before :each do
11
+ DatabaseCleaner.start
12
+ end
13
+ config.after :each do
14
+ DatabaseCleaner.clean
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_record'
2
+ ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ':memory:')
3
+
4
+ #migrations
5
+ class CreateAllTables < ActiveRecord::Migration
6
+ def self.up
7
+ create_table(:people ) {|t| t.string :name}
8
+ create_table(:entries ) {|t| t.integer :person_id; t.string :name }
9
+ end
10
+ end
11
+
12
+ ActiveRecord::Migration.verbose = false
13
+ CreateAllTables.up
14
+
15
+
16
+ class Person < ActiveRecord::Base
17
+ has_many :entries
18
+ end
19
+
20
+ class Entry < ActiveRecord::Base
21
+ belongs_to :person
22
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-blockwhere
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - yuki teraoka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3-ruby
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: arel
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: 3.0.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: 3.0.0
139
+ description: 'Simply conditions DSL for ActiveRecord '
140
+ email:
141
+ - info@techscore.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - .gitignore
147
+ - Gemfile
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - activerecord-blockwhere.gemspec
152
+ - lib/active_record/blockwhere.rb
153
+ - lib/active_record/blockwhere/arel_node_operations.rb
154
+ - lib/active_record/blockwhere/version.rb
155
+ - lib/active_record/blockwhere/where_proxy.rb
156
+ - spec/activerecord-blockwhere_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/support/database_cleaner.rb
159
+ - spec/support/models.rb
160
+ homepage: https://github.com/techscore/activerecord-blockwhere
161
+ licenses:
162
+ - BSD
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - '>='
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.0.3
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Simply conditions DSL for ActiveRecord. It helps using Arel Predications
184
+ in the block.
185
+ test_files:
186
+ - spec/activerecord-blockwhere_spec.rb
187
+ - spec/spec_helper.rb
188
+ - spec/support/database_cleaner.rb
189
+ - spec/support/models.rb