query_methods_extend 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 751bd8940dadfc4c6d65ea648ce49868b0d8e366
4
+ data.tar.gz: 28ac09fa9a5c36060b61b4103b2072a8c82f736c
5
+ SHA512:
6
+ metadata.gz: 1038ddcb987eb19e450e43f943e349f0f2ef233c27f1af0565233a1714e96558f28f191065b01e541159b33c8ed529b3f2f48263dec7a5f6877e8f9ccc7ee0d8
7
+ data.tar.gz: 2be7a69f1e282163e61adf5a4094f8b12c69f9c78955748f4291eab095f202f9b88cbac04b2783466bd9dc55240bcb655b7745d1ede3990e8faba81a48f4982d
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in query_methods_extend.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 alicuche
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,154 @@
1
+ # QueryMethodsExtend
2
+
3
+ Extend query activerecord in rails 4
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'query_methods_extend'
11
+ ```
12
+
13
+ And model file:
14
+ ```ruby
15
+ include QueryMethodsExtend
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ The structure book model:
21
+ ```ruby
22
+ Book(
23
+ id: integer,
24
+ name: string,
25
+ price: decimal,
26
+ created_at: datetime,
27
+ updated_at: datetime
28
+ )
29
+ ```
30
+
31
+ ## Union query
32
+ ```ruby
33
+ query_one = Book.where(price: 10).select(:name, :price)
34
+ query_two = Book.where(created_at: DateTime.now).select(:name, :price)
35
+ query_one.union(query_two)
36
+
37
+ ***
38
+ SELECT "books".* FROM (
39
+ SELECT "books"."name", "books"."price" FROM "books"
40
+ WHERE "books"."created_at" = '2015-01-22 15:28:17.524026'
41
+ UNION SELECT "books"."name", "books"."price" FROM "books"
42
+ WHERE "books"."price" = 10
43
+ ) AS books
44
+ ```
45
+
46
+ ```ruby
47
+ query = Book.where(price: 10)
48
+ Book.where(created_at: DateTime.now).union(query).where(name: 'Happy')
49
+
50
+ ***
51
+ SELECT "books".* FROM (
52
+ SELECT "books".* FROM "books"
53
+ WHERE "books"."created_at" = '2015-01-22 15:57:14.143253'
54
+ UNION SELECT "books".* FROM "books"
55
+ WHERE "books"."price" = 10
56
+ ) AS books
57
+ WHERE "books"."name" = 'Happy'
58
+ ```
59
+
60
+ ```ruby
61
+ query_one = Book.where(price: 10)
62
+ query_two = Book.where(price: 15)
63
+ Book.where(created_at: DateTime.now).union(query_one).union(query_two)
64
+
65
+ ***
66
+ SELECT "books".* FROM (
67
+ SELECT "books".* FROM (
68
+ SELECT "books".* FROM "books"
69
+ WHERE "books"."created_at" = '2015-01-22 16:00:10.364535'
70
+ UNION SELECT "books".* FROM "books"
71
+ WHERE "books"."price" = 10
72
+ ) AS books
73
+ UNION SELECT "books".* FROM "books"
74
+ WHERE "books"."price" = 15
75
+ ) AS books
76
+ ```
77
+
78
+ ## Or query
79
+ ```ruby
80
+ Book.where(price: 10, name: 'Rails').or.where(price: 15, name: 'Happy')
81
+
82
+ ***
83
+ SELECT "books".* FROM "books"
84
+ WHERE (("books"."price" = 10 AND "books"."name" = 'Rails')
85
+ OR ("books"."price" = 15 AND "books"."name" = 'Happy'))
86
+ ```
87
+
88
+ ```ruby
89
+ Book.where(price: 10, name: 'Rails').or(price: 15, name: 'Happy')
90
+
91
+ ***
92
+ SELECT "books".* FROM "books"
93
+ WHERE "books"."price" = 10
94
+ AND "books"."name" = 'Rails'
95
+ AND ("books"."price" = 15 OR "books"."name" = 'Happy')
96
+ ```
97
+
98
+ ```ruby
99
+ Book.where(price: 10, name: 'Rails').or.where(price: 15, name: 'Ruby').or(price: 15, id: 1..5)
100
+
101
+ ***
102
+ SELECT "books".* FROM "books"
103
+ WHERE (("books"."price" = 10 AND "books"."name" = 'Rails')
104
+ OR ("books"."price" = 15 AND "books"."name" = 'Ruby'))
105
+ AND ("books"."price" = 15 OR "books"."id" BETWEEN 1 AND 5)
106
+ ```
107
+
108
+ ## Like query
109
+ ```ruby
110
+ Book.like(name: 'ruby')
111
+ ***
112
+ SELECT "books".* FROM "books" WHERE (books.name LIKE '%ruby%')
113
+
114
+ Book.l_like(name: 'ruby')
115
+ ***
116
+ SELECT "books".* FROM "books" WHERE (books.name LIKE '%ruby')
117
+
118
+ Book.r_like(name: 'ruby')
119
+ ***
120
+ SELECT "books".* FROM "books" WHERE (books.name LIKE 'ruby%')
121
+
122
+ Book.regex_like(name: '%ruby%')
123
+ ***
124
+ SELECT "books".* FROM "books" WHERE (books.name LIKE '%ruby%')
125
+ ```
126
+
127
+ ## Operators query
128
+ ```ruby
129
+ Book.lt(price: 5)
130
+ ***
131
+ SELECT "books".* FROM "books" WHERE (books.price < 5)
132
+
133
+ Book.lteq(price: 5)
134
+ ***
135
+ SELECT "books".* FROM "books" WHERE (books.price <= 5)
136
+
137
+ Book.gt(price: 5)
138
+ ***
139
+ SELECT "books".* FROM "books" WHERE (books.price > 5)
140
+
141
+ Book.gteq(price: 5)
142
+ ***
143
+ SELECT "books".* FROM "books" WHERE (books.price >= 5)
144
+ ```
145
+
146
+
147
+
148
+ ## Contributing
149
+
150
+ 1. Fork it ( https://github.com/[my-github-username]/query_methods_extend/fork )
151
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
152
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
153
+ 4. Push to the branch (`git push origin my-new-feature`)
154
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ require "query_methods_extend/version"
2
+ require "query_methods_extend/or"
3
+ require "query_methods_extend/like"
4
+ require "query_methods_extend/operators"
5
+ require "query_methods_extend/union"
6
+
7
+ module QueryMethodsExtend extend ActiveSupport::Concern
8
+ included do
9
+ include OrQuery
10
+ include Like
11
+ include Operators
12
+ include Union
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ module QueryMethodsExtend
2
+ module Like extend ActiveSupport::Concern
3
+ included do
4
+ attr_accessor :extend_like_string
5
+
6
+ scope :like, ->(agrs){
7
+ @extend_like_string = '%?%'
8
+ like_basic agrs
9
+ }
10
+
11
+ scope :l_like, ->(agrs){
12
+ @extend_like_string = '%?'
13
+ like_basic agrs
14
+ }
15
+
16
+ scope :r_like, ->(agrs){
17
+ @extend_like_string = '?%'
18
+ like_basic agrs
19
+ }
20
+
21
+ scope :regex_like, ->(agrs){
22
+ @extend_like_string = '?'
23
+ like_basic agrs
24
+ }
25
+
26
+ scope :like_basic, ->(agrs){
27
+ if agrs.class == Hash
28
+ items = self
29
+ agrs.each do |agr|
30
+ field, value = agr
31
+ items = items.where("#{self.table_name}.#{field} LIKE ?", @extend_like_string.gsub('?', value.to_s))
32
+ end
33
+ return items
34
+ else
35
+ raise 'Agruments should be a HASH'
36
+ end
37
+ }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ module QueryMethodsExtend
2
+ module Operators extend ActiveSupport::Concern
3
+ included do
4
+ scope :gt, ->(agrs){
5
+ @extend_operator_string = '>'
6
+ operator_basic agrs
7
+ }
8
+
9
+ scope :gteq, ->(agrs){
10
+ @extend_operator_string = '>='
11
+ operator_basic agrs
12
+ }
13
+
14
+ scope :lt, ->(agrs){
15
+ @extend_operator_string = '<'
16
+ operator_basic agrs
17
+ }
18
+
19
+ scope :lteq, ->(agrs){
20
+ @extend_operator_string = '<='
21
+ operator_basic agrs
22
+ }
23
+
24
+ scope :operator_basic, ->(agrs){
25
+ if agrs.class == Hash
26
+ items = self
27
+ agrs.each do |agr|
28
+ field, value = agr
29
+ items = items.where("#{self.table_name}.#{field} #{@extend_operator_string} ?", value)
30
+ end
31
+ return items
32
+ else
33
+ raise 'Agruments should be a HASH'
34
+ end
35
+ }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ module QueryMethodsExtend
2
+ module ExtendMethods
3
+ @is_query_or = false
4
+
5
+ def set_is_query_or value
6
+ @is_query_or = value if self.where_values.size > 0
7
+ self
8
+ end
9
+
10
+ def where!(opts, *rest)
11
+ if Hash === opts
12
+ opts = sanitize_forbidden_attributes(opts)
13
+ references!(ActiveRecord::PredicateBuilder.references(opts))
14
+ end
15
+
16
+ if @is_query_or
17
+ set_is_query_or false
18
+ self.where_values = ["(#{build_string_where(self.where_values)}) OR (#{build_string_where(build_where(opts, rest))})"]
19
+ else
20
+ self.where_values += build_where(opts, rest)
21
+ end
22
+ self
23
+ end
24
+
25
+ def build_string_where where_data
26
+ where_data.map{ |data| data.class == String ? data : data.to_sql }.join(' AND ')
27
+ end
28
+
29
+ end
30
+
31
+ module OrQuery extend ActiveSupport::Concern
32
+ included do
33
+ scope :or, ->(agrs = nil){
34
+ if agrs
35
+ if agrs.class == Hash
36
+ act = self.unscoped.where(agrs).where_values.map{ |data| data.to_sql }.join(' OR ')
37
+ self.where("(#{act})")
38
+ else
39
+ raise 'Agruments should be a HASH'
40
+ end
41
+ else
42
+ all.extending(ExtendMethods).set_is_query_or(true)
43
+ end
44
+ }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,10 @@
1
+ module QueryMethodsExtend
2
+ module Union extend ActiveSupport::Concern
3
+ included do
4
+ scope :union, ->(query){
5
+ query_self = self.all.to_sql
6
+ self.unscoped.from("(#{query_self} UNION #{query.to_sql}) AS #{self.table_name}")
7
+ }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module QueryMethodsExtend
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'query_methods_extend/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "query_methods_extend"
8
+ spec.version = QueryMethodsExtend::VERSION
9
+ spec.authors = 'Alicuche'
10
+ spec.email = 'alicuche@gmail.com'
11
+ spec.summary = ''
12
+ spec.description = ''
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ # spec.add_development_dependency "activerecord", "~> 4.0"
24
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_methods_extend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alicuche
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ''
42
+ email: alicuche@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - LICENSE
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/query_methods_extend.rb
54
+ - lib/query_methods_extend/like.rb
55
+ - lib/query_methods_extend/operators.rb
56
+ - lib/query_methods_extend/or.rb
57
+ - lib/query_methods_extend/union.rb
58
+ - lib/query_methods_extend/version.rb
59
+ - query_methods_extend.gemspec
60
+ homepage: ''
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.5
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: ''
84
+ test_files: []