human_attributes 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +11 -11
- data/README.md +90 -9
- data/lib/human_attributes/config.rb +50 -68
- data/lib/human_attributes/engine.rb +1 -0
- data/lib/human_attributes/errors.rb +6 -0
- data/lib/human_attributes/extension.rb +16 -23
- data/lib/human_attributes/formatters/enum.rb +25 -0
- data/lib/human_attributes/version.rb +1 -1
- data/spec/dummy/app/models/purchase.rb +19 -10
- data/spec/dummy/config/locales/en.yml +15 -0
- data/spec/dummy/db/migrate/20211006145358_add_payment_method_to_purchase.rb +5 -0
- data/spec/dummy/db/migrate/20211103114451_add_shipping_to_purchase.rb +5 -0
- data/spec/dummy/db/migrate/20211103114830_add_store_to_purchase.rb +5 -0
- data/spec/dummy/db/schema.rb +4 -3
- data/spec/dummy/spec/factories/purchases.rb +3 -0
- data/spec/dummy/spec/lib/active_record_extension_spec.rb +47 -1
- metadata +9 -4
- data/.hound.yml +0 -4
- data/.travis.yml +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 100442352d961937e384df6bfb4aa8c137be3359048ce9c0359f4a208beb4db8
|
4
|
+
data.tar.gz: 946c6db1955e759761eb9be4c3af1142a6541b600ffec8a80af318e62c381d5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 394cdc7be845cb97ea5dcd4d6f0c2e41a28eb8b660a42297aadb6a3a28145f7477d2e2225b28269845aff8e26858393d019a6aae24bbbb0136a841812788af45
|
7
|
+
data.tar.gz: 2df11b1cc727eef343ce702d559f14e9f6f3c432993b5231051b2343911ca1b26a61f90959df684a924be4ea59db99e7c413ec72da8fb4e0c4197da1b57db367
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,11 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
|
5
|
+
# 0.7.0
|
6
|
+
|
7
|
+
##### Fixed
|
8
|
+
|
9
|
+
* Fix enum translations: move translations under pluralized attribute key.
|
5
10
|
# 0.7.0
|
6
11
|
##### Changed
|
7
12
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
human_attributes (0.7.
|
4
|
+
human_attributes (0.7.1)
|
5
5
|
factory_bot
|
6
6
|
rails (>= 4.2.0)
|
7
7
|
|
@@ -94,12 +94,12 @@ GEM
|
|
94
94
|
enumerize (2.4.0)
|
95
95
|
activesupport (>= 3.2)
|
96
96
|
erubi (1.10.0)
|
97
|
-
factory_bot (6.2.
|
97
|
+
factory_bot (6.2.1)
|
98
98
|
activesupport (>= 5.0.0)
|
99
99
|
ffi (1.15.1)
|
100
100
|
formatador (0.2.5)
|
101
|
-
globalid (0.
|
102
|
-
activesupport (>=
|
101
|
+
globalid (1.0.0)
|
102
|
+
activesupport (>= 5.0)
|
103
103
|
guard (2.17.0)
|
104
104
|
formatador (>= 0.2.4)
|
105
105
|
listen (>= 2.7, < 4.0)
|
@@ -126,12 +126,12 @@ GEM
|
|
126
126
|
lumberjack (1.2.8)
|
127
127
|
mail (2.7.1)
|
128
128
|
mini_mime (>= 0.1.1)
|
129
|
-
marcel (1.0.
|
129
|
+
marcel (1.0.2)
|
130
130
|
method_source (1.0.0)
|
131
131
|
mini_mime (1.0.3)
|
132
132
|
minitest (5.14.4)
|
133
133
|
nenv (0.3.0)
|
134
|
-
nio4r (2.5.
|
134
|
+
nio4r (2.5.8)
|
135
135
|
nokogiri (1.11.7-x86_64-darwin)
|
136
136
|
racc (~> 1.4)
|
137
137
|
nokogiri (1.11.7-x86_64-linux)
|
@@ -232,12 +232,12 @@ GEM
|
|
232
232
|
json (>= 1.8, < 3)
|
233
233
|
simplecov-html (~> 0.10.0)
|
234
234
|
simplecov-html (0.10.2)
|
235
|
-
sprockets (4.
|
235
|
+
sprockets (4.1.1)
|
236
236
|
concurrent-ruby (~> 1.0)
|
237
237
|
rack (> 1, < 3)
|
238
|
-
sprockets-rails (3.
|
239
|
-
actionpack (>=
|
240
|
-
activesupport (>=
|
238
|
+
sprockets-rails (3.4.2)
|
239
|
+
actionpack (>= 5.2)
|
240
|
+
activesupport (>= 5.2)
|
241
241
|
sprockets (>= 3.0.0)
|
242
242
|
sqlite3 (1.4.2)
|
243
243
|
sync (0.5.0)
|
@@ -249,7 +249,7 @@ GEM
|
|
249
249
|
tzinfo (2.0.4)
|
250
250
|
concurrent-ruby (~> 1.0)
|
251
251
|
unicode-display_width (2.0.0)
|
252
|
-
websocket-driver (0.7.
|
252
|
+
websocket-driver (0.7.5)
|
253
253
|
websocket-extensions (>= 0.1.0)
|
254
254
|
websocket-extensions (0.1.5)
|
255
255
|
zeitwerk (2.4.2)
|
data/README.md
CHANGED
@@ -14,6 +14,7 @@ It's a Gem to convert ActiveRecord models' attributes and methods to human reada
|
|
14
14
|
- [DateTime](#datetime)
|
15
15
|
- [Boolean](#boolean)
|
16
16
|
- [Enumerize](#enumerize)
|
17
|
+
- [Enum](#enum)
|
17
18
|
- [Custom Formatter](#custom-formatter)
|
18
19
|
- [Common Options](#common-options)
|
19
20
|
- [Default](#default)
|
@@ -83,7 +84,7 @@ Executing the `humanize` method, inside the class definition, will allow you to
|
|
83
84
|
With...
|
84
85
|
|
85
86
|
```ruby
|
86
|
-
|
87
|
+
purchase = Purchase.new
|
87
88
|
purchase.quantity = 20
|
88
89
|
purchase.commission = 5.3
|
89
90
|
```
|
@@ -114,7 +115,7 @@ And the options to use with numeric types, are the same as in [NumberHelper](htt
|
|
114
115
|
With...
|
115
116
|
|
116
117
|
```ruby
|
117
|
-
|
118
|
+
purchase = Purchase.new
|
118
119
|
purchase.expired_at = "04/06/1984 09:20:00"
|
119
120
|
purchase.created_at = "04/06/1984 09:20:00"
|
120
121
|
purchase.updated_at = "04/06/1984 09:20:00"
|
@@ -153,7 +154,7 @@ purchase.human_updated_at #=> "1984"
|
|
153
154
|
With...
|
154
155
|
|
155
156
|
```ruby
|
156
|
-
|
157
|
+
purchase = Purchase.new
|
157
158
|
purchase.expired_at = "04/06/1984 09:20:00"
|
158
159
|
purchase.created_at = "04/06/1984 09:20:00"
|
159
160
|
purchase.updated_at = "04/06/1984 09:20:00"
|
@@ -192,7 +193,7 @@ purchase.human_updated_at #=> "1984"
|
|
192
193
|
With...
|
193
194
|
|
194
195
|
```ruby
|
195
|
-
|
196
|
+
purchase = Purchase.new
|
196
197
|
purchase.paid = true
|
197
198
|
```
|
198
199
|
|
@@ -226,7 +227,7 @@ purchase.human_paid #=> "No"
|
|
226
227
|
Installing [Enumerize](https://github.com/brainspec/enumerize) gem with...
|
227
228
|
|
228
229
|
```ruby
|
229
|
-
|
230
|
+
purchase = Purchase.new
|
230
231
|
purchase.state = :finished
|
231
232
|
```
|
232
233
|
|
@@ -257,12 +258,92 @@ purchase.state = :finished
|
|
257
258
|
purchase.human_state #=> "F."
|
258
259
|
```
|
259
260
|
|
261
|
+
#### Enum
|
262
|
+
|
263
|
+
Having...
|
264
|
+
|
265
|
+
```ruby
|
266
|
+
class Purchase < ActiveRecord::Base
|
267
|
+
enum payment_method: [:credit, :debit, :cash, :bitcoin]
|
268
|
+
end
|
269
|
+
```
|
270
|
+
|
271
|
+
You can add
|
272
|
+
```ruby
|
273
|
+
class Purchase < ActiveRecord::Base
|
274
|
+
enum payment_method: [:credit, :debit, :cash, :bitcoin]
|
275
|
+
humanize :payment_method, enum: true
|
276
|
+
end
|
277
|
+
```
|
278
|
+
|
279
|
+
And `/your_app/config/locales/en.yml`
|
280
|
+
|
281
|
+
```yaml
|
282
|
+
en:
|
283
|
+
activerecord:
|
284
|
+
attributes:
|
285
|
+
purchase:
|
286
|
+
payment_methods:
|
287
|
+
credit: "Credit Card"
|
288
|
+
debit: "Debit Card"
|
289
|
+
cash: "Cash"
|
290
|
+
bitcoin: "Bitcoin"
|
291
|
+
```
|
292
|
+
|
293
|
+
|
294
|
+
And then you can do...
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
purchase = Purchase.new
|
298
|
+
purchase.payment_method = :debit
|
299
|
+
purchase.human_payment_method #=> "Debit Card"
|
300
|
+
```
|
301
|
+
|
302
|
+
If you want to use the same enum translations for multiple models, use this locales structure:
|
303
|
+
|
304
|
+
`/your_app/config/locales/en.yml`
|
305
|
+
|
306
|
+
```yaml
|
307
|
+
en:
|
308
|
+
enum:
|
309
|
+
default:
|
310
|
+
payment_methods:
|
311
|
+
credit: "Credit Card"
|
312
|
+
debit: "Debit Card"
|
313
|
+
cash: "Cash"
|
314
|
+
bitcoin: "Bitcoin"
|
315
|
+
```
|
316
|
+
|
317
|
+
and then in other models will also work:
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
class Debt < ActiveRecord::Base
|
321
|
+
enum payment_method: [:credit, :debit, :cash, :bitcoin]
|
322
|
+
humanize :payment_method, enum: true
|
323
|
+
end
|
324
|
+
```
|
325
|
+
|
326
|
+
```ruby
|
327
|
+
purchase = Purchase.new
|
328
|
+
purchase.payment_method = :debit
|
329
|
+
|
330
|
+
debt = Debt.new
|
331
|
+
debt.payment_method = :bitcoin
|
332
|
+
|
333
|
+
purchase.human_payment_method #=> "Debit Card"
|
334
|
+
debt.human_payment_method #=> "Bitcoin"
|
335
|
+
```
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
|
260
341
|
#### Custom Formatter
|
261
342
|
|
262
343
|
With...
|
263
344
|
|
264
345
|
```ruby
|
265
|
-
|
346
|
+
purchase = Purchase.create!
|
266
347
|
```
|
267
348
|
|
268
349
|
And having...
|
@@ -288,7 +369,7 @@ The following options are available to use with all the formatters presented bef
|
|
288
369
|
With...
|
289
370
|
|
290
371
|
```ruby
|
291
|
-
|
372
|
+
purchase = Purchase.new
|
292
373
|
purchase.amount = nil
|
293
374
|
```
|
294
375
|
|
@@ -313,7 +394,7 @@ Useful when you want to define multiple formatters for the same attribute.
|
|
313
394
|
With...
|
314
395
|
|
315
396
|
```ruby
|
316
|
-
|
397
|
+
purchase = Purchase.new
|
317
398
|
purchase.paid = true
|
318
399
|
purchase.amount = 20
|
319
400
|
```
|
@@ -339,7 +420,7 @@ purchase.amount_to_currency #=> "$20" # default suffix
|
|
339
420
|
With...
|
340
421
|
|
341
422
|
```ruby
|
342
|
-
|
423
|
+
purchase = Purchase.new
|
343
424
|
purchase.amount = 20
|
344
425
|
```
|
345
426
|
|
@@ -1,74 +1,56 @@
|
|
1
1
|
module HumanAttributes
|
2
2
|
module Config
|
3
|
-
|
4
|
-
{
|
5
|
-
name:
|
6
|
-
category:
|
7
|
-
|
8
|
-
suffix: :to_currency
|
9
|
-
},
|
10
|
-
{
|
11
|
-
name: :number,
|
12
|
-
category: :numeric,
|
13
|
-
formatter: :number_to_human,
|
14
|
-
suffix: :to_human
|
15
|
-
},
|
16
|
-
{
|
17
|
-
name: :size,
|
18
|
-
category: :numeric,
|
19
|
-
formatter: :number_to_human_size,
|
20
|
-
suffix: :to_human_size
|
21
|
-
},
|
22
|
-
{
|
23
|
-
name: :percentage,
|
24
|
-
category: :numeric,
|
25
|
-
formatter: :number_to_percentage,
|
26
|
-
suffix: :to_percentage
|
27
|
-
},
|
28
|
-
{
|
29
|
-
name: :phone,
|
30
|
-
category: :numeric,
|
31
|
-
formatter: :number_to_phone,
|
32
|
-
suffix: :to_phone
|
33
|
-
},
|
34
|
-
{
|
35
|
-
name: :delimiter,
|
36
|
-
category: :numeric,
|
37
|
-
formatter: :number_with_delimiter,
|
38
|
-
suffix: :with_delimiter
|
39
|
-
},
|
40
|
-
{
|
41
|
-
name: :precision,
|
42
|
-
category: :numeric,
|
43
|
-
formatter: :number_with_precision,
|
44
|
-
suffix: :with_precision
|
45
|
-
},
|
46
|
-
{
|
47
|
-
name: :date,
|
48
|
-
category: :date,
|
49
|
-
suffix: :to_human_date
|
50
|
-
},
|
51
|
-
{
|
52
|
-
name: :datetime,
|
53
|
-
category: :datetime,
|
54
|
-
suffix: :to_human_datetime
|
55
|
-
},
|
56
|
-
{
|
57
|
-
name: :boolean,
|
58
|
-
category: :boolean,
|
59
|
-
suffix: :to_human_boolean
|
60
|
-
},
|
61
|
-
{
|
62
|
-
name: :enumerize,
|
63
|
-
category: :enumerize,
|
64
|
-
suffix: :to_human_enum
|
65
|
-
},
|
66
|
-
{
|
67
|
-
name: :custom,
|
68
|
-
category: :custom,
|
69
|
-
suffix: :to_custom_value
|
3
|
+
def self.build_type(name, category, suffix, formatter = nil)
|
4
|
+
config_type = {
|
5
|
+
name: name,
|
6
|
+
category: category,
|
7
|
+
suffix: suffix
|
70
8
|
}
|
71
|
-
|
9
|
+
if formatter
|
10
|
+
config_type[:formatter] = formatter
|
11
|
+
end
|
12
|
+
config_type
|
13
|
+
end
|
14
|
+
|
15
|
+
TYPES = [
|
16
|
+
[:currency, :numeric, :to_currency, :number_to_currency],
|
17
|
+
[:number, :numeric, :to_human, :number_to_human],
|
18
|
+
[:size, :numeric, :to_human_size, :number_to_human_size],
|
19
|
+
[:percentage, :numeric, :to_percentage, :number_to_percentage],
|
20
|
+
[:phone, :numeric, :to_phone, :number_to_phone],
|
21
|
+
[:delimiter, :numeric, :with_delimiter, :number_with_delimiter],
|
22
|
+
[:precision, :numeric, :with_precision, :number_with_precision],
|
23
|
+
[:date, :date, :to_human_date],
|
24
|
+
[:datetime, :datetime, :to_human_datetime],
|
25
|
+
[:boolean, :boolean, :to_human_boolean],
|
26
|
+
[:enumerize, :enumerize, :to_human_enum],
|
27
|
+
[:enum, :enum, :to_human_enum],
|
28
|
+
[:custom, :custom, :to_custom_value]
|
29
|
+
].map { |config_type| build_type(*config_type) }
|
30
|
+
|
31
|
+
OPTIONS = {
|
32
|
+
boolean: [
|
33
|
+
{ boolean: true }
|
34
|
+
],
|
35
|
+
date: [
|
36
|
+
{ date: true },
|
37
|
+
{ date: { format: :short, suffix: "to_short_date" } },
|
38
|
+
{ date: { format: :short, suffix: "to_long_date" } },
|
39
|
+
{ datetime: true },
|
40
|
+
{ datetime: { format: :short, suffix: "to_short_datetime" } },
|
41
|
+
{ datetime: { format: :short, suffix: "to_long_datetime" } }
|
42
|
+
],
|
43
|
+
datetime: [
|
44
|
+
{ datetime: true },
|
45
|
+
{ datetime: { format: :short, suffix: "to_short_datetime" } },
|
46
|
+
{ datetime: { format: :short, suffix: "to_long_datetime" } }
|
47
|
+
],
|
48
|
+
decimal: [{ delimiter: true }],
|
49
|
+
enum: [{ enum: true }],
|
50
|
+
float: [{ delimiter: true }],
|
51
|
+
id: [{ custom: { formatter: ->(_o, value) { "#{_o.model_name.human}: ##{value}" } } }],
|
52
|
+
integer: [{ delimiter: true }]
|
53
|
+
}
|
72
54
|
|
73
55
|
def category_by_type(type)
|
74
56
|
type_config(type)[:category]
|
@@ -17,6 +17,7 @@ module HumanAttributes
|
|
17
17
|
require_relative "./formatters/datetime"
|
18
18
|
require_relative "./formatters/boolean"
|
19
19
|
require_relative "./formatters/enumerize"
|
20
|
+
require_relative "./formatters/enum"
|
20
21
|
require_relative "./formatters/custom"
|
21
22
|
require_relative "./method_builder"
|
22
23
|
require_relative "./extension"
|
@@ -18,6 +18,12 @@ module HumanAttributes
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
class NotEnumAttribute < RuntimeError
|
22
|
+
def initialize
|
23
|
+
super("needs to be a Rails Enum")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
21
27
|
class MissingFormatterOption < RuntimeError
|
22
28
|
def initialize
|
23
29
|
super("custom type needs formatter option with a proc")
|
@@ -7,7 +7,9 @@ module HumanAttributes
|
|
7
7
|
formatters = HumanAttributes::FormattersBuilder.new(attrs, options).build
|
8
8
|
@builder ||= HumanAttributes::MethodBuilder.new(self)
|
9
9
|
@humanizers ||= []
|
10
|
-
formatters.each
|
10
|
+
formatters.each do |formatter|
|
11
|
+
@humanizers << @builder.build(formatter)
|
12
|
+
end
|
11
13
|
end
|
12
14
|
|
13
15
|
def humanize_attributes(options = {})
|
@@ -16,7 +18,7 @@ module HumanAttributes
|
|
16
18
|
columns.each do |col|
|
17
19
|
next if col.name.ends_with?("_id")
|
18
20
|
next if included_attrs && !included_attrs.include?(col.name.to_sym)
|
19
|
-
next if excluded_attrs
|
21
|
+
next if excluded_attrs&.include?(col.name.to_sym)
|
20
22
|
|
21
23
|
humanize_from_type(col)
|
22
24
|
end
|
@@ -33,30 +35,21 @@ module HumanAttributes
|
|
33
35
|
private
|
34
36
|
|
35
37
|
def humanize_from_type(col)
|
36
|
-
if
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
elsif col.type == :datetime
|
41
|
-
humanize_date(col.name)
|
42
|
-
humanize_datetime(col.name)
|
43
|
-
elsif [:decimal, :float, :integer].include?(col.type)
|
44
|
-
humanize(col.name, delimiter: true)
|
45
|
-
elsif col.type == :boolean
|
46
|
-
humanize(col.name, boolean: true)
|
38
|
+
if HumanAttributes::Config::OPTIONS.has_key? humanizer_type(col)
|
39
|
+
HumanAttributes::Config::OPTIONS[humanizer_type(col)].each do |options|
|
40
|
+
humanize(col.name, options)
|
41
|
+
end
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
humanize(attr_name, datetime: { format: :short, suffix: "to_short_datetime" })
|
59
|
-
humanize(attr_name, datetime: { format: :short, suffix: "to_long_datetime" })
|
45
|
+
def humanizer_type(col)
|
46
|
+
type = col.type
|
47
|
+
if col.name == 'id'
|
48
|
+
type = 'id'
|
49
|
+
elsif defined_enums.has_key?(col.name)
|
50
|
+
type = 'enum'
|
51
|
+
end
|
52
|
+
type.to_sym
|
60
53
|
end
|
61
54
|
end
|
62
55
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module HumanAttributes
|
2
|
+
module Formatters
|
3
|
+
class Enum < Base
|
4
|
+
def apply(_instance, value)
|
5
|
+
return unless value
|
6
|
+
|
7
|
+
is_enum_defined = _instance.class.defined_enums.has_key?(attribute.to_s)
|
8
|
+
raise_error('NotEnumAttribute') unless is_enum_defined
|
9
|
+
class_name = _instance.class.name.underscore
|
10
|
+
pluralized_attribute = attribute.to_s.pluralize
|
11
|
+
attr_key = "activerecord.attributes.#{class_name}.#{pluralized_attribute}.#{value}"
|
12
|
+
enum_key = "enum.defaults.#{pluralized_attribute}.#{value}"
|
13
|
+
translate(attr_key, translate(enum_key, value))
|
14
|
+
end
|
15
|
+
|
16
|
+
def translate(key, default)
|
17
|
+
I18n.t(key, { default: default })
|
18
|
+
end
|
19
|
+
|
20
|
+
def enum_defined?(_instance)
|
21
|
+
_instance.class.defined_enums.has_key?(attribute.to_s)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -2,16 +2,19 @@
|
|
2
2
|
#
|
3
3
|
# Table name: purchases
|
4
4
|
#
|
5
|
-
# id
|
6
|
-
# paid
|
7
|
-
# commission
|
8
|
-
# quantity
|
9
|
-
# state
|
10
|
-
# expired_at
|
11
|
-
# amount
|
12
|
-
# description
|
13
|
-
# created_at
|
14
|
-
# updated_at
|
5
|
+
# id :integer not null, primary key
|
6
|
+
# paid :boolean
|
7
|
+
# commission :decimal(, )
|
8
|
+
# quantity :integer
|
9
|
+
# state :string
|
10
|
+
# expired_at :datetime
|
11
|
+
# amount :decimal(, )
|
12
|
+
# description :text
|
13
|
+
# created_at :datetime not null
|
14
|
+
# updated_at :datetime not null
|
15
|
+
# payment_method :integer
|
16
|
+
# shipping :integer
|
17
|
+
# store :integer
|
15
18
|
#
|
16
19
|
|
17
20
|
require "enumerize"
|
@@ -22,9 +25,15 @@ class Purchase < ApplicationRecord
|
|
22
25
|
STATES = %i{pending canceled finished}
|
23
26
|
|
24
27
|
enumerize :state, in: STATES, default: :pending
|
28
|
+
enum payment_method: [:credit, :debit, :cash, :bitcoin]
|
29
|
+
enum shipping: [:standard, :express]
|
30
|
+
enum store: [:physical, :online]
|
25
31
|
|
26
32
|
humanize_attributes
|
27
33
|
humanize :state, enumerize: true
|
28
34
|
humanize :commission, percentage: true
|
29
35
|
humanize :amount, currency: true
|
36
|
+
humanize :payment_method, enum: true
|
37
|
+
humanize :shipping, enum: true
|
38
|
+
humanize :store, enum: true
|
30
39
|
end
|
@@ -33,3 +33,18 @@ en:
|
|
33
33
|
boolean:
|
34
34
|
positive: "Yes"
|
35
35
|
negative: "No"
|
36
|
+
enum:
|
37
|
+
defaults:
|
38
|
+
shippings:
|
39
|
+
standard: "Standard"
|
40
|
+
express: "Express"
|
41
|
+
activerecord:
|
42
|
+
attributes:
|
43
|
+
purchase:
|
44
|
+
payment_methods:
|
45
|
+
credit: "Credit Card"
|
46
|
+
debit: "Debit Card"
|
47
|
+
cash: "Cash"
|
48
|
+
bitcoin: "Bitcoin"
|
49
|
+
|
50
|
+
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -10,8 +10,7 @@
|
|
10
10
|
#
|
11
11
|
# It's strongly recommended that you check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema.define(version:
|
14
|
-
|
13
|
+
ActiveRecord::Schema.define(version: 2021_11_03_114830) do
|
15
14
|
create_table "purchases", force: :cascade do |t|
|
16
15
|
t.boolean "paid"
|
17
16
|
t.decimal "commission"
|
@@ -22,6 +21,8 @@ ActiveRecord::Schema.define(version: 2016_11_13_032308) do
|
|
22
21
|
t.text "description"
|
23
22
|
t.datetime "created_at", null: false
|
24
23
|
t.datetime "updated_at", null: false
|
24
|
+
t.integer "payment_method"
|
25
|
+
t.integer "shipping"
|
26
|
+
t.integer "store"
|
25
27
|
end
|
26
|
-
|
27
28
|
end
|
@@ -293,7 +293,50 @@ RSpec.describe "ActiveRecordExtension" do
|
|
293
293
|
end
|
294
294
|
end
|
295
295
|
|
296
|
-
context "with
|
296
|
+
context "with Enum attributes" do
|
297
|
+
context "with attribute translation" do
|
298
|
+
before do
|
299
|
+
class Purchase < ApplicationRecord
|
300
|
+
humanize :payment_method, enum: true
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
it { expect(purchase.human_payment_method).to eq("Debit Card") }
|
305
|
+
end
|
306
|
+
context "with enum default translation" do
|
307
|
+
before do
|
308
|
+
class Purchase < ApplicationRecord
|
309
|
+
humanize :shipping, enum: true
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
it { expect(purchase.human_shipping).to eq("Express") }
|
314
|
+
end
|
315
|
+
context "without translation" do
|
316
|
+
before do
|
317
|
+
class Purchase < ApplicationRecord
|
318
|
+
humanize :store, enum: true
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
it { expect(purchase.human_store).to eq("online") }
|
323
|
+
end
|
324
|
+
context "with no enum value" do
|
325
|
+
before do
|
326
|
+
class Purchase < ApplicationRecord
|
327
|
+
humanize :quantity, enum: true
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
it do
|
332
|
+
expect { purchase.human_quantity }.to(
|
333
|
+
raise_error(HumanAttributes::Error::NotEnumAttribute)
|
334
|
+
)
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
context "with date and datetime format" do
|
297
340
|
before { purchase.expired_at = "04/06/1984 09:20:00" }
|
298
341
|
before { purchase.created_at = "04/06/1985 09:20:00" }
|
299
342
|
|
@@ -364,6 +407,9 @@ RSpec.describe "ActiveRecordExtension" do
|
|
364
407
|
it { expect(purchase.human_amount).to eq("2,000,000.95") }
|
365
408
|
it { expect(purchase.human_expired_at).to eq("Fri, 06 Apr 1984 09:00:00 +0000") }
|
366
409
|
it { expect(purchase.expired_at_to_short_datetime).to eq("06 Apr 09:00") }
|
410
|
+
it { expect(purchase.human_payment_method).to eq("Debit Card") }
|
411
|
+
it { expect(purchase.human_shipping).to eq("Express") }
|
412
|
+
it { expect(purchase.human_store).to eq("online") }
|
367
413
|
it { expect(purchase).to respond_to(:human_created_at) }
|
368
414
|
it { expect(purchase).to respond_to(:created_at_to_short_datetime) }
|
369
415
|
it { expect(purchase).to respond_to(:human_updated_at) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: human_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Platanus
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: factory_bot
|
@@ -206,11 +206,9 @@ files:
|
|
206
206
|
- ".circleci/setup-rubygems.sh"
|
207
207
|
- ".coveralls.yml"
|
208
208
|
- ".gitignore"
|
209
|
-
- ".hound.yml"
|
210
209
|
- ".rspec"
|
211
210
|
- ".rubocop.yml"
|
212
211
|
- ".ruby-version"
|
213
|
-
- ".travis.yml"
|
214
212
|
- CHANGELOG.md
|
215
213
|
- Gemfile
|
216
214
|
- Gemfile.lock
|
@@ -229,6 +227,7 @@ files:
|
|
229
227
|
- lib/human_attributes/formatters/custom.rb
|
230
228
|
- lib/human_attributes/formatters/date.rb
|
231
229
|
- lib/human_attributes/formatters/datetime.rb
|
230
|
+
- lib/human_attributes/formatters/enum.rb
|
232
231
|
- lib/human_attributes/formatters/enumerize.rb
|
233
232
|
- lib/human_attributes/formatters/numeric.rb
|
234
233
|
- lib/human_attributes/formatters_builder.rb
|
@@ -278,6 +277,9 @@ files:
|
|
278
277
|
- spec/dummy/config/routes.rb
|
279
278
|
- spec/dummy/config/storage.yml
|
280
279
|
- spec/dummy/db/migrate/20161113032308_create_purchases.rb
|
280
|
+
- spec/dummy/db/migrate/20211006145358_add_payment_method_to_purchase.rb
|
281
|
+
- spec/dummy/db/migrate/20211103114451_add_shipping_to_purchase.rb
|
282
|
+
- spec/dummy/db/migrate/20211103114830_add_store_to_purchase.rb
|
281
283
|
- spec/dummy/db/schema.rb
|
282
284
|
- spec/dummy/public/404.html
|
283
285
|
- spec/dummy/public/422.html
|
@@ -359,6 +361,9 @@ test_files:
|
|
359
361
|
- spec/dummy/config/routes.rb
|
360
362
|
- spec/dummy/config/storage.yml
|
361
363
|
- spec/dummy/db/migrate/20161113032308_create_purchases.rb
|
364
|
+
- spec/dummy/db/migrate/20211006145358_add_payment_method_to_purchase.rb
|
365
|
+
- spec/dummy/db/migrate/20211103114451_add_shipping_to_purchase.rb
|
366
|
+
- spec/dummy/db/migrate/20211103114830_add_store_to_purchase.rb
|
362
367
|
- spec/dummy/db/schema.rb
|
363
368
|
- spec/dummy/public/404.html
|
364
369
|
- spec/dummy/public/422.html
|
data/.hound.yml
DELETED
data/.travis.yml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
sudo: false
|
2
|
-
language: ruby
|
3
|
-
rvm:
|
4
|
-
- 2.3.1
|
5
|
-
script:
|
6
|
-
- RAILS_ENV=test bundle exec rake db:create db:migrate
|
7
|
-
- bundle exec rspec spec
|
8
|
-
deploy:
|
9
|
-
provider: rubygems
|
10
|
-
api_key:
|
11
|
-
secure: E6ajLuO5il2IstmXIMo+jVqLN8x5GO7LVLSRolZwxYHOiN28OYJbPvKGKCEoDJ0l8XHcsJJAaaKKGg9rJEtzoqCVDvIyf4Zmp3XW804yVEoO9m69gJRn7TDbVvEFyJDPwWDG0Li9J4FbmyLoubqPT4KjTC8tVzYN7cKo9FbHlvtS1F9DvnmvyYVW/dU3iuQDFFD0ctO32+9TS1RnJ9RJXEFJd2GFX2eIKJgvlSQZeI2ZgXVZ1PSZVKElkIQLMnhDJVy/Zj/wde1eRzU/AXNQi5p+4LLnLOAxiqWCq8LsPufmuDHZlpkXBOjXYPhaLiRZFeKu/ymBGEu/FqXkpu//ANQDKZS5AkhbddAfZcR3+hRiCb5pUKuRX5X912w20ormNePKYwfs//WpZECneL3UaPq4T4msstOTjeBomJ99zvpU7DC1LAVv1ja1e44eHd3lXasVtXOFMJUv2eTncGY+nVqdqyFFTnB59AtloxfTFaGI9e894rE7dpxEzPO3UOVeBodBH8CkZVMG6OWiP7zkF9vqfrrBETiQXlyGaN9XEk8G+y6Rui8eoETXZYJKfXHPZnv56ntTBHoMu7VWdYPVuwuKKIyaOoNJPsIbKRbF7O9XWkb5/HbDPnt8Q+eFEntYP3Jr+YelghAKN2RX0CDhXFUBxxf7Ew7GEvt2tpbHq44=
|
12
|
-
gem: human_attributes
|
13
|
-
on:
|
14
|
-
tags: true
|
15
|
-
repo: platanus/human_attributes
|