default_where 2.1.0 → 2.2.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
- SHA1:
3
- metadata.gz: bab39ef3d31eaf2792f44350e22cdc97ea35d3e3
4
- data.tar.gz: fd311a5d219d7cf5b5ee6ecf5f3240fd9b79408c
2
+ SHA256:
3
+ metadata.gz: 2b85a4951acd26f1a6797d490d439eee08d8c67e3ffb85b448b575e397944198
4
+ data.tar.gz: 4170d4c7ef435d25c74ba47baf650d55b2bf452552be983a853df4be470a7034
5
5
  SHA512:
6
- metadata.gz: ae8a24b39eb7fa0a412139fb9e0be1506e9753c5c10969dbe169a4fe50c35c30b6973cfd1e9649f146ad2ad706d8351d0bfa479dc4a21d30e39d34e093c81f41
7
- data.tar.gz: 46bc20372f0a1f118fe25ec723ab29d2b64f9c9e4568bbdd82ac7e46568848abcebcc8aada55e763a40e9cd72832bc3e8e7600bb9f0ee0e77607ef2ecf4c4829
6
+ metadata.gz: fdd9c0a69cb31188ce01230ace81e618e4c14ad92f88f9e6622d2857c253ca6363c34d17bd5c1269cf8a7c7f7338e4a4d463cfde03950616e274ad6dd6129de3
7
+ data.tar.gz: eb48499fae1cbe71cb4adb659982b31145a1f29a3d22ce950e25735ac4a2df6e3614641b9b419da35e677a75b84c22358a166a0da2abaaa77857f18613c70a40
data/README.md CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  This Library set default params process for where query in ActiveRecord
4
4
 
5
+ It's a wise decision to use `default_where` with [default_form](https://github.com/qinmingyuan/default_form) to replace `ransack`
6
+
5
7
  ## Features and Usage
6
8
 
7
9
  ### Normal equal params
8
10
 
9
- - Params:
10
-
11
+ * Params:
11
12
  ```ruby
12
13
  # rails 4 and later, default_where does nothing
13
14
  params = { role_id: 1, age: 20 }
@@ -16,7 +17,7 @@ User.default_where(params)
16
17
 
17
18
  ### Equal params with association
18
19
 
19
- - params
20
+ * params
20
21
  ```ruby
21
22
  User.belongs_to :role
22
23
  params = { name: 'dhh', 'role.id': 2 }
@@ -24,41 +25,40 @@ params = { name: 'dhh', 'role.id': 2 }
24
25
  # you can use any table name or reference name
25
26
  params = { name: 'dhh', 'roles.id': 2 }
26
27
  ```
27
- - Before use `default_where`
28
+ * Before use `default_where`
28
29
  ```ruby
29
30
  User.includes(:student).where(name: params[:name], role: {id: params[:'role.id']})
30
31
  ```
31
- - After Use `default_where`
32
+ * After Use `default_where`
32
33
  ```ruby
33
34
  User.default_where(params)
34
35
  ```
35
36
 
36
37
  ### Range params
37
- - params
38
+ * params
38
39
  ```ruby
39
40
  params = { 'role_id-lte': 2 }
40
41
  ```
41
- - Before use `default_where`
42
+ * Before use `default_where`
42
43
  ```ruby
43
44
  User.where('role_id >= ?', params[:'role_id-lte'])
44
45
  ```
45
- - After use `default_where`
46
+ * After use `default_where`
46
47
  ```ruby
47
48
  User.default_where(params)
48
49
  ```
49
50
 
50
51
  ### Auto remove blank params by default, no need write query with `if else`
51
- - params
52
+ * params
52
53
  ```ruby
53
54
  params = { age: '', role_id: 1 }
54
55
  ```
55
-
56
- - Before use `default_where`
56
+ * Before use `default_where`
57
57
  ```ruby
58
58
  users = User.where(role_id: params[:role_id])
59
59
  users = users.where(age: params[:age]) if params[:age]
60
60
  ```
61
- - After use `default_where`
61
+ * After use `default_where`
62
62
  ```ruby
63
63
  User.default_where(params)
64
64
 
@@ -66,30 +66,47 @@ User.default_where(params)
66
66
  User.default_where(params, { allow: [nil] })
67
67
  ```
68
68
 
69
+ ### Auto call strip for string
70
+ * params
71
+ ```ruby
72
+ params= { name: ' dhh ' }
73
+ ```
74
+ * Before use `default_where`
75
+ ```ruby
76
+ User.where(name: params[:name].strip)
77
+ ```
78
+ * After use `default_where`
79
+ ```ruby
80
+ User.default_where(params)
81
+
82
+ # also can control whether use strip
83
+ User.default_where(params, { strip: false })
84
+ ```
85
+
69
86
  ### Order params
70
- - Params
87
+ * Params
71
88
  ```ruby
72
89
  params = { 'age-asc': '1', 'last_login_at-asc': '2' }
73
90
  ```
74
- - Before use `default_where`
91
+ * Before use `default_where`
75
92
  ```ruby
76
93
  User.order(age: :asc, last_login_at: :asc)
77
94
  ```
78
- - After use `default_where`
95
+ * After use `default_where`
79
96
  ```ruby
80
97
  User.default_where(params)
81
98
  ```
82
99
 
83
100
  ## A sample with all params above
84
- - Params
101
+ * Params
85
102
  ```ruby
86
103
  { name: 'dhh', 'role.id': 2, 'age-lte': 2, 'age-asc': '1', 'last_login_at-asc': '2' }
87
104
  ```
88
- - Before use `default_where`
105
+ * Before use `default_where`
89
106
  ```ruby
90
107
  User.includes(:role).where(name: params[:name], 'roles.id': params[:'role.id']).order(age: :asc, last_login_at: :asc)
91
108
  ```
92
- - After use `default_where`
109
+ * After use `default_where`
93
110
  ```ruby
94
111
  User.default_where(params)
95
112
  ```
data/lib/default_where.rb CHANGED
@@ -10,7 +10,7 @@ module DefaultWhere
10
10
  include DefaultWhere::Like
11
11
 
12
12
  REJECT = ['', ' ', nil]
13
-
13
+ STRIP = true
14
14
 
15
15
  def default_where(params = {}, options = {})
16
16
  return all if params.blank?
@@ -40,6 +40,10 @@ module DefaultWhere
40
40
  default_reject = REJECT
41
41
  end
42
42
 
43
+ unless options.has_key? :strip
44
+ options[:strip] = STRIP
45
+ end
46
+
43
47
  params = params.to_h
44
48
  params.stringify_keys!
45
49
  params.reject! { |_, value| default_reject.include?(value) }
@@ -48,20 +52,22 @@ module DefaultWhere
48
52
  tables = []
49
53
  final_params = {}
50
54
 
51
- params.each do |key, _|
55
+ params.each do |key, value|
56
+ value = value.strip if value.is_a?(String) && options[:strip]
57
+
52
58
  if key =~ /\./
53
59
  table, col = key.split('.')
54
60
  as_model = reflections[table]
55
61
  f_col, _ = col.split('-')
56
62
 
57
63
  if as_model && as_model.klass.column_names.include?(f_col)
58
- final_params["#{as_model.table_name}.#{col}"] = params[key]
64
+ final_params["#{as_model.table_name}.#{col}"] = value
59
65
  tables << as_model.table_name
60
66
  refs << table.to_sym
61
67
  elsif connection.data_sources.include? table
62
- final_params["#{table}.#{col}"] = params[key]
68
+ final_params["#{table}.#{col}"] = value
63
69
  tables << table
64
- keys = reflections.select { |_, v| v.table_name == table }.keys
70
+ keys = reflections.select { |_, v| !v.polymorphic? && v.table_name == table }.keys
65
71
  if keys && keys.size == 1
66
72
  refs << keys.first.to_sym
67
73
  end
@@ -69,7 +75,7 @@ module DefaultWhere
69
75
  else
70
76
  f_key, _ = key.split('-')
71
77
  if column_names.include?(f_key)
72
- final_params["#{table_name}.#{key}"] = params[key]
78
+ final_params["#{key}"] = value
73
79
  end
74
80
  end
75
81
  end
@@ -7,7 +7,11 @@ module DefaultWhere
7
7
 
8
8
  params.select{ |k, _| k.end_with?('-like') }.each do |key, value|
9
9
  real_key = key.sub(/-like$/, '')
10
- agent_key = key.gsub(/[-\.]/, '_')
10
+ agent_key = key.gsub(/[-.]/, '_')
11
+
12
+ if column_names.include?(real_key)
13
+ real_key = "#{table_name}.#{real_key}"
14
+ end
11
15
 
12
16
  where_string << "#{real_key} like :#{agent_key}"
13
17
  where_hash.merge! agent_key.to_sym => '%' + value.to_s + '%'
@@ -6,7 +6,12 @@ module DefaultWhere
6
6
 
7
7
  params.each do |key, value|
8
8
  real_key = key.sub(/-not$/, '')
9
- where_hash.merge! real_key.to_sym => value
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
10
15
  end
11
16
 
12
17
  if where_hash.present?
@@ -18,7 +18,11 @@ module DefaultWhere
18
18
  options.each do |key, value|
19
19
  exp = Regexp.new(char + '$')
20
20
  real_key = key.sub(exp, '')
21
- agent_key = key.gsub(/[-\.]/, '_')
21
+ agent_key = key.gsub(/[-.]/, '_')
22
+
23
+ if column_names.include?(real_key)
24
+ real_key = "#{table_name}.#{real_key}"
25
+ end
22
26
 
23
27
  where_string << "#{real_key} #{sign} :#{agent_key}"
24
28
 
@@ -1,3 +1,3 @@
1
1
  module DefaultWhere
2
- VERSION = '2.1.0'
2
+ VERSION = '2.2.0'
3
3
  end
data/test/database.yml ADDED
@@ -0,0 +1,16 @@
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
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require 'helper'
2
2
 
3
3
  class DefaultWhereTest < ActiveSupport::TestCase
4
4
 
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ # $DEBUG = true
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require "active_record"
13
+ require "minitest/autorun"
14
+
15
+ db_config = YAML.load_file(File.expand_path("../database.yml", __FILE__)).fetch(ENV["DB"] || "sqlite")
16
+ ActiveRecord::Base.establish_connection(db_config)
17
+ ActiveRecord::Schema.verbose = false
18
+
19
+ def teardown_db
20
+ tables = ActiveRecord::Base.connection.data_sources
21
+ tables.each do |table|
22
+ ActiveRecord::Base.connection.drop_table(table)
23
+ end
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: default_where
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - qinmingyuan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-05 00:00:00.000000000 Z
11
+ date: 2018-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
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: rake
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'
27
55
  description: Description of QueryScope.
28
56
  email:
29
57
  - mingyuan0715@foxmail.com
@@ -40,8 +68,9 @@ files:
40
68
  - lib/default_where/order.rb
41
69
  - lib/default_where/range.rb
42
70
  - lib/default_where/version.rb
71
+ - test/database.yml
43
72
  - test/default_where_test.rb
44
- - test/test_helper.rb
73
+ - test/helper.rb
45
74
  homepage: https://github.com/qinmingyuan/default_where
46
75
  licenses:
47
76
  - MIT
@@ -62,10 +91,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
91
  version: '0'
63
92
  requirements: []
64
93
  rubyforge_project:
65
- rubygems_version: 2.6.11
94
+ rubygems_version: 2.7.6
66
95
  signing_key:
67
96
  specification_version: 4
68
97
  summary: default process params for where
69
98
  test_files:
70
99
  - test/default_where_test.rb
71
- - test/test_helper.rb
100
+ - test/helper.rb
101
+ - test/database.yml
data/test/test_helper.rb DELETED
@@ -1,18 +0,0 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
- ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
- require "rails/test_help"
7
-
8
- # Filter out Minitest backtrace while allowing backtrace from other libraries
9
- # to be shown.
10
- Minitest.backtrace_filter = Minitest::BacktraceFilter.new
11
-
12
- # Load support files
13
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14
-
15
- # Load fixtures from the engine
16
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
17
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
18
- end