default_where 2.2.1 → 2.2.2

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
  SHA256:
3
- metadata.gz: 415c588b05d60281fb9ce000d231a468ff9bf924f08a91a0d5ab062f4b8c03db
4
- data.tar.gz: 2663281cf4596d7a59eda6dd0866d589580d38f2a2c3bd547a815800041b8560
3
+ metadata.gz: 4925afe89ab55b52bb5945813486b4aeefaa3f29e9a2ed93ca0ca3982076df39
4
+ data.tar.gz: 9b564d353c2b3e7f578e3773aac664294ccd4d08d092d6b71ecc2aa92d247d31
5
5
  SHA512:
6
- metadata.gz: 8a7456d42265499ebef6ab5df439539e0d6f4c2baae493fdfda5232112a09a1ba67a56c60a0103b44f1643741f576b11510f94e07c34c8f62a83b1c57657b86f
7
- data.tar.gz: 00f9cee62150cefeedc252988d49cd81cd5aea77507e531e6c7c88930d1a95d27d0ce6283ec46c84884c4d4afaa3f10fdf96677c645ec684670456fec0198775
6
+ metadata.gz: ae9e5b07c2b5b8fb269370c98d2a8c3d772d263f646a7fa2b229e1b338436c7a55a1ba6448ff44d39381171d42d0e615b1dd204eabbaa0cd634c350615f293e2
7
+ data.tar.gz: eaa3827a022d87f95a5ecbea489bb87e07b5e6a051981ee21cc94dd130fc115574a8f3e212bb0352619a038bdf8ab4997e0d177ce4ad511115ea22dfee483980
data/README.md CHANGED
@@ -66,6 +66,14 @@ User.default_where(params)
66
66
  User.default_where(params, { allow: [nil] })
67
67
  ```
68
68
 
69
+ ### OR
70
+ ```ruby
71
+ params = {
72
+ 'users.email-not-or-name': 'dhh'
73
+ }
74
+
75
+ ```
76
+
69
77
  ### Auto call strip for string
70
78
  * params
71
79
  ```ruby
data/Rakefile CHANGED
@@ -8,25 +8,29 @@ require 'rdoc/task'
8
8
 
9
9
  RDoc::Task.new(:rdoc) do |rdoc|
10
10
  rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'QueryScope'
11
+ rdoc.title = 'DefaultWhere'
12
12
  rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.rdoc')
13
+ rdoc.rdoc_files.include('README.md')
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
-
18
-
19
-
20
17
  Bundler::GemHelper.install_tasks
21
18
 
22
19
  require 'rake/testtask'
23
20
 
24
- Rake::TestTask.new(:test) do |t|
25
- t.libs << 'lib'
21
+ Rake::TestTask.new(test: :environment) do |t|
26
22
  t.libs << 'test'
27
23
  t.pattern = 'test/**/*_test.rb'
28
24
  t.verbose = false
29
25
  end
30
26
 
27
+ task :environment do
28
+ ActiveRecord::Tasks::DatabaseTasks.database_configuration = YAML.load_file('test/config/database.yml')
29
+ ActiveRecord::Tasks::DatabaseTasks.env = 'test'
30
+ ActiveRecord::Tasks::DatabaseTasks.root = __dir__
31
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = ['test/migrations']
32
+ ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration
33
+ ActiveRecord::Base.establish_connection :test
34
+ end
31
35
 
32
- task default: :test
36
+ load 'active_record/railties/databases.rake'
data/lib/default_or.rb ADDED
@@ -0,0 +1,20 @@
1
+ module DefaultWhere::DefaultOr
2
+
3
+ def default_or(hash)
4
+ return all if hash.blank?
5
+
6
+ keys = hash.keys
7
+ query = where(keys[0] => hash[keys[0]])
8
+
9
+ keys[1..-1].each do |key|
10
+ query = query.or(where(key => hash[key]))
11
+ end
12
+
13
+ query
14
+ end
15
+
16
+ end
17
+
18
+ ActiveSupport.on_load :active_record do
19
+ extend DefaultWhere::DefaultOr
20
+ end
data/lib/default_where.rb CHANGED
@@ -2,12 +2,14 @@ require 'default_where/not'
2
2
  require 'default_where/range'
3
3
  require 'default_where/like'
4
4
  require 'default_where/order'
5
+ require 'default_where/or'
5
6
 
6
7
  module DefaultWhere
7
8
  include DefaultWhere::Not
8
9
  include DefaultWhere::Range
9
10
  include DefaultWhere::Order
10
11
  include DefaultWhere::Like
12
+ #include DefaultWhere::Or
11
13
 
12
14
  REJECT = ['', nil]
13
15
  STRIP = true
@@ -15,8 +17,10 @@ module DefaultWhere
15
17
  def default_where(params = {}, options = {})
16
18
  return all if params.blank?
17
19
 
20
+ params = params.to_h
18
21
  params, refs, tables = params_with_table(params, options)
19
22
 
23
+ #or_params = filter_or(params)
20
24
  range_params = filter_range(params)
21
25
  order_params = filter_order(params)
22
26
  not_params = filter_not(params)
@@ -33,9 +37,9 @@ module DefaultWhere
33
37
 
34
38
  def params_with_table(params = {}, options = {})
35
39
  if options.has_key?(:reject)
36
- default_reject = [options[:reject]].flatten
40
+ default_reject = Array(options[:reject])
37
41
  elsif options.has_key?(:allow)
38
- default_reject = REJECT - [options[:allow]].flatten
42
+ default_reject = REJECT - Array(options[:allow])
39
43
  else
40
44
  default_reject = REJECT
41
45
  end
@@ -44,39 +48,38 @@ module DefaultWhere
44
48
  options[:strip] = STRIP
45
49
  end
46
50
 
47
- params = params.to_h
48
- params.stringify_keys!
49
- params.reject! { |_, value| default_reject.include?(value) }
50
-
51
51
  refs = []
52
52
  tables = []
53
53
  final_params = {}
54
54
 
55
55
  params.each do |key, value|
56
56
  value = value.strip if value.is_a?(String) && options[:strip]
57
+ next if default_reject.include?(value)
58
+ key = key.to_s
57
59
 
58
60
  if key =~ /\./
59
- table, col = key.split('.')
60
- as_model = reflections[table]
61
- f_col, _ = col.split('-')
62
-
63
- if as_model && as_model.klass.column_names.include?(f_col)
64
- final_params["#{as_model.table_name}.#{col}"] = value
65
- tables << as_model.table_name
66
- refs << table.to_sym
67
- elsif connection.data_sources.include? table
68
- final_params["#{table}.#{col}"] = value
69
- tables << table
70
- keys = reflections.select { |_, v| !v.polymorphic? && v.table_name == table }.keys
71
- if keys && keys.size == 1
72
- refs << keys.first.to_sym
61
+ _table, _column = key.split('.')
62
+ _real_column = _column.split('-').first
63
+ _ref = reflections[_table]
64
+ if _ref && _ref.klass.column_names.include?(_real_column)
65
+ _table = _ref.table_name
66
+ elsif connection.data_sources.include?(_table) && connection.column_exists?(_table, _real_column)
67
+ _refs = reflections.select { |_, v| v.table_name == _table && !v.polymorphic? }
68
+ if _refs.size == 1
69
+ _ref = _refs.first
70
+ else
71
+ raise "#{key} makes confused, please use reflection name!"
73
72
  end
73
+ else
74
+ next
74
75
  end
76
+ refs << _ref.name unless refs.include?(_ref.name)
77
+ tables << _table unless tables.include?(_table)
78
+ final_params["#{_table}.#{_column}"] = value
75
79
  else
76
- f_key, _ = key.split('-')
77
- if column_names.include?(f_key)
78
- final_params["#{key}"] = value
79
- end
80
+ _real_column = key.split('-').first
81
+ next unless column_names.include?(_real_column)
82
+ final_params[key] = value
80
83
  end
81
84
  end
82
85
 
@@ -85,4 +88,6 @@ module DefaultWhere
85
88
 
86
89
  end
87
90
 
88
- ActiveRecord::Base.extend DefaultWhere
91
+ ActiveSupport.on_load :active_record do
92
+ extend DefaultWhere
93
+ end
@@ -1,11 +1,12 @@
1
1
  module DefaultWhere
2
2
  module Like
3
+ PATTERN = ['-like', '-rl', '-ll']
3
4
 
4
5
  def like_scope(params)
5
6
  where_string = []
6
7
  where_hash = {}
7
8
 
8
- params.select{ |k, _| k.end_with?('-like') }.each do |key, value|
9
+ params.select{ |k, _| k.end_with?(*PATTERN) }.each do |key, value|
9
10
  real_key = key.sub(/-like$/, '')
10
11
  agent_key = key.gsub(/[-.]/, '_')
11
12
 
@@ -14,7 +15,16 @@ module DefaultWhere
14
15
  end
15
16
 
16
17
  where_string << "#{real_key} like :#{agent_key}"
17
- where_hash.merge! agent_key.to_sym => '%' + value.to_s + '%'
18
+
19
+ if key.end_with?('-ll')
20
+ like_value = value.to_s + '%'
21
+ elsif key.end_with?('-rl')
22
+ like_value = '%' + value.to_s
23
+ else
24
+ like_value = '%' + value.to_s + '%'
25
+ end
26
+
27
+ where_hash.merge! agent_key.to_sym => like_value
18
28
  end
19
29
 
20
30
  where_string = where_string.join ' AND '
@@ -29,7 +39,7 @@ module DefaultWhere
29
39
 
30
40
  def filter_like(params)
31
41
  params.select do |k, _|
32
- k.end_with?('-like')
42
+ k.end_with?(*PATTERN)
33
43
  end
34
44
  end
35
45
 
@@ -0,0 +1,32 @@
1
+ module DefaultWhere
2
+ module Or
3
+
4
+ def or_scope(params)
5
+ where_hash = {}
6
+
7
+ params.each do |key, value|
8
+ real_key = key.split('-or-')
9
+
10
+ if column_names.include?(real_key)
11
+ real_key = "#{table_name}.#{real_key}"
12
+ end
13
+
14
+ where_hash.merge! real_key => value
15
+ end
16
+
17
+ if where_hash.present?
18
+ where.not(where_hash)
19
+ else
20
+ all
21
+ end
22
+ end
23
+
24
+ def filter_or(params)
25
+ params.select do |k, _|
26
+ k.include?('-or-')
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -1,3 +1,3 @@
1
1
  module DefaultWhere
2
- VERSION = '2.2.1'
2
+ VERSION = '2.2.2'
3
3
  end
@@ -0,0 +1,32 @@
1
+ default: &default
2
+ adapter: mysql2
3
+ encoding: utf8
4
+ pool: 5
5
+ username: root
6
+ password: root123_ROOT
7
+ socket: /tmp/mysql.sock
8
+
9
+ development:
10
+ <<: *default
11
+ database: default_where_development
12
+
13
+ test:
14
+ <<: *default
15
+ database: default_where_test
16
+
17
+ sqlite:
18
+ adapter: sqlite3
19
+ database: "file:memdb1?mode=memory&cache=shared"
20
+
21
+ mysql:
22
+ adapter: mysql2
23
+ username: root
24
+ password: root
25
+ database: default_where
26
+
27
+ postgresql:
28
+ adapter: postgresql
29
+ username: postgres
30
+ password:
31
+ database: default_where
32
+ min_messages: ERROR
@@ -0,0 +1,32 @@
1
+ default: &default
2
+ adapter: mysql2
3
+ encoding: utf8
4
+ pool: 5
5
+ username: root
6
+ password: root123_ROOT
7
+ socket: /tmp/mysql.sock
8
+
9
+ development:
10
+ <<: *default
11
+ database: default_where_development
12
+
13
+ test:
14
+ <<: *default
15
+ database: default_where_test
16
+
17
+ sqlite:
18
+ adapter: sqlite3
19
+ database: "file:memdb1?mode=memory&cache=shared"
20
+
21
+ mysql:
22
+ adapter: mysql2
23
+ username: root
24
+ password: root
25
+ database: default_where
26
+
27
+ postgresql:
28
+ adapter: postgresql
29
+ username: postgres
30
+ password:
31
+ database: default_where
32
+ min_messages: ERROR
@@ -1,16 +1,19 @@
1
1
  require 'helper'
2
+ require 'models/user'
2
3
 
3
4
  class DefaultWhereTest < ActiveSupport::TestCase
4
5
 
5
- test "truth" do
6
+ test 'truth' do
6
7
  assert_kind_of Module, DefaultWhere
7
8
  end
8
9
 
9
- test "" do
10
+ test 'basic' do
11
+ create :user
10
12
  params = { id: 1, uid: 2, name: 3 }
11
13
  options = { signs: 'name' }
12
14
 
13
-
15
+ count = User.default_where(name: 'qinmingyuan').count
16
+ assert_equal 1, count
14
17
  end
15
18
 
16
19
  end
@@ -0,0 +1,7 @@
1
+ FactoryBot.define do
2
+
3
+ factory :user do
4
+ name 'qinmingyuan'
5
+ end
6
+
7
+ end
data/test/helper.rb CHANGED
@@ -1,24 +1,36 @@
1
1
  # $DEBUG = true
2
2
 
3
- require "rubygems"
4
- require "bundler/setup"
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
5
  begin
6
- Bundler.setup(:default, :development)
6
+ Bundler.setup(:default, :test, :development)
7
7
  rescue Bundler::BundlerError => e
8
8
  $stderr.puts e.message
9
9
  $stderr.puts "Run `bundle install` to install missing gems"
10
10
  exit e.status_code
11
11
  end
12
- require "active_record"
13
- require "minitest/autorun"
12
+ require 'active_record'
13
+ require 'minitest/autorun'
14
+ require 'factory_bot'
14
15
 
15
- db_config = YAML.load_file(File.expand_path("../database.yml", __FILE__)).fetch(ENV["DB"] || "sqlite")
16
- ActiveRecord::Base.establish_connection(db_config)
17
16
  ActiveRecord::Schema.verbose = false
17
+ ActiveRecord::Tasks::DatabaseTasks.database_configuration = YAML.load_file('test/config/database.yml')
18
+ ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration
19
+ ActiveRecord::Base.establish_connection :test
20
+
21
+ if defined?(FactoryBot)
22
+ FactoryBot.definition_file_paths << File.expand_path('test/factories', __dir__)
23
+ FactoryBot.find_definitions
24
+ end
25
+
26
+ class ActiveSupport::TestCase
27
+ include FactoryBot::Syntax::Methods
28
+ end
18
29
 
19
30
  def teardown_db
20
31
  tables = ActiveRecord::Base.connection.data_sources
21
32
  tables.each do |table|
22
- ActiveRecord::Base.connection.drop_table(table)
33
+ ActiveRecord::Base.connection.truncate(table)
23
34
  end
24
- end
35
+ end
36
+ teardown_db
@@ -0,0 +1,12 @@
1
+ class TestInit < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ end
6
+
7
+ create_table :posts do |t|
8
+ t.string :title
9
+ t.references :user
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'default_where'
3
+ class User < ActiveRecord::Base
4
+ extend DefaultWhere
5
+
6
+ end
7
+
metadata CHANGED
@@ -1,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: default_where
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - qinmingyuan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-04 00:00:00.000000000 Z
11
+ date: 2018-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '5.0'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
19
  version: '4.0'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '5.0'
30
27
  - - ">="
31
28
  - !ruby/object:Gem::Version
32
29
  version: '4.0'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rdoc
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: factory_bot_rails
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
61
75
  description: Description of QueryScope.
62
76
  email:
63
77
  - mingyuan0715@foxmail.com
@@ -65,21 +79,26 @@ executables: []
65
79
  extensions: []
66
80
  extra_rdoc_files: []
67
81
  files:
68
- - MIT-LICENSE
69
82
  - README.md
70
83
  - Rakefile
84
+ - lib/default_or.rb
71
85
  - lib/default_where.rb
72
86
  - lib/default_where/like.rb
73
87
  - lib/default_where/not.rb
88
+ - lib/default_where/or.rb
74
89
  - lib/default_where/order.rb
75
90
  - lib/default_where/range.rb
76
91
  - lib/default_where/version.rb
77
- - test/database.yml
92
+ - test/config/database.yml
93
+ - test/config/database.yml.example
78
94
  - test/default_where_test.rb
95
+ - test/factories/users.rb
79
96
  - test/helper.rb
97
+ - test/migrations/20180508082145_test_init.rb
98
+ - test/models/user.rb
80
99
  homepage: https://github.com/qinmingyuan/default_where
81
100
  licenses:
82
- - MIT
101
+ - LGPL-3.0
83
102
  metadata: {}
84
103
  post_install_message:
85
104
  rdoc_options: []
@@ -97,11 +116,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
116
  version: '0'
98
117
  requirements: []
99
118
  rubyforge_project:
100
- rubygems_version: 2.7.6
119
+ rubygems_version: 2.7.7
101
120
  signing_key:
102
121
  specification_version: 4
103
122
  summary: default process params for where
104
123
  test_files:
105
124
  - test/default_where_test.rb
125
+ - test/migrations/20180508082145_test_init.rb
126
+ - test/config/database.yml.example
127
+ - test/config/database.yml
106
128
  - test/helper.rb
107
- - test/database.yml
129
+ - test/models/user.rb
130
+ - test/factories/users.rb
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2015 qinmingyuan<mingyuan0715@foxmail.com>
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/test/database.yml DELETED
@@ -1,16 +0,0 @@
1
- sqlite:
2
- adapter: sqlite3
3
- database: "file:memdb1?mode=memory&cache=shared"
4
-
5
- mysql:
6
- adapter: mysql2
7
- username: root
8
- password:
9
- database: default_where
10
-
11
- postgresql:
12
- adapter: postgresql
13
- username: postgres
14
- password:
15
- database: default_where
16
- min_messages: ERROR