cast_about_for 0.1.0 → 0.1.1

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: a49119ec5a7172e8ee08ea954f8b5463856f8bde
4
- data.tar.gz: 611350d0aa629f2cd032ddc41f05f532192b5593
3
+ metadata.gz: 767ed28a1c4419971645523dc4f7c806995eea5b
4
+ data.tar.gz: 65fe5b3fbc32d318f17a987a75b076e9412c2b72
5
5
  SHA512:
6
- metadata.gz: d960f66b2d079f0c6dc7a333c3e4277982e4ac60ba93c5ab9faf8d24ab8911df1dec868df8e156fb0a64fe77acea3a7739c12aac5ef708d3dcd42a2f27b3e91e
7
- data.tar.gz: 2f2c07a76b98c4b165866ac73e9dc9bce4cbfedf9ed2e29202fb011248683cc7c392205a6e4c66fb7d9ef4f23508ae81f01f78177117b09a15fecbc9fd7ea9e3
6
+ metadata.gz: e8f7def0a88a6d6e7f368faa94eaa80400cc735012c60a37ade4c9c2e1cb4f094754d23a1ee51b51cdab3998b5b49870a374801f66f8f689bf2ad934f6e81431
7
+ data.tar.gz: a4953ee1aaf64abb56975d115871dc2e21d2ad23f8173137ecd0dcf63d23688de4287aa83c1c88cfc06b9d9f1689c3ae2ea1ecaa15d88d52101e33ad017f291b
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
- # source 'https://rubygems.org'
2
- source 'https://ruby.taobao.org/'
1
+ source 'https://rubygems.org'
3
2
 
4
3
  # Specify your gem's dependencies in cast_about_for.gemspec
5
4
  gemspec
data/README.md CHANGED
@@ -38,14 +38,14 @@ You can always use the `#cast_about_for` class method to query:
38
38
 
39
39
  ``` ruby
40
40
  def index
41
- @products = Product.cast_about_for(params, jsonapi: true)
41
+ @products = Product.cast_about_for(params)
42
42
  end
43
43
  ```
44
44
 
45
45
  Want to count records? Simple:
46
46
 
47
47
  ```ruby
48
- Product.cast_about_for(params, jsonapi: true).count
48
+ Product.cast_about_for(params).count
49
49
  ```
50
50
 
51
51
  ## cast_about_for_params Configure
@@ -56,7 +56,7 @@ If you want to use a column query the SQL look like `SELECT "products".* FROM "p
56
56
 
57
57
  ``` ruby
58
58
  # params = {name: 'iPhone'}
59
- # Product.cast_about_for(params, jsonapi: false)
59
+ # Product.cast_about_for(params)
60
60
 
61
61
  class Product < ActiveRecord::Base
62
62
  cast_about_for_params equal: ['name']
@@ -64,11 +64,11 @@ class Product < ActiveRecord::Base
64
64
  # ...
65
65
  end
66
66
  ```
67
- OR you want to alias of the 'name' column
67
+ Or you want to alias of the `name` argument in `params`
68
68
 
69
69
  ``` ruby
70
70
  # params = { nick_name: "iPhone"}
71
- # User.cast_about_for(params, jsonapi: false)
71
+ # Product.cast_about_for(params)
72
72
 
73
73
  class Product < ActiveRecord::Base
74
74
  cast_about_for_params equal: [{name: "nick_name"}]
@@ -77,13 +77,11 @@ class Product < ActiveRecord::Base
77
77
  end
78
78
  ```
79
79
 
80
- !Your `name` of the `params` must be instead of `nick_name`.
81
-
82
- AND you have other columns, you can do it like below.
80
+ And you have other alias arguments , you can do it like below.
83
81
 
84
82
  ``` ruby
85
83
  # params = { nick_name: "iPhone", info: "sales", price: "600"}
86
- # User.cast_about_for(params, jsonapi: false)
84
+ # Product.cast_about_for(params)
87
85
 
88
86
  class Product < ActiveRecord::Base
89
87
  cast_about_for_params equal: [{name: "nick_name"}, {information: "info"}, price]
@@ -99,7 +97,7 @@ If you want to use a column query the SQL look like `SELECT "products".* FROM "p
99
97
 
100
98
  ``` ruby
101
99
  # params = {introduce: 'To'}
102
- # Product.cast_about_for(params, jsonapi: false)
100
+ # Product.cast_about_for(params)
103
101
 
104
102
  class Product < ActiveRecord::Base
105
103
  cast_about_for_params like: ['introduce']
@@ -108,7 +106,7 @@ class Product < ActiveRecord::Base
108
106
  end
109
107
  ```
110
108
 
111
- Suck as `Equal`. If you want alias of 'introduce', you can
109
+ Suck as `Equal`. If you want alias of `introduce` argument, you can
112
110
 
113
111
  ``` ruby
114
112
  class Product < ActiveRecord::Base
@@ -122,26 +120,84 @@ end
122
120
  If you want to use a column query the SQL look like `SELECT "products".* FROM "products" WHERE (production_date >= '2016-07-05 13:09:00')`, you can pass it as an option:
123
121
 
124
122
  ``` ruby
125
- # params = {started_at: '2016-07-05 13:09:00'}
126
- # Product.cast_about_for(params, jsonapi: false)
123
+ # params = {production_started_at: '2016-07-05 13:09:00', by_time: 'production_date'}
124
+ # Product.cast_about_for(params)
125
+
126
+ class Product < ActiveRecord::Base
127
+ cast_about_for_params after: { field: 'by_time', time: "production_started_at" }
128
+
129
+ # ...
130
+ end
131
+ ```
132
+ In addition, if your `params` not include `by_time` option. Like `params = {production_started_at: '2016-07-05 13:09:00'}`, the query column will be set the default column `created_at`.
133
+
134
+ If you want to use multiple column query the SQL look like `SELECT "products".* FROM "products" WHERE (production_date >= '2016-07-05 13:09:00') AND (created_at >= '2016-07-04')`, you can do it like this:
135
+
136
+ ``` ruby
137
+ # params = {production_started_at: '2016-07-05 13:09:00', by_time: 'production_date', create_field: 'created_at', production_created_at: '2016-07-04'}
138
+ # Product.cast_about_for(params)
127
139
 
128
140
  class Product < ActiveRecord::Base
129
- cast_about_for_params after: { production_date: "started_at" }
141
+ cast_about_for_params after: [{field: 'by_time', time: "production_started_at"}, { field: 'create_field', time: "production_created_at"}]
130
142
 
131
143
  # ...
132
144
  end
133
145
  ```
146
+ If you want more columns to query, you can code it like this pattern: `cast_about_for_params after: [{field: 'by_time', time: "production_started_at"}, { field: 'create_field', time: "production_created_at"}, {field: '..', time: '..'}, {...}, ...]`
147
+
148
+ If you want to set the field exact column you can do it like this:
149
+
150
+ ``` ruby
151
+ # params = {production_started_at: '2016-07-05 13:09:00'}
152
+ # Product.cast_about_for(params) # The SQL: `SELECT "products".* FROM "products" WHERE (production_date >= '2016-07-05 13:09:00')`
153
+
154
+ class Product < ActiveRecord::Base
155
+ cast_about_for_params after: [{field: {exact: "production_date"}, time: "production_started_at"}]
156
+
157
+ # ...
158
+ end
159
+ ```
160
+
134
161
 
135
162
  ### Before
136
163
 
164
+ Just like the above `After`.
137
165
  If you want to use a column query the SQL look like `SELECT "products".* FROM "products" WHERE (production_date <= '2016-07-05 13:09:00')`, you can pass it as an option:
138
166
 
139
167
  ``` ruby
140
- # params = {before_at: '2016-07-05 13:09:00'}
141
- # Product.cast_about_for(params, jsonapi: false)
168
+ # params = {production_ended_at: '2016-07-05 13:09:00', by_time: 'production_date'}
169
+ # Product.cast_about_for(params)
170
+
171
+ class Product < ActiveRecord::Base
172
+ cast_about_for_params before: {field: 'by_time', time: 'production_ended_at'}
173
+
174
+ # ...
175
+ end
176
+ ```
177
+ In addition, if your `params` not include `by_time` option. Like `params = {production_ended_at: '2016-07-05 13:09:00'}`, the query column will be set the default column `created_at`.
178
+
179
+ If you want to use multiple column query the SQL look like `SELECT "products".* FROM "products" WHERE (production_date <= '2016-07-05 13:09:00') AND (created_at <= '2016-07-04')`, you can do it like this:
180
+
181
+ ``` ruby
182
+ # params = {production_ended_at: '2016-07-05 13:09:00', by_time: 'production_date', create_field: 'created_at', production_created_at: '2016-07-04'}
183
+ # Product.cast_about_for(params)
142
184
 
143
185
  class Product < ActiveRecord::Base
144
- cast_about_for_params before: { production_date: "before_at" }
186
+ cast_about_for_params after: [{field: 'by_time', time: "production_ended_at"}, { field: 'create_field', time: "production_created_at"}]
187
+
188
+ # ...
189
+ end
190
+ ```
191
+ If you want more columns to query, you can code it like this pattern: `cast_about_for_params after: [{field: 'by_time', time: "production_ended_at"}, { field: 'create_field', time: "production_created_at"}, {field: '..', time: '..'}, {...}, ...]`
192
+
193
+ If you want to set the field exact column you can do it like this:
194
+
195
+ ``` ruby
196
+ # params = {production_ended_at: '2016-07-05 13:09:00'}
197
+ # Product.cast_about_for(params) # The SQL: `SELECT "products".* FROM "products" WHERE (production_date <= '2016-07-05 13:09:00')`
198
+
199
+ class Product < ActiveRecord::Base
200
+ cast_about_for_params after: [{field: {exact: "production_date"}, time: "production_ended_at"}]
145
201
 
146
202
  # ...
147
203
  end
@@ -153,7 +209,7 @@ If you have a column use enum, you can pass it as an option:
153
209
 
154
210
  ``` ruby
155
211
  # params = {category: "food"}
156
- # Product.cast_about_for(params, jsonapi: false)
212
+ # Product.cast_about_for(params)
157
213
 
158
214
  class Product < ActiveRecord::Base
159
215
  enum category: {food: 0}
@@ -163,6 +219,18 @@ class Product < ActiveRecord::Base
163
219
  end
164
220
  ```
165
221
 
222
+ Suck as `Equal`. If you want alias of `category` argument, you can
223
+
224
+ ``` ruby
225
+ # params = {other_name: "food"}
226
+ # Product.cast_about_for(params)
227
+
228
+ class Product < ActiveRecord::Base
229
+ cast_about_for_params enum: [{category: 'other_name'}]
230
+
231
+ # ...
232
+ end
233
+ ```
166
234
  ## Advanced Usage
167
235
 
168
236
  ### JSON API
@@ -171,7 +239,6 @@ If you are using `JSON API`, you can set in the `#cast_about_for`:
171
239
 
172
240
  ```ruby
173
241
  Product.cast_about_for(params, jsonapi: true) # JSON API
174
- Product.cast_about_for(params, jsonapi: false) # or not
175
242
  ```
176
243
 
177
244
  ### Custom Query by Block
@@ -14,10 +14,9 @@ module CastAboutFor
14
14
  raise ArgumentError, "Unknown cast_about_for key: '#{key}" unless CAST_ABOUT_FOR_KEY.include?(key)
15
15
  end
16
16
 
17
- validate_keys = options.slice(*CAST_ABOUT_FOR_KEY)
17
+ validate_keys = options.slice(*CAST_ABOUT_FOR_KEY.first(3))
18
18
 
19
19
  validate_keys.each do |key, value|
20
- next unless value.is_a?(Array)
21
20
  value.each do |attribute|
22
21
  attribute = attribute.is_a?(Hash) ? attribute.first.first : attribute
23
22
  raise ArgumentError, "Unknown column: #{attribute}" unless self.respond_to?(attribute) || self.column_names.include?(attribute.to_s)
@@ -23,7 +23,7 @@ module CastAboutFor
23
23
  def cast_about_for_by_equal search_values, params, seach_model
24
24
  search_values.each do |search_value|
25
25
  search_column, search_name = obtain_value(search_value)
26
- seach_model = seach_model.where("#{search_column} = ?", params[search_name.to_sym]) if params[search_name.to_sym].present?
26
+ seach_model = seach_model.where("#{self.to_s.tableize}.#{search_column} = ?", params[search_name.to_sym]) if params.present? && params[search_name.to_sym].present?
27
27
  end
28
28
  seach_model
29
29
  end
@@ -31,28 +31,39 @@ module CastAboutFor
31
31
  def cast_about_for_by_like search_values, params, seach_model
32
32
  search_values.each do |search_value|
33
33
  search_column, search_name = obtain_value(search_value)
34
- seach_model = seach_model.where("#{search_column} LIKE ?", "%#{params[search_name.to_sym]}%") if params[search_name.to_sym].present?
34
+ seach_model = seach_model.where("#{self.to_s.tableize}.#{search_column} LIKE ?", "%#{params[search_name.to_sym]}%") if params.present? && params[search_name.to_sym].present?
35
35
  end
36
36
  seach_model
37
37
  end
38
38
 
39
39
  def cast_about_for_by_after search_values, params, seach_model
40
- search_column, search_name = obtain_by_star_value(search_values, params[search_values[:field].to_sym])
41
- seach_model = seach_model.after(params[search_name.to_sym].to_datetime, field: "#{self.to_s.tableize}.#{search_column.to_s}") if params[search_name.to_sym].present?
40
+ step = "after"
41
+ if search_values.is_a?(Array)
42
+ search_values.each do |search_value|
43
+ seach_model = find_records_from_by_star(search_value, params, seach_model, step)
44
+ end
45
+ else
46
+ seach_model = find_records_from_by_star(search_values, params, seach_model, step)
47
+ end
42
48
  seach_model
43
49
  end
44
50
 
45
51
  def cast_about_for_by_before search_values, params, seach_model
46
- search_column, search_name = obtain_by_star_value(search_values, params[search_values[:field].to_sym])
47
-
48
- seach_model = seach_model.before(params[search_name.to_sym].to_datetime, field: "#{self.to_s.tableize}.#{search_column.to_s}") if params[search_name.to_sym].present?
52
+ step = "before"
53
+ if search_values.is_a?(Array)
54
+ search_values.each do |search_value|
55
+ seach_model = find_records_from_by_star(search_value, params, seach_model, step)
56
+ end
57
+ else
58
+ seach_model = find_records_from_by_star(search_values, params, seach_model, step)
59
+ end
49
60
  seach_model
50
61
  end
51
62
 
52
63
  def cast_about_for_by_enum search_values, params, seach_model
53
64
  search_values.each do |search_value|
54
65
  search_column, search_name = obtain_value(search_value)
55
- seach_model = seach_model.where("#{search_column} = ?", self.send(search_column.to_s.pluralize.to_sym)[params[search_name.to_sym]]) if params[search_name.to_sym].present?
66
+ seach_model = seach_model.where("#{self.to_s.tableize}.#{search_column} = ?", self.send(search_column.to_s.pluralize.to_sym)[params[search_name.to_sym]]) if params.present? && params[search_name.to_sym].present?
56
67
  end
57
68
  seach_model
58
69
  end
@@ -64,7 +75,14 @@ module CastAboutFor
64
75
  end
65
76
  end
66
77
 
67
- def obtain_by_star_value(value, field)
78
+ def find_records_from_by_star(search_value, params, seach_model, step)
79
+ search_column, search_name = obtain_by_star_value(search_value, params)
80
+ seach_model = seach_model.send("#{step}", params[search_name.to_sym].to_datetime, field: "#{self.to_s.tableize}.#{search_column.to_s}") if params.present? && params[search_name.to_sym].present?
81
+ seach_model
82
+ end
83
+
84
+ def obtain_by_star_value(value, params)
85
+ field = value[:field].present? ? (value[:field].is_a?(Hash) ? value[:field][:exact] : params[value[:field].to_sym]) : nil
68
86
  column = field.present? ? field : :created_at
69
87
  raise ArgumentError, "Unknown column: #{column}" unless self.respond_to?(column) || self.column_names.include?(column.to_s)
70
88
  [column.to_sym, value[:time]]
@@ -1,3 +1,3 @@
1
1
  module CastAboutFor
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cast_about_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - JeskTop
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-21 00:00:00.000000000 Z
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  requirements: []
179
179
  rubyforge_project:
180
- rubygems_version: 2.6.6
180
+ rubygems_version: 2.5.1
181
181
  signing_key:
182
182
  specification_version: 4
183
183
  summary: query ActiveRecord? Easy.