query_methods_extend 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 458610213fb0ca54d4fc08f62d83d1aaafd07753
4
- data.tar.gz: 73f79ee1edd5f50f9702126b70edd987c2aada66
3
+ metadata.gz: 9c4039add270257fd9f5c8c1799aec94af2603eb
4
+ data.tar.gz: 7a45d215f5a8f2307e60ccbc8d7feb594f0e3ff5
5
5
  SHA512:
6
- metadata.gz: 7b53316ac1c3840e54730ece5d26223b41d3f3ac0e2d85040d1bfeb289f2e946f69fa7db8f197fcee8d703d569b0dba6c0b7f9c1cf8ed118916f2f71912c0184
7
- data.tar.gz: 7d6d43fd4bd310842e3f5a5b9da7557fd2e2e7f85442288cccc2506c7631b8afd4d3efb5225b58b466f2ede4b3614462f38edd62077dd8a10e7e51be956a6951
6
+ metadata.gz: 329c27fa87fc6f178f55ddb12d07fe2a5e69c11f74cd391d397ee1a0b5ce649a3041a99f55937290d4b79635bd08b3981d2e140881028a2a3eb6f9519114b660
7
+ data.tar.gz: bb9845d99f036e524aaf7037df007351ee02e63e8200927a7d2c6a3c6879ee641a7202c697e90f81f1ffefacaaa73d2c56c7b5076af73f7f883a2131958d623e
data/README.md CHANGED
@@ -11,30 +11,22 @@ Add this line to your application's Gemfile:
11
11
  gem 'query_methods_extend'
12
12
  ```
13
13
 
14
- And model file:
15
- ```ruby
16
- include QueryMethodsExtend
17
- ```
18
-
19
14
  ## Usage
20
15
 
21
16
  The structure book models:
22
17
  ```ruby
23
18
  class Store < ActiveRecord::Base
24
- include QueryMethodsExtend
25
19
  has_many :categories
26
20
  end
27
21
  // Store(id: integer, address: string)
28
22
 
29
23
  class Category < ActiveRecord::Base
30
- include QueryMethodsExtend
31
24
  belongs_to :store
32
25
  has_many :books
33
26
  end
34
27
  // Category(id: integer, name: string, store_id: integer)
35
28
 
36
29
  class Book < ActiveRecord::Base
37
- include QueryMethodsExtend
38
30
  belongs_to :category
39
31
  end
40
32
  // Book(id: integer, name: string, price: integer, category_id: integer)
@@ -57,7 +49,7 @@ SELECT "categories".* FROM "categories"
57
49
  )
58
50
  ```
59
51
 
60
- And take **all books in the stores** in Vietnam (without use **:through**):
52
+ And take **all books in the stores** in Vietnam:
61
53
  ```ruby
62
54
  Store.where(address: 'Vietnam').categories.books
63
55
 
@@ -1,4 +1,5 @@
1
1
  require "query_methods_extend/version"
2
+ require "query_methods_extend/railtie"
2
3
  require "query_methods_extend/association"
3
4
  require "query_methods_extend/or"
4
5
  require "query_methods_extend/like"
@@ -1,13 +1,14 @@
1
1
  module ActiveRecord
2
2
  module QueryMethods
3
3
  include QueryMethodsExtend::Association
4
+ include QueryMethodsExtend::Like
5
+ include QueryMethodsExtend::Operators
6
+ include QueryMethodsExtend::OrQuery
7
+ include QueryMethodsExtend::Union
4
8
 
5
9
  def method_missing(method_name, *args, &block)
6
- if respond_to? method_name
7
- super
8
- else
9
- check_association_exist(method_name) || super
10
- end
10
+ check_association_exist(method_name) || super
11
11
  end
12
+
12
13
  end
13
14
  end
@@ -12,7 +12,7 @@ module QueryMethodsExtend
12
12
 
13
13
  association = self.try(:reflect_on_association, method_name)
14
14
  result = false
15
- if association && association.macro == :has_many
15
+ if association && association.try(:macro) == :has_many
16
16
  model_reflect = association.inverse_of
17
17
  model = model_reflect.active_record
18
18
  query = self.select(association.primary_key_column.name)
@@ -3,38 +3,40 @@ module QueryMethodsExtend
3
3
  included do
4
4
  attr_accessor :extend_like_string
5
5
 
6
- scope :like, ->(agrs){
6
+ def self.like agrs
7
7
  @extend_like_string = '%{?}%'
8
8
  like_basic agrs
9
- }
9
+ end
10
10
 
11
- scope :l_like, ->(agrs){
11
+ def self.l_like agrs
12
12
  @extend_like_string = '%{?}'
13
13
  like_basic agrs
14
- }
14
+ end
15
15
 
16
- scope :r_like, ->(agrs){
16
+ def self.r_like agrs
17
17
  @extend_like_string = '{?}%'
18
18
  like_basic agrs
19
- }
19
+ end
20
20
 
21
- scope :regex_like, ->(agrs){
21
+ def self.regex_like agrs
22
22
  @extend_like_string = '{?}'
23
23
  like_basic agrs
24
- }
24
+ end
25
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))
26
+ private
27
+ def self.like_basic agrs
28
+ if agrs.class == Hash
29
+ items = self
30
+ agrs.each do |agr|
31
+ field, value = agr
32
+ items = items.where("#{self.table_name}.#{field} LIKE ?", @extend_like_string.gsub('{?}', value.to_s))
33
+ end
34
+ return items
35
+ else
36
+ raise "Like method with agruments should be a HASH"
32
37
  end
33
- return items
34
- else
35
- raise "Like method with agruments should be a HASH"
36
38
  end
37
- }
39
+
38
40
  end
39
41
  end
40
42
  end
@@ -1,38 +1,40 @@
1
1
  module QueryMethodsExtend
2
2
  module Operators extend ActiveSupport::Concern
3
3
  included do
4
- scope :gt, ->(agrs){
4
+ def self.gt agrs
5
5
  @extend_operator_string = '>'
6
6
  operator_basic agrs
7
- }
7
+ end
8
8
 
9
- scope :gteq, ->(agrs){
9
+ def self.gteq agrs
10
10
  @extend_operator_string = '>='
11
11
  operator_basic agrs
12
- }
12
+ end
13
13
 
14
- scope :lt, ->(agrs){
14
+ def self.lt agrs
15
15
  @extend_operator_string = '<'
16
16
  operator_basic agrs
17
- }
17
+ end
18
18
 
19
- scope :lteq, ->(agrs){
19
+ def self.lteq agrs
20
20
  @extend_operator_string = '<='
21
21
  operator_basic agrs
22
- }
22
+ end
23
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)
24
+ private
25
+ def self.operator_basic agrs
26
+ if agrs.class == Hash
27
+ items = self
28
+ agrs.each do |agr|
29
+ field, value = agr
30
+ items = items.where("#{self.table_name}.#{field} #{@extend_operator_string} ?", value)
31
+ end
32
+ return items
33
+ else
34
+ raise "Operators '#{@extend_operator_string}' with agruments should be a HASH"
30
35
  end
31
- return items
32
- else
33
- raise "Operators '#{@extend_operator_string}' with agruments should be a HASH"
34
36
  end
35
- }
37
+
36
38
  end
37
39
  end
38
40
  end
@@ -30,7 +30,7 @@ module QueryMethodsExtend
30
30
 
31
31
  module OrQuery extend ActiveSupport::Concern
32
32
  included do
33
- scope :or, ->(agrs = nil){
33
+ def self.or agrs = nil
34
34
  if agrs
35
35
  if agrs.class == Hash
36
36
  act = self.unscoped.where(agrs).where_values.map{ |data| data.to_sql }.join(' OR ')
@@ -41,7 +41,8 @@ module QueryMethodsExtend
41
41
  else
42
42
  all.extending(OrExtend).set_is_query_or(true)
43
43
  end
44
- }
44
+ end
45
45
  end
46
46
  end
47
+
47
48
  end
@@ -0,0 +1,9 @@
1
+ module QueryMethodsExtend
2
+ class Railtie < Rails::Railtie
3
+ initializer "query_methods_extend.initializer" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ include QueryMethodsExtend
6
+ end
7
+ end
8
+ end
9
+ end
@@ -40,5 +40,6 @@ module QueryMethodsExtend
40
40
  expect(result).to match_array([@book1, @book2])
41
41
  end
42
42
  end
43
+
43
44
  end
44
45
  end
File without changes
@@ -1,10 +1,10 @@
1
1
  module QueryMethodsExtend
2
2
  module Union extend ActiveSupport::Concern
3
3
  included do
4
- scope :union, ->(query){
4
+ def self.union query
5
5
  query_self = self.all.to_sql
6
6
  self.unscoped.from("(#{query_self} UNION #{query.to_sql}) AS #{self.table_name}")
7
- }
7
+ end
8
8
  end
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module QueryMethodsExtend
2
- VERSION = "0.0.5"
3
- end
2
+ VERSION = '1.0.0'
3
+ end
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "activerecord", ">= 3.0"
21
+ spec.add_development_dependency "activerecord", ">= 3.0"
22
+ spec.add_development_dependency 'rails', '~> 4.0'
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.7"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "active_record"
2
+ require "rails"
2
3
  require "supports/migrate"
3
4
  # This file was generated by the `rspec --init` command. Conventionally, all
4
5
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
@@ -1,13 +1,15 @@
1
1
  require "query_methods_extend"
2
- require "query_methods_extend/basic"
2
+ # require "query_methods_extend/railtie"
3
3
  require "query_methods_extend/association"
4
4
  require "query_methods_extend/like"
5
5
  require "query_methods_extend/operators"
6
6
  require "query_methods_extend/or"
7
7
  require "query_methods_extend/union"
8
- require 'models/book.rb'
9
- require 'models/category.rb'
10
- require 'models/store.rb'
8
+ require "query_methods_extend/activerecord"
9
+
10
+ require 'models/book'
11
+ require 'models/category'
12
+ require 'models/store'
11
13
 
12
14
  def run_migrate
13
15
  ActiveRecord::Base.establish_connection(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_methods_extend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alicuche
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,13 +17,27 @@ dependencies:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
- type: :runtime
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -99,11 +113,12 @@ files:
99
113
  - lib/query_methods_extend/like.rb
100
114
  - lib/query_methods_extend/operators.rb
101
115
  - lib/query_methods_extend/or.rb
102
- - lib/query_methods_extend/spec/association.rb
116
+ - lib/query_methods_extend/railtie.rb
117
+ - lib/query_methods_extend/spec/association_spec.rb
103
118
  - lib/query_methods_extend/spec/like_spec.rb
104
- - lib/query_methods_extend/spec/operators.rb
105
- - lib/query_methods_extend/spec/or.rb
106
- - lib/query_methods_extend/spec/union.rb
119
+ - lib/query_methods_extend/spec/operators_spec.rb
120
+ - lib/query_methods_extend/spec/or_spec.rb
121
+ - lib/query_methods_extend/spec/union_spec.rb
107
122
  - lib/query_methods_extend/union.rb
108
123
  - lib/query_methods_extend/version.rb
109
124
  - query_methods_extend.gemspec