human_attributes 0.6.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/.circleci/config.yml +104 -0
- data/.circleci/setup-rubygems.sh +3 -0
- data/.rubocop.yml +65 -594
- data/.ruby-version +1 -1
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +219 -147
- data/Guardfile +4 -4
- data/README.md +127 -26
- data/Rakefile +1 -1
- data/human_attributes.gemspec +11 -7
- 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 +21 -26
- data/lib/human_attributes/formatters/base.rb +1 -0
- data/lib/human_attributes/formatters/enum.rb +25 -0
- data/lib/human_attributes/formatters/enumerize.rb +1 -0
- data/lib/human_attributes/formatters_builder.rb +1 -0
- data/lib/human_attributes/version.rb +1 -1
- data/spec/dummy/Rakefile +1 -1
- data/spec/dummy/app/assets/config/manifest.js +3 -0
- data/spec/dummy/app/assets/stylesheets/application.css +3 -3
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +0 -3
- data/spec/dummy/app/{assets/javascripts → javascript/packs}/application.js +3 -1
- data/spec/dummy/app/jobs/application_job.rb +7 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/models/purchase.rb +20 -11
- data/spec/dummy/app/views/layouts/application.html.erb +10 -9
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/rails +3 -3
- data/spec/dummy/bin/rake +2 -2
- data/spec/dummy/bin/setup +18 -14
- data/spec/dummy/config/application.rb +12 -22
- data/spec/dummy/config/boot.rb +3 -3
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +4 -18
- data/spec/dummy/config/environment.rb +1 -1
- data/spec/dummy/config/environments/development.rb +49 -14
- data/spec/dummy/config/environments/production.rb +63 -22
- data/spec/dummy/config/environments/test.rb +29 -12
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +4 -3
- data/spec/dummy/config/initializers/backtrace_silencers.rb +4 -3
- data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +3 -1
- data/spec/dummy/config/initializers/permissions_policy.rb +11 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +2 -2
- data/spec/dummy/config/locales/en.yml +26 -1
- data/spec/dummy/config/puma.rb +43 -0
- data/spec/dummy/config/routes.rb +1 -54
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +3 -1
- data/spec/dummy/db/migrate/20161113032308_create_purchases.rb +1 -1
- 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 +17 -17
- data/spec/dummy/public/404.html +6 -6
- data/spec/dummy/public/422.html +6 -6
- data/spec/dummy/public/500.html +6 -6
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/spec/factories/purchases.rb +9 -22
- data/spec/dummy/spec/lib/active_record_extension_spec.rb +81 -33
- metadata +104 -38
- data/.hound.yml +0 -4
- data/.travis.yml +0 -15
- data/spec/dummy/README.rdoc +0 -28
- data/spec/dummy/bin/bundle +0 -3
- data/spec/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy/config/secrets.yml +0 -22
data/README.md
CHANGED
@@ -1,23 +1,33 @@
|
|
1
|
-
# Human Attributes
|
1
|
+
# Human Attributes
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/human_attributes.svg)](https://badge.fury.io/rb/human_attributes)
|
3
|
+
[![CircleCI](https://circleci.com/gh/platanus/human_attributes.svg?style=shield)](https://app.circleci.com/pipelines/github/platanus/human_attributes)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/platanus/human_attributes/badge.svg)](https://coveralls.io/github/platanus/human_attributes)
|
2
5
|
|
3
6
|
It's a Gem to convert ActiveRecord models' attributes and methods to human readable representations of these.
|
4
7
|
|
5
|
-
- [
|
6
|
-
- [
|
7
|
-
- [
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
- [
|
15
|
-
|
16
|
-
|
17
|
-
- [
|
18
|
-
- [
|
19
|
-
- [
|
20
|
-
- [
|
8
|
+
- [Human Attributes](#human-attributes)
|
9
|
+
- [Installation](#installation)
|
10
|
+
- [Usage](#usage)
|
11
|
+
- [Formatters](#formatters)
|
12
|
+
- [Numeric](#numeric)
|
13
|
+
- [Date](#date)
|
14
|
+
- [DateTime](#datetime)
|
15
|
+
- [Boolean](#boolean)
|
16
|
+
- [Enumerize](#enumerize)
|
17
|
+
- [Enum](#enum)
|
18
|
+
- [Custom Formatter](#custom-formatter)
|
19
|
+
- [Common Options](#common-options)
|
20
|
+
- [Default](#default)
|
21
|
+
- [Suffix](#suffix)
|
22
|
+
- [Multiple Formatters](#multiple-formatters)
|
23
|
+
- [Humanize Active Record Attributes](#humanize-active-record-attributes)
|
24
|
+
- [Integration with Draper Gem](#integration-with-draper-gem)
|
25
|
+
- [Rake Task](#rake-task)
|
26
|
+
- [Testing](#testing)
|
27
|
+
- [Publishing](#publishing)
|
28
|
+
- [Contributing](#contributing)
|
29
|
+
- [Credits](#credits)
|
30
|
+
- [License](#license)
|
21
31
|
|
22
32
|
## Installation
|
23
33
|
|
@@ -74,7 +84,7 @@ Executing the `humanize` method, inside the class definition, will allow you to
|
|
74
84
|
With...
|
75
85
|
|
76
86
|
```ruby
|
77
|
-
|
87
|
+
purchase = Purchase.new
|
78
88
|
purchase.quantity = 20
|
79
89
|
purchase.commission = 5.3
|
80
90
|
```
|
@@ -105,7 +115,7 @@ And the options to use with numeric types, are the same as in [NumberHelper](htt
|
|
105
115
|
With...
|
106
116
|
|
107
117
|
```ruby
|
108
|
-
|
118
|
+
purchase = Purchase.new
|
109
119
|
purchase.expired_at = "04/06/1984 09:20:00"
|
110
120
|
purchase.created_at = "04/06/1984 09:20:00"
|
111
121
|
purchase.updated_at = "04/06/1984 09:20:00"
|
@@ -144,7 +154,7 @@ purchase.human_updated_at #=> "1984"
|
|
144
154
|
With...
|
145
155
|
|
146
156
|
```ruby
|
147
|
-
|
157
|
+
purchase = Purchase.new
|
148
158
|
purchase.expired_at = "04/06/1984 09:20:00"
|
149
159
|
purchase.created_at = "04/06/1984 09:20:00"
|
150
160
|
purchase.updated_at = "04/06/1984 09:20:00"
|
@@ -183,7 +193,7 @@ purchase.human_updated_at #=> "1984"
|
|
183
193
|
With...
|
184
194
|
|
185
195
|
```ruby
|
186
|
-
|
196
|
+
purchase = Purchase.new
|
187
197
|
purchase.paid = true
|
188
198
|
```
|
189
199
|
|
@@ -217,7 +227,7 @@ purchase.human_paid #=> "No"
|
|
217
227
|
Installing [Enumerize](https://github.com/brainspec/enumerize) gem with...
|
218
228
|
|
219
229
|
```ruby
|
220
|
-
|
230
|
+
purchase = Purchase.new
|
221
231
|
purchase.state = :finished
|
222
232
|
```
|
223
233
|
|
@@ -248,12 +258,92 @@ purchase.state = :finished
|
|
248
258
|
purchase.human_state #=> "F."
|
249
259
|
```
|
250
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
|
+
|
251
341
|
#### Custom Formatter
|
252
342
|
|
253
343
|
With...
|
254
344
|
|
255
345
|
```ruby
|
256
|
-
|
346
|
+
purchase = Purchase.create!
|
257
347
|
```
|
258
348
|
|
259
349
|
And having...
|
@@ -279,7 +369,7 @@ The following options are available to use with all the formatters presented bef
|
|
279
369
|
With...
|
280
370
|
|
281
371
|
```ruby
|
282
|
-
|
372
|
+
purchase = Purchase.new
|
283
373
|
purchase.amount = nil
|
284
374
|
```
|
285
375
|
|
@@ -304,7 +394,7 @@ Useful when you want to define multiple formatters for the same attribute.
|
|
304
394
|
With...
|
305
395
|
|
306
396
|
```ruby
|
307
|
-
|
397
|
+
purchase = Purchase.new
|
308
398
|
purchase.paid = true
|
309
399
|
purchase.amount = 20
|
310
400
|
```
|
@@ -330,7 +420,7 @@ purchase.amount_to_currency #=> "$20" # default suffix
|
|
330
420
|
With...
|
331
421
|
|
332
422
|
```ruby
|
333
|
-
|
423
|
+
purchase = Purchase.new
|
334
424
|
purchase.amount = 20
|
335
425
|
```
|
336
426
|
|
@@ -501,6 +591,17 @@ bundle exec guard
|
|
501
591
|
|
502
592
|
You need to put **all your tests** in the `/human_attributes/spec/dummy/spec/` directory.
|
503
593
|
|
594
|
+
## Publishing
|
595
|
+
|
596
|
+
On master/main branch...
|
597
|
+
|
598
|
+
1. Change `VERSION` in `lib/gemaker/version.rb`.
|
599
|
+
2. Change `Unreleased` title to current version in `CHANGELOG.md`.
|
600
|
+
3. Run `bundle install`.
|
601
|
+
4. Commit new release. For example: `Releasing v0.1.0`.
|
602
|
+
5. Create tag. For example: `git tag v0.1.0`.
|
603
|
+
6. Push tag. For example: `git push origin v0.1.0`.
|
604
|
+
|
504
605
|
## Contributing
|
505
606
|
|
506
607
|
1. Fork it
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ rescue LoadError
|
|
4
4
|
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
5
5
|
end
|
6
6
|
|
7
|
-
APP_RAKEFILE = File.expand_path("
|
7
|
+
APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
|
8
8
|
load "rails/tasks/engine.rake"
|
9
9
|
load "rails/tasks/statistics.rake"
|
10
10
|
Bundler::GemHelper.install_tasks
|
data/human_attributes.gemspec
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
$:.push File.expand_path("
|
1
|
+
$:.push File.expand_path("lib", __dir__)
|
2
2
|
|
3
3
|
# Maintain your gem"s version:
|
4
4
|
require "human_attributes/version"
|
@@ -19,14 +19,18 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
s.test_files = Dir["spec/**/*"]
|
21
21
|
|
22
|
-
s.add_dependency "rails", ">= 4.2.0"
|
23
22
|
s.add_dependency "factory_bot"
|
24
|
-
s.
|
25
|
-
|
23
|
+
s.add_dependency "rails", ">= 4.2.0"
|
24
|
+
|
25
|
+
s.add_development_dependency "coveralls"
|
26
|
+
s.add_development_dependency "draper"
|
27
|
+
s.add_development_dependency "enumerize", "~> 2.1"
|
28
|
+
s.add_development_dependency "guard-rspec", "~> 4.7"
|
26
29
|
s.add_development_dependency "pry"
|
27
30
|
s.add_development_dependency "pry-rails"
|
31
|
+
s.add_development_dependency "rspec_junit_formatter"
|
32
|
+
s.add_development_dependency "rspec-rails"
|
33
|
+
s.add_development_dependency "rubocop", "~> 1.9"
|
34
|
+
s.add_development_dependency "rubocop-rails"
|
28
35
|
s.add_development_dependency "sqlite3"
|
29
|
-
s.add_development_dependency "rspec-rails", "~> 3.4"
|
30
|
-
s.add_development_dependency "guard-rspec", "~> 4.7"
|
31
|
-
s.add_development_dependency "coveralls"
|
32
36
|
end
|
@@ -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,54 +18,47 @@ 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)
|
22
|
+
|
20
23
|
humanize_from_type(col)
|
21
24
|
end
|
22
25
|
end
|
23
26
|
|
24
27
|
def humanizers
|
25
28
|
return [] unless @humanizers
|
29
|
+
|
26
30
|
@humanizers.uniq!
|
27
|
-
@humanizers.
|
31
|
+
@humanizers.select! { |method| method_defined?(method) }
|
28
32
|
@humanizers
|
29
33
|
end
|
30
34
|
|
31
35
|
private
|
32
36
|
|
33
37
|
def humanize_from_type(col)
|
34
|
-
if
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
elsif col.type == :datetime
|
39
|
-
humanize_date(col.name)
|
40
|
-
humanize_datetime(col.name)
|
41
|
-
elsif [:decimal, :float, :integer].include?(col.type)
|
42
|
-
humanize(col.name, delimiter: true)
|
43
|
-
elsif col.type == :boolean
|
44
|
-
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
|
45
42
|
end
|
46
43
|
end
|
47
44
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
humanize(attr_name, datetime: { format: :short, suffix: "to_short_datetime" })
|
57
|
-
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
|
58
53
|
end
|
59
54
|
end
|
60
55
|
end
|
61
56
|
end
|
62
57
|
|
63
|
-
ActiveRecord::Base.
|
58
|
+
ActiveRecord::Base.include(HumanAttributes::Extension)
|
64
59
|
|
65
60
|
begin
|
66
|
-
Draper::Decorator.
|
61
|
+
Draper::Decorator.include(HumanAttributes::Extension)
|
67
62
|
rescue NameError
|
68
63
|
nil
|
69
64
|
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
|
data/spec/dummy/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
2
|
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
3
|
|
4
|
-
|
4
|
+
require_relative "config/application"
|
5
5
|
|
6
6
|
Rails.application.load_tasks
|
@@ -6,9 +6,9 @@
|
|
6
6
|
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
7
|
*
|
8
8
|
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
-
* compiled file so the styles you add here take precedence over styles defined in any
|
10
|
-
*
|
11
|
-
* file per style scope.
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
12
|
*
|
13
13
|
*= require_tree .
|
14
14
|
*= require_self
|
@@ -5,9 +5,11 @@
|
|
5
5
|
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
6
|
//
|
7
7
|
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
-
// compiled file.
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
9
|
//
|
10
10
|
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
11
|
// about supported directives.
|
12
12
|
//
|
13
|
+
//= require rails-ujs
|
14
|
+
//= require activestorage
|
13
15
|
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class ApplicationJob < ActiveJob::Base
|
2
|
+
# Automatically retry jobs that encountered a deadlock
|
3
|
+
# retry_on ActiveRecord::Deadlocked
|
4
|
+
|
5
|
+
# Most jobs are safe to ignore if the underlying records are no longer available
|
6
|
+
# discard_on ActiveJob::DeserializationError
|
7
|
+
end
|