human_attributes 0.1.1 → 0.2.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/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +72 -14
- data/lib/human_attributes/active_record_extension.rb +11 -1
- data/lib/human_attributes/config.rb +5 -0
- data/lib/human_attributes/engine.rb +1 -0
- data/lib/human_attributes/formatters/date.rb +3 -1
- data/lib/human_attributes/formatters/datetime.rb +11 -0
- data/lib/human_attributes/version.rb +1 -1
- data/spec/dummy/spec/lib/active_record_extension_spec.rb +22 -13
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6852d543a6bd4e642ae907b2ad208261151c26d8
|
4
|
+
data.tar.gz: bee00b07e2bb8bbc6dd2995b92c03e3e64fbe42f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d86cc67c665b2ba71de7a755dca9f9955880a726be5161fd21e6fe21bc7d74f16637ea91ac75d69fdb7287812d67b7bddcb11ff8d7f710101fedb4bcc2fb8e2
|
7
|
+
data.tar.gz: a49ef0b112c2a756960b5d8408c0ef13a5f8696f55c3f4d24918a69cd1ec51aa4303875c382b6552ab8e6c2ffebdb9a57951824b790cfdb8d4859dcbe981bd90
|
data/CHANGELOG.md
CHANGED
@@ -2,8 +2,16 @@
|
|
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
|
+
### v0.2.0
|
6
|
+
|
7
|
+
##### Added
|
8
|
+
|
9
|
+
* DateTime formatter
|
10
|
+
|
5
11
|
### v0.1.1
|
6
12
|
|
13
|
+
##### Fixed
|
14
|
+
|
7
15
|
* Broken specs. Shame on me :(
|
8
16
|
|
9
17
|
### v0.1.0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -7,6 +7,7 @@ It's a Gem to convert ActiveRecord models' attributes and methods to human reada
|
|
7
7
|
- [Formatters](#formatters)
|
8
8
|
- [Numeric](#numeric)
|
9
9
|
- [Date](#date)
|
10
|
+
- [DateTime](#datetime)
|
10
11
|
- [Boolean](#boolean)
|
11
12
|
- [Enumerize](#enumerize)
|
12
13
|
- [Custom Formatter](#custom-formatter)
|
@@ -109,13 +110,62 @@ purchase.created_at = "04/06/1984 09:20:00"
|
|
109
110
|
purchase.updated_at = "04/06/1984 09:20:00"
|
110
111
|
```
|
111
112
|
|
113
|
+
And `/your_app/config/locales/en.yml`
|
114
|
+
|
115
|
+
```yaml
|
116
|
+
en:
|
117
|
+
date:
|
118
|
+
formats:
|
119
|
+
default: "%Y-%m-%d"
|
120
|
+
short: "%b %d"
|
121
|
+
```
|
122
|
+
|
112
123
|
And having...
|
113
124
|
|
114
125
|
```ruby
|
115
126
|
class Purchase < ActiveRecord::Base
|
116
127
|
humanize :expired_at, date: { format: :short }
|
117
128
|
humanize :created_at, date: true
|
118
|
-
humanize :updated_at,
|
129
|
+
humanize :updated_at, date: { format: "%Y" }
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
You can do...
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
purchase.human_expired_at #=> "04 Jun"
|
137
|
+
purchase.human_created_at #=> "1984-06-04"
|
138
|
+
purchase.human_updated_at #=> "1984"
|
139
|
+
```
|
140
|
+
|
141
|
+
#### DateTime
|
142
|
+
|
143
|
+
With...
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
pruchase = Purchase.new
|
147
|
+
purchase.expired_at = "04/06/1984 09:20:00"
|
148
|
+
purchase.created_at = "04/06/1984 09:20:00"
|
149
|
+
purchase.updated_at = "04/06/1984 09:20:00"
|
150
|
+
```
|
151
|
+
|
152
|
+
And `/your_app/config/locales/en.yml`
|
153
|
+
|
154
|
+
```yaml
|
155
|
+
en:
|
156
|
+
time:
|
157
|
+
formats:
|
158
|
+
default: "%a, %d %b %Y %H:%M:%S %z"
|
159
|
+
short: "%d %b %H:%M"
|
160
|
+
```
|
161
|
+
|
162
|
+
And having...
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
class Purchase < ActiveRecord::Base
|
166
|
+
humanize :expired_at, datetime: { format: :short }
|
167
|
+
humanize :created_at, datetime: true
|
168
|
+
humanize :updated_at, datetime: { format: "%Y" }
|
119
169
|
end
|
120
170
|
```
|
121
171
|
|
@@ -127,8 +177,6 @@ purchase.human_created_at #=> "Mon, 04 Jun 1984 09:20:00 +0000"
|
|
127
177
|
purchase.human_updated_at #=> "1984"
|
128
178
|
```
|
129
179
|
|
130
|
-
The options you can use with the date type are the same as in [Rails guides](http://guides.rubyonrails.org/v4.2/i18n.html#adding-date-time-formats)
|
131
|
-
|
132
180
|
#### Boolean
|
133
181
|
|
134
182
|
With...
|
@@ -316,17 +364,27 @@ The `humanize_attributes` method will infer from the attribute's data type which
|
|
316
364
|
With our `Purchase` model we will get:
|
317
365
|
|
318
366
|
```ruby
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
367
|
+
human_id => Purchase: #1
|
368
|
+
human_paid => Yes
|
369
|
+
human_commission => 1000.990%
|
370
|
+
human_quantity => 1
|
371
|
+
human_expired_at => Fri, 06 Apr 1984 09:00:00 +0000
|
372
|
+
expired_at_to_short_date => Apr 06
|
373
|
+
expired_at_to_long_date => Apr 06
|
374
|
+
expired_at_to_short_datetime => 06 Apr 09:00
|
375
|
+
expired_at_to_long_datetime => 06 Apr 09:00
|
376
|
+
human_amount => $2,000,000.95
|
377
|
+
human_created_at => Sat, 10 Dec 2016 21:18:15 +0000
|
378
|
+
created_at_to_short_date => Dec 10
|
379
|
+
created_at_to_long_date => Dec 10
|
380
|
+
created_at_to_short_datetime => 10 Dec 21:18
|
381
|
+
created_at_to_long_datetime => 10 Dec 21:18
|
382
|
+
human_updated_at => Sat, 10 Dec 2016 21:18:15 +0000
|
383
|
+
updated_at_to_short_date => Dec 10
|
384
|
+
updated_at_to_long_date => Dec 10
|
385
|
+
updated_at_to_short_datetime => 10 Dec 21:18
|
386
|
+
updated_at_to_long_datetime => 10 Dec 21:18
|
387
|
+
human_state => Pending
|
330
388
|
```
|
331
389
|
|
332
390
|
> You can pass to `humanize_attributes` the option `only: [:attr1, :attr2]` to humanize specific attributes. The `except` option works in similar way.
|
@@ -33,8 +33,11 @@ module HumanAttributes
|
|
33
33
|
def humanize_from_type(col)
|
34
34
|
if col.name == "id"
|
35
35
|
humanize(:id, custom: { formatter: ->(_o, value) { "#{model_name.human}: ##{value}" } })
|
36
|
-
elsif
|
36
|
+
elsif col.type == :date
|
37
37
|
humanize_date(col.name)
|
38
|
+
elsif col.type == :datetime
|
39
|
+
humanize_date(col.name)
|
40
|
+
humanize_datetime(col.name)
|
38
41
|
elsif [:decimal, :float, :integer].include?(col.type)
|
39
42
|
humanize(col.name, delimiter: true)
|
40
43
|
elsif col.type == :boolean
|
@@ -45,6 +48,13 @@ module HumanAttributes
|
|
45
48
|
def humanize_date(attr_name)
|
46
49
|
humanize(attr_name, date: true)
|
47
50
|
humanize(attr_name, date: { format: :short, suffix: "to_short_date" })
|
51
|
+
humanize(attr_name, date: { format: :short, suffix: "to_long_date" })
|
52
|
+
end
|
53
|
+
|
54
|
+
def humanize_datetime(attr_name)
|
55
|
+
humanize(attr_name, datetime: true)
|
56
|
+
humanize(attr_name, datetime: { format: :short, suffix: "to_short_datetime" })
|
57
|
+
humanize(attr_name, datetime: { format: :short, suffix: "to_long_datetime" })
|
48
58
|
end
|
49
59
|
end
|
50
60
|
end
|
@@ -14,6 +14,7 @@ module HumanAttributes
|
|
14
14
|
require_relative "./formatters/base"
|
15
15
|
require_relative "./formatters/numeric"
|
16
16
|
require_relative "./formatters/date"
|
17
|
+
require_relative "./formatters/datetime"
|
17
18
|
require_relative "./formatters/boolean"
|
18
19
|
require_relative "./formatters/enumerize"
|
19
20
|
require_relative "./formatters/custom"
|
@@ -291,47 +291,56 @@ RSpec.describe "ActiveRecordExtension" do
|
|
291
291
|
end
|
292
292
|
end
|
293
293
|
|
294
|
-
context "with date
|
294
|
+
context "with date and datetime formats" do
|
295
295
|
before { purchase.expired_at = "04/06/1984 09:20:00" }
|
296
|
+
before { purchase.created_at = "04/06/1985 09:20:00" }
|
296
297
|
|
297
298
|
context "passing custom format" do
|
298
299
|
before do
|
299
300
|
class Purchase < ActiveRecord::Base
|
300
301
|
humanize :expired_at, date: { format: "%Y" }
|
302
|
+
humanize :created_at, datetime: { format: "%Y" }
|
301
303
|
end
|
302
304
|
end
|
303
305
|
|
304
306
|
it { expect(purchase.human_expired_at).to eq("1984") }
|
307
|
+
it { expect(purchase.human_created_at).to eq("1985") }
|
305
308
|
end
|
306
309
|
|
307
310
|
context "without options" do
|
308
311
|
before do
|
309
312
|
class Purchase < ActiveRecord::Base
|
310
313
|
humanize :expired_at, date: true
|
314
|
+
humanize :created_at, datetime: true
|
311
315
|
end
|
312
316
|
end
|
313
317
|
|
314
|
-
it { expect(purchase.human_expired_at).to eq("
|
318
|
+
it { expect(purchase.human_expired_at).to eq("1984-06-04") }
|
319
|
+
it { expect(purchase.human_created_at).to eq("Tue, 04 Jun 1985 09:20:00 +0000") }
|
315
320
|
end
|
316
321
|
|
317
322
|
context "with short format" do
|
318
323
|
before do
|
319
324
|
class Purchase < ActiveRecord::Base
|
320
325
|
humanize :expired_at, date: { format: :short }
|
326
|
+
humanize :created_at, datetime: { format: :short }
|
321
327
|
end
|
322
328
|
end
|
323
329
|
|
324
|
-
it { expect(purchase.human_expired_at).to eq("
|
330
|
+
it { expect(purchase.human_expired_at).to eq("Jun 04") }
|
331
|
+
it { expect(purchase.human_created_at).to eq("04 Jun 09:20") }
|
325
332
|
end
|
326
333
|
|
327
334
|
context "with long format" do
|
328
335
|
before do
|
329
336
|
class Purchase < ActiveRecord::Base
|
330
337
|
humanize :expired_at, date: { format: :long }
|
338
|
+
humanize :created_at, datetime: { format: :long }
|
331
339
|
end
|
332
340
|
end
|
333
341
|
|
334
|
-
it { expect(purchase.human_expired_at).to eq("June 04, 1984
|
342
|
+
it { expect(purchase.human_expired_at).to eq("June 04, 1984") }
|
343
|
+
it { expect(purchase.human_created_at).to eq("June 04, 1985 09:20") }
|
335
344
|
end
|
336
345
|
end
|
337
346
|
end
|
@@ -352,11 +361,11 @@ RSpec.describe "ActiveRecordExtension" do
|
|
352
361
|
it { expect(purchase.human_commission).to eq("1,000.99") }
|
353
362
|
it { expect(purchase.human_amount).to eq("2,000,000.95") }
|
354
363
|
it { expect(purchase.human_expired_at).to eq("Fri, 06 Apr 1984 09:00:00 +0000") }
|
355
|
-
it { expect(purchase.
|
364
|
+
it { expect(purchase.expired_at_to_short_datetime).to eq("06 Apr 09:00") }
|
356
365
|
it { expect(purchase).to respond_to(:human_created_at) }
|
357
|
-
it { expect(purchase).to respond_to(:
|
366
|
+
it { expect(purchase).to respond_to(:created_at_to_short_datetime) }
|
358
367
|
it { expect(purchase).to respond_to(:human_updated_at) }
|
359
|
-
it { expect(purchase).to respond_to(:
|
368
|
+
it { expect(purchase).to respond_to(:updated_at_to_short_datetime) }
|
360
369
|
end
|
361
370
|
|
362
371
|
context "with only option" do
|
@@ -372,11 +381,11 @@ RSpec.describe "ActiveRecordExtension" do
|
|
372
381
|
it { expect(purchase).not_to respond_to(:human_commission) }
|
373
382
|
it { expect(purchase.human_amount).to eq("2,000,000.95") }
|
374
383
|
it { expect(purchase).not_to respond_to(:human_expired_at) }
|
375
|
-
it { expect(purchase).not_to respond_to(:
|
384
|
+
it { expect(purchase).not_to respond_to(:expired_at_to_short_datetime) }
|
376
385
|
it { expect(purchase).not_to respond_to(:human_created_at) }
|
377
|
-
it { expect(purchase).not_to respond_to(:
|
386
|
+
it { expect(purchase).not_to respond_to(:created_at_to_short_datetime) }
|
378
387
|
it { expect(purchase).not_to respond_to(:human_updated_at) }
|
379
|
-
it { expect(purchase).not_to respond_to(:
|
388
|
+
it { expect(purchase).not_to respond_to(:updated_at_to_short_datetime) }
|
380
389
|
end
|
381
390
|
|
382
391
|
context "with except option" do
|
@@ -392,11 +401,11 @@ RSpec.describe "ActiveRecordExtension" do
|
|
392
401
|
it { expect(purchase.human_commission).to eq("1,000.99") }
|
393
402
|
it { expect(purchase).not_to respond_to(:human_amount) }
|
394
403
|
it { expect(purchase.human_expired_at).to eq("Fri, 06 Apr 1984 09:00:00 +0000") }
|
395
|
-
it { expect(purchase.
|
404
|
+
it { expect(purchase.expired_at_to_short_datetime).to eq("06 Apr 09:00") }
|
396
405
|
it { expect(purchase).to respond_to(:human_created_at) }
|
397
|
-
it { expect(purchase).to respond_to(:
|
406
|
+
it { expect(purchase).to respond_to(:created_at_to_short_datetime) }
|
398
407
|
it { expect(purchase).to respond_to(:human_updated_at) }
|
399
|
-
it { expect(purchase).to respond_to(:
|
408
|
+
it { expect(purchase).to respond_to(:updated_at_to_short_datetime) }
|
400
409
|
end
|
401
410
|
end
|
402
411
|
end
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Platanus
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- lib/human_attributes/formatters/boolean.rb
|
165
165
|
- lib/human_attributes/formatters/custom.rb
|
166
166
|
- lib/human_attributes/formatters/date.rb
|
167
|
+
- lib/human_attributes/formatters/datetime.rb
|
167
168
|
- lib/human_attributes/formatters/enumerize.rb
|
168
169
|
- lib/human_attributes/formatters/numeric.rb
|
169
170
|
- lib/human_attributes/formatters_builder.rb
|