enum_fields 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +76 -10
- data/___release.md +1 -1
- data/lib/enum_fields/enum_field.rb +12 -6
- data/lib/enum_fields/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4aba8fff03dbdf4c90cbe1b6ab1237ae6c06173b90b121e4b8766d5d43999b2e
|
|
4
|
+
data.tar.gz: 949a56d075cca6f44244cd72e9e31da089f4752793eb905758cdff3c32a1cee7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 586be756530f00da32796e169e531b1d0ca0233d18b1993997f4d38686e797d1e51e8acb85daaa2ebafbad25cbdb7f604c2bd07b2592a99f203622d3591a9095
|
|
7
|
+
data.tar.gz: a6f73682ed1cf7f02cdec75ed3e8116c42f9c950428b029eb0b7a87542e2b5ad8309fec276853000e5f8bb055e6e0dbc63642748b0f3542ec5251821ead55255
|
data/README.md
CHANGED
|
@@ -103,16 +103,6 @@ This automatically generates:
|
|
|
103
103
|
}
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
#### Custom Column Mapping
|
|
107
|
-
|
|
108
|
-
If your accessor name differs from your column name:
|
|
109
|
-
|
|
110
|
-
```ruby
|
|
111
|
-
class User < ApplicationRecord
|
|
112
|
-
enum_field :role, definitions, column: :user_role
|
|
113
|
-
end
|
|
114
|
-
```
|
|
115
|
-
|
|
116
106
|
### Generated Methods
|
|
117
107
|
|
|
118
108
|
For an enum field defined as:
|
|
@@ -227,6 +217,82 @@ Campaign.completed_stage
|
|
|
227
217
|
|
|
228
218
|
Automatically validates that the column value is included in the defined values (with `allow_nil: true`).
|
|
229
219
|
|
|
220
|
+
### Options
|
|
221
|
+
|
|
222
|
+
#### `column`
|
|
223
|
+
|
|
224
|
+
Map the accessor to a different database column name:
|
|
225
|
+
|
|
226
|
+
```ruby
|
|
227
|
+
enum_field :role, definitions, column: :user_role
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### `scope`
|
|
231
|
+
|
|
232
|
+
Controls whether query scopes are generated. Defaults to `true`. Set to `false` to skip scope generation:
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
enum_field :speed, definitions, scope: false
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### `validate`
|
|
239
|
+
|
|
240
|
+
Controls whether inclusion validation is added. Defaults to `true`. Set to `false` to skip validation:
|
|
241
|
+
|
|
242
|
+
```ruby
|
|
243
|
+
enum_field :speed, definitions, validate: false
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Virtual Attributes
|
|
247
|
+
|
|
248
|
+
`enum_field` works with computed/virtual attributes that aren't backed by a database column. Define a method on the model and use `scope: false` and `validate: false` since those features require a real column:
|
|
249
|
+
|
|
250
|
+
```ruby
|
|
251
|
+
class Segment < ApplicationRecord
|
|
252
|
+
enum_field :size_category, {
|
|
253
|
+
small: {
|
|
254
|
+
value: "small",
|
|
255
|
+
label: "Small (< 100)",
|
|
256
|
+
},
|
|
257
|
+
medium: {
|
|
258
|
+
value: "medium",
|
|
259
|
+
label: "Medium (< 1K)",
|
|
260
|
+
},
|
|
261
|
+
large: {
|
|
262
|
+
value: "large",
|
|
263
|
+
label: "Large (< 10K)",
|
|
264
|
+
},
|
|
265
|
+
}, scope: false, validate: false
|
|
266
|
+
|
|
267
|
+
def size_category
|
|
268
|
+
case profiles_count
|
|
269
|
+
when ...100
|
|
270
|
+
"small"
|
|
271
|
+
when 100...1_000
|
|
272
|
+
"medium"
|
|
273
|
+
else
|
|
274
|
+
"large"
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
All instance methods work as expected:
|
|
281
|
+
|
|
282
|
+
```ruby
|
|
283
|
+
segment.size_category # "small"
|
|
284
|
+
segment.size_category_label # "Small (< 100)"
|
|
285
|
+
segment.size_category_metadata # { value: "small", label: "Small (< 100)" }
|
|
286
|
+
segment.small_size_category? # true
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Class methods (options, values, counts) also work normally:
|
|
290
|
+
|
|
291
|
+
```ruby
|
|
292
|
+
Segment.size_category_options # [["Small (< 100)", "small"], ["Medium (< 1K)", "medium"], ...]
|
|
293
|
+
Segment.size_category_values # ["small", "medium", "large"]
|
|
294
|
+
```
|
|
295
|
+
|
|
230
296
|
### Custom Properties
|
|
231
297
|
|
|
232
298
|
You can add any custom properties to your definitions, and the gem will automatically create accessor methods for them:
|
data/___release.md
CHANGED
|
@@ -104,7 +104,7 @@ module EnumFields
|
|
|
104
104
|
column_name = @column_name
|
|
105
105
|
|
|
106
106
|
@model_class.define_method("#{accessor}_metadata") do
|
|
107
|
-
column_value =
|
|
107
|
+
column_value = public_send(column_name)
|
|
108
108
|
return nil if column_value.nil?
|
|
109
109
|
|
|
110
110
|
definitions = self.class.enum_field_for(accessor)
|
|
@@ -120,7 +120,7 @@ module EnumFields
|
|
|
120
120
|
|
|
121
121
|
@definition.properties.each do |property|
|
|
122
122
|
@model_class.define_method("#{accessor}_#{property}") do
|
|
123
|
-
column_value =
|
|
123
|
+
column_value = public_send(column_name)
|
|
124
124
|
return nil if column_value.nil?
|
|
125
125
|
|
|
126
126
|
definitions = self.class.enum_field_for(accessor)
|
|
@@ -145,6 +145,8 @@ module EnumFields
|
|
|
145
145
|
end
|
|
146
146
|
|
|
147
147
|
def define_scopes!
|
|
148
|
+
return unless column_scopeable?
|
|
149
|
+
|
|
148
150
|
column_name = @column_name
|
|
149
151
|
|
|
150
152
|
@definition.each do |key, metadata|
|
|
@@ -192,10 +194,6 @@ module EnumFields
|
|
|
192
194
|
end
|
|
193
195
|
end
|
|
194
196
|
|
|
195
|
-
def column_validatable?
|
|
196
|
-
@definition.present? && @options.fetch(:validate, true)
|
|
197
|
-
end
|
|
198
|
-
|
|
199
197
|
def column_polymorphic_association_name
|
|
200
198
|
return nil unless @model_class.respond_to?(:reflect_on_all_associations)
|
|
201
199
|
|
|
@@ -205,5 +203,13 @@ module EnumFields
|
|
|
205
203
|
|
|
206
204
|
reflection&.name
|
|
207
205
|
end
|
|
206
|
+
|
|
207
|
+
def column_scopeable?
|
|
208
|
+
@definition.present? && @options.fetch(:scope, true)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def column_validatable?
|
|
212
|
+
@definition.present? && @options.fetch(:validate, true)
|
|
213
|
+
end
|
|
208
214
|
end
|
|
209
215
|
end
|
data/lib/enum_fields/version.rb
CHANGED