human_attributes 0.1.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.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +7 -0
  6. data/Gemfile +14 -0
  7. data/Gemfile.lock +187 -0
  8. data/Guardfile +15 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +402 -0
  11. data/Rakefile +10 -0
  12. data/human_attributes.gemspec +30 -0
  13. data/lib/human_attributes/active_record_extension.rb +53 -0
  14. data/lib/human_attributes/config.rb +92 -0
  15. data/lib/human_attributes/engine.rb +24 -0
  16. data/lib/human_attributes/errors.rb +46 -0
  17. data/lib/human_attributes/formatters/base.rb +27 -0
  18. data/lib/human_attributes/formatters/boolean.rb +10 -0
  19. data/lib/human_attributes/formatters/custom.rb +17 -0
  20. data/lib/human_attributes/formatters/date.rb +9 -0
  21. data/lib/human_attributes/formatters/enumerize.rb +11 -0
  22. data/lib/human_attributes/formatters/numeric.rb +11 -0
  23. data/lib/human_attributes/formatters_builder.rb +44 -0
  24. data/lib/human_attributes/method_builder.rb +20 -0
  25. data/lib/human_attributes/version.rb +3 -0
  26. data/lib/human_attributes.rb +26 -0
  27. data/lib/tasks/human_attributes_tasks.rake +32 -0
  28. data/spec/dummy/README.rdoc +28 -0
  29. data/spec/dummy/Rakefile +6 -0
  30. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  31. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  32. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  33. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  34. data/spec/dummy/app/models/purchase.rb +28 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/spec/dummy/bin/bundle +3 -0
  37. data/spec/dummy/bin/rails +4 -0
  38. data/spec/dummy/bin/rake +4 -0
  39. data/spec/dummy/bin/setup +29 -0
  40. data/spec/dummy/config/application.rb +32 -0
  41. data/spec/dummy/config/boot.rb +5 -0
  42. data/spec/dummy/config/database.yml +25 -0
  43. data/spec/dummy/config/environment.rb +5 -0
  44. data/spec/dummy/config/environments/development.rb +41 -0
  45. data/spec/dummy/config/environments/production.rb +79 -0
  46. data/spec/dummy/config/environments/test.rb +42 -0
  47. data/spec/dummy/config/initializers/assets.rb +11 -0
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  50. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/spec/dummy/config/initializers/inflections.rb +16 -0
  52. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  53. data/spec/dummy/config/initializers/session_store.rb +3 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/spec/dummy/config/locales/en.yml +25 -0
  56. data/spec/dummy/config/routes.rb +56 -0
  57. data/spec/dummy/config/secrets.yml +22 -0
  58. data/spec/dummy/config.ru +4 -0
  59. data/spec/dummy/db/development.sqlite3 +0 -0
  60. data/spec/dummy/db/migrate/20161113032308_create_purchases.rb +15 -0
  61. data/spec/dummy/db/schema.rb +28 -0
  62. data/spec/dummy/db/test.sqlite3 +0 -0
  63. data/spec/dummy/log/development.log +16 -0
  64. data/spec/dummy/log/test.log +25913 -0
  65. data/spec/dummy/public/404.html +67 -0
  66. data/spec/dummy/public/422.html +67 -0
  67. data/spec/dummy/public/500.html +66 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/spec/assets/image.png +0 -0
  70. data/spec/dummy/spec/assets/video.mp4 +0 -0
  71. data/spec/dummy/spec/factories/purchases.rb +26 -0
  72. data/spec/dummy/spec/lib/active_record_extension_spec.rb +402 -0
  73. data/spec/dummy/spec/support/test_helpers.rb +5 -0
  74. data/spec/rails_helper.rb +28 -0
  75. data/spec/spec_helper.rb +9 -0
  76. metadata +293 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c976855d597f75927fa829c1a2199670105a2138
4
+ data.tar.gz: a2b543e73651f4163120846393b829507e0f34f8
5
+ SHA512:
6
+ metadata.gz: 958e5976a6ba8759dd3886d6e66b0fae721fe1a53ebb8f6440098311981ae9036d2451fb28d05e8813e79ffe420e3e215b52b1d8944047aa50a8f076323521c5
7
+ data.tar.gz: 0a7a9209d5d7da99aa7b5a49a1261973169924836f6a1adb0b56f93ca7ae249a4d1a8e8abd13c479ddf8b5487020c7847ad201e9cbe957dfaeb8c94ae83399cd
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/db/*.sqlite3-journal
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
8
+ spec/dummy/.sass-cache
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require rails_helper
3
+ --format=doc
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ### v0.1.0
6
+
7
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in human_attributes.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
data/Gemfile.lock ADDED
@@ -0,0 +1,187 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ human_attributes (0.1.0)
5
+ enumerize (~> 1.1, >= 1.1.1)
6
+ factory_girl_rails (~> 4.6.0)
7
+ rails (~> 4.2, >= 4.2.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionmailer (4.2.7.1)
13
+ actionpack (= 4.2.7.1)
14
+ actionview (= 4.2.7.1)
15
+ activejob (= 4.2.7.1)
16
+ mail (~> 2.5, >= 2.5.4)
17
+ rails-dom-testing (~> 1.0, >= 1.0.5)
18
+ actionpack (4.2.7.1)
19
+ actionview (= 4.2.7.1)
20
+ activesupport (= 4.2.7.1)
21
+ rack (~> 1.6)
22
+ rack-test (~> 0.6.2)
23
+ rails-dom-testing (~> 1.0, >= 1.0.5)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
25
+ actionview (4.2.7.1)
26
+ activesupport (= 4.2.7.1)
27
+ builder (~> 3.1)
28
+ erubis (~> 2.7.0)
29
+ rails-dom-testing (~> 1.0, >= 1.0.5)
30
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
31
+ activejob (4.2.7.1)
32
+ activesupport (= 4.2.7.1)
33
+ globalid (>= 0.3.0)
34
+ activemodel (4.2.7.1)
35
+ activesupport (= 4.2.7.1)
36
+ builder (~> 3.1)
37
+ activerecord (4.2.7.1)
38
+ activemodel (= 4.2.7.1)
39
+ activesupport (= 4.2.7.1)
40
+ arel (~> 6.0)
41
+ activesupport (4.2.7.1)
42
+ i18n (~> 0.7)
43
+ json (~> 1.7, >= 1.7.7)
44
+ minitest (~> 5.1)
45
+ thread_safe (~> 0.3, >= 0.3.4)
46
+ tzinfo (~> 1.1)
47
+ arel (6.0.3)
48
+ builder (3.2.2)
49
+ coderay (1.1.1)
50
+ concurrent-ruby (1.0.2)
51
+ diff-lcs (1.2.5)
52
+ enumerize (1.1.1)
53
+ activesupport (>= 3.2)
54
+ erubis (2.7.0)
55
+ factory_girl (4.5.0)
56
+ activesupport (>= 3.0.0)
57
+ factory_girl_rails (4.6.0)
58
+ factory_girl (~> 4.5.0)
59
+ railties (>= 3.0.0)
60
+ ffi (1.9.14)
61
+ formatador (0.2.5)
62
+ globalid (0.3.7)
63
+ activesupport (>= 4.1.0)
64
+ guard (2.14.0)
65
+ formatador (>= 0.2.4)
66
+ listen (>= 2.7, < 4.0)
67
+ lumberjack (~> 1.0)
68
+ nenv (~> 0.1)
69
+ notiffany (~> 0.0)
70
+ pry (>= 0.9.12)
71
+ shellany (~> 0.0)
72
+ thor (>= 0.18.1)
73
+ guard-compat (1.2.1)
74
+ guard-rspec (4.7.3)
75
+ guard (~> 2.1)
76
+ guard-compat (~> 1.1)
77
+ rspec (>= 2.99.0, < 4.0)
78
+ i18n (0.7.0)
79
+ json (1.8.3)
80
+ listen (3.1.5)
81
+ rb-fsevent (~> 0.9, >= 0.9.4)
82
+ rb-inotify (~> 0.9, >= 0.9.7)
83
+ ruby_dep (~> 1.2)
84
+ loofah (2.0.3)
85
+ nokogiri (>= 1.5.9)
86
+ lumberjack (1.0.10)
87
+ mail (2.6.4)
88
+ mime-types (>= 1.16, < 4)
89
+ method_source (0.8.2)
90
+ mime-types (3.1)
91
+ mime-types-data (~> 3.2015)
92
+ mime-types-data (3.2016.0521)
93
+ mini_portile2 (2.1.0)
94
+ minitest (5.9.1)
95
+ nenv (0.3.0)
96
+ nokogiri (1.6.8.1)
97
+ mini_portile2 (~> 2.1.0)
98
+ notiffany (0.1.1)
99
+ nenv (~> 0.1)
100
+ shellany (~> 0.0)
101
+ pry (0.10.4)
102
+ coderay (~> 1.1.0)
103
+ method_source (~> 0.8.1)
104
+ slop (~> 3.4)
105
+ pry-rails (0.3.4)
106
+ pry (>= 0.9.10)
107
+ rack (1.6.4)
108
+ rack-test (0.6.3)
109
+ rack (>= 1.0)
110
+ rails (4.2.7.1)
111
+ actionmailer (= 4.2.7.1)
112
+ actionpack (= 4.2.7.1)
113
+ actionview (= 4.2.7.1)
114
+ activejob (= 4.2.7.1)
115
+ activemodel (= 4.2.7.1)
116
+ activerecord (= 4.2.7.1)
117
+ activesupport (= 4.2.7.1)
118
+ bundler (>= 1.3.0, < 2.0)
119
+ railties (= 4.2.7.1)
120
+ sprockets-rails
121
+ rails-deprecated_sanitizer (1.0.3)
122
+ activesupport (>= 4.2.0.alpha)
123
+ rails-dom-testing (1.0.7)
124
+ activesupport (>= 4.2.0.beta, < 5.0)
125
+ nokogiri (~> 1.6.0)
126
+ rails-deprecated_sanitizer (>= 1.0.1)
127
+ rails-html-sanitizer (1.0.3)
128
+ loofah (~> 2.0)
129
+ railties (4.2.7.1)
130
+ actionpack (= 4.2.7.1)
131
+ activesupport (= 4.2.7.1)
132
+ rake (>= 0.8.7)
133
+ thor (>= 0.18.1, < 2.0)
134
+ rake (11.3.0)
135
+ rb-fsevent (0.9.7)
136
+ rb-inotify (0.9.7)
137
+ ffi (>= 0.5.0)
138
+ rspec (3.4.0)
139
+ rspec-core (~> 3.4.0)
140
+ rspec-expectations (~> 3.4.0)
141
+ rspec-mocks (~> 3.4.0)
142
+ rspec-core (3.4.4)
143
+ rspec-support (~> 3.4.0)
144
+ rspec-expectations (3.4.0)
145
+ diff-lcs (>= 1.2.0, < 2.0)
146
+ rspec-support (~> 3.4.0)
147
+ rspec-mocks (3.4.1)
148
+ diff-lcs (>= 1.2.0, < 2.0)
149
+ rspec-support (~> 3.4.0)
150
+ rspec-rails (3.4.2)
151
+ actionpack (>= 3.0, < 4.3)
152
+ activesupport (>= 3.0, < 4.3)
153
+ railties (>= 3.0, < 4.3)
154
+ rspec-core (~> 3.4.0)
155
+ rspec-expectations (~> 3.4.0)
156
+ rspec-mocks (~> 3.4.0)
157
+ rspec-support (~> 3.4.0)
158
+ rspec-support (3.4.1)
159
+ ruby_dep (1.5.0)
160
+ shellany (0.0.1)
161
+ slop (3.6.0)
162
+ sprockets (3.7.0)
163
+ concurrent-ruby (~> 1.0)
164
+ rack (> 1, < 3)
165
+ sprockets-rails (3.2.0)
166
+ actionpack (>= 4.0)
167
+ activesupport (>= 4.0)
168
+ sprockets (>= 3.0.0)
169
+ sqlite3 (1.3.12)
170
+ thor (0.19.1)
171
+ thread_safe (0.3.5)
172
+ tzinfo (1.2.2)
173
+ thread_safe (~> 0.1)
174
+
175
+ PLATFORMS
176
+ ruby
177
+
178
+ DEPENDENCIES
179
+ guard-rspec (~> 4.7)
180
+ human_attributes!
181
+ pry
182
+ pry-rails
183
+ rspec-rails (~> 3.4.0)
184
+ sqlite3
185
+
186
+ BUNDLED WITH
187
+ 1.13.3
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ spec_dic = "spec/dummy/spec"
3
+ # RSpec files
4
+ watch("spec/spec_helper.rb") { spec_dic }
5
+ watch("spec/rails_helper.rb") { spec_dic }
6
+ watch(%r{^spec\/dummy\/spec\/support\/(.+)\.rb$}) { spec_dic }
7
+ watch(%r{^spec\/dummy\/spec\/.+_spec\.rb$})
8
+ # Engine files
9
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/dummy/spec/lib/#{m[1]}_spec.rb" }
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/dummy/spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb)$}) { |m| "spec/dummy/spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ # Dummy app files
13
+ watch(%r{^spec\/dummy\/app/(.+)\.rb$}) { |m| "spec/dummy/spec/#{m[1]}_spec.rb" }
14
+ watch(%r{^spec\/dummy\/app/(.*)(\.erb)$}) { |m| "spec/dummy/spec/#{m[1]}#{m[2]}_spec.rb" }
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright 2016 Platanus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,402 @@
1
+ # Human Attributes
2
+
3
+ It's a Gem to convert ActiveRecord models' attributes and methods to human readable representations of these.
4
+
5
+ - [Installation](#installation)
6
+ - [Usage](#usage)
7
+ - [Formatters](#formatters)
8
+ - [Numeric](#numeric)
9
+ - [Date](#date)
10
+ - [Boolean](#boolean)
11
+ - [Enumerize](#enumerize)
12
+ - [Custom Formatter](#custom-formatter)
13
+ - [Common Options](#common-options)
14
+ - [Default](#default)
15
+ - [Suffix](#suffix)
16
+ - [Multiple Formatters](#multiple-formatters)
17
+ - [Humanize Active Record Attributes](#humanize-active-record-attributes)
18
+ - [Rake Task](#rake-task)
19
+
20
+ ## Installation
21
+
22
+ Add to your Gemfile:
23
+
24
+ ```ruby
25
+ gem "human_attributes"
26
+ ```
27
+
28
+ ```bash
29
+ bundle install
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ Suppose you have the following model:
35
+
36
+ ```ruby
37
+ # == Schema Information
38
+ #
39
+ # Table name: purchases
40
+ #
41
+ # id :integer not null, primary key
42
+ # paid :boolean
43
+ # commission :decimal(, )
44
+ # quantity :integer
45
+ # state :string
46
+ # expired_at :datetime
47
+ # amount :decimal(, )
48
+ # description :text
49
+ # created_at :datetime not null
50
+ # updated_at :datetime not null
51
+ #
52
+
53
+ class Purchase < ActiveRecord::Base
54
+ extend Enumerize
55
+
56
+ STATES = %i{pending canceled finished}
57
+
58
+ enumerize :state, in: STATES, default: :pending
59
+
60
+ def commission_amount
61
+ amount * commission / 100.0
62
+ end
63
+ end
64
+ ```
65
+
66
+ Executing the `humanize` method, inside the class definition, will allow you to apply **Formatters** to `Purchase`'s attributes and methods.
67
+
68
+ ### Formatters
69
+
70
+ #### Numeric
71
+
72
+ With...
73
+
74
+ ```ruby
75
+ pruchase = Purchase.new
76
+ purchase.quantity = 20
77
+ purchase.commission = 5.3
78
+ ```
79
+
80
+ And having...
81
+
82
+ ```ruby
83
+ class Purchase < ActiveRecord::Base
84
+ humanize :quantity, percentage: true
85
+ humanize :commission, :commission_amount, currency: { unit: "R$", separator: ",", delimiter: "" }
86
+ end
87
+ ```
88
+
89
+ You can do...
90
+
91
+ ```ruby
92
+ purchase.human_quantity #=> "20.000%"
93
+ purchase.human_commission #=> "R$5,30"
94
+ purchase.human_commission_amount #=> R$1 060 000,03
95
+ ```
96
+
97
+ The available numeric types are:`currency`, `number`, `size`, `percentage`, `phone`, `delimiter` and `precision`.
98
+
99
+ And the options to use with numeric types, are the same as in [NumberHelper](http://api.rubyonrails.org/v4.2/classes/ActionView/Helpers/NumberHelper.html)
100
+
101
+ #### Date
102
+
103
+ With...
104
+
105
+ ```ruby
106
+ pruchase = Purchase.new
107
+ purchase.expired_at = "04/06/1984 09:20:00"
108
+ purchase.created_at = "04/06/1984 09:20:00"
109
+ purchase.updated_at = "04/06/1984 09:20:00"
110
+ ```
111
+
112
+ And having...
113
+
114
+ ```ruby
115
+ class Purchase < ActiveRecord::Base
116
+ humanize :expired_at, date: { format: :short }
117
+ humanize :created_at, date: true
118
+ humanize :updated_at, , date: { format: "%Y" }
119
+ end
120
+ ```
121
+
122
+ You can do...
123
+
124
+ ```ruby
125
+ purchase.human_expired_at #=> "04 Jun 09:20"
126
+ purchase.human_created_at #=> "Mon, 04 Jun 1984 09:20:00 +0000"
127
+ purchase.human_updated_at #=> "1984"
128
+ ```
129
+
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
+ #### Boolean
133
+
134
+ With...
135
+
136
+ ```ruby
137
+ pruchase = Purchase.new
138
+ purchase.paid = true
139
+ ```
140
+
141
+ And `/your_app/config/locales/en.yml`
142
+
143
+ ```yaml
144
+ en:
145
+ boolean:
146
+ positive: "Yes"
147
+ negative: "No"
148
+ ```
149
+
150
+ Having...
151
+
152
+ ```ruby
153
+ class Purchase < ActiveRecord::Base
154
+ humanize :paid, boolean: true
155
+ end
156
+ ```
157
+
158
+ You can do...
159
+
160
+ ```ruby
161
+ purchase.human_paid #=> "Yes"
162
+ purchase.paid = false
163
+ purchase.human_paid #=> "No"
164
+ ```
165
+
166
+ #### Enumerize
167
+
168
+ Installing [Enumerize](https://github.com/brainspec/enumerize) gem with...
169
+
170
+ ```ruby
171
+ pruchase = Purchase.new
172
+ purchase.state = :finished
173
+ ```
174
+
175
+ And `/your_app/config/locales/en.yml`
176
+
177
+ ```yaml
178
+ en:
179
+ enumerize:
180
+ purchase:
181
+ state:
182
+ pending: "P."
183
+ finished: "F."
184
+ canceled: "C."
185
+ ```
186
+
187
+ Having...
188
+
189
+ ```ruby
190
+ class Purchase < ActiveRecord::Base
191
+ humanize :state, enumerize: true
192
+ end
193
+ ```
194
+
195
+ You can do...
196
+
197
+ ```ruby
198
+ purchase.state = :finished
199
+ purchase.human_state #=> "F."
200
+ ```
201
+
202
+ #### Custom Formatter
203
+
204
+ With...
205
+
206
+ ```ruby
207
+ pruchase = Purchase.create!
208
+ ```
209
+
210
+ And having...
211
+
212
+ ```ruby
213
+ class Purchase < ActiveRecord::Base
214
+ humanize :id, custom: { formatter: ->(purchase, value) { "Purchase: #{value}-#{purchase.id}" } }
215
+ end
216
+ ```
217
+
218
+ You can do...
219
+
220
+ ```ruby
221
+ purchase.human_id #=> "Purchase: 1-1"
222
+ ```
223
+
224
+ ### Common Options
225
+
226
+ The following options are available to use with all the formatters presented before.
227
+
228
+ #### Default
229
+
230
+ With...
231
+
232
+ ```ruby
233
+ pruchase = Purchase.new
234
+ purchase.amount = nil
235
+ ```
236
+
237
+ Having...
238
+
239
+ ```ruby
240
+ class Purchase < ActiveRecord::Base
241
+ humanize :amount, currency: { default: 0 }
242
+ end
243
+ ```
244
+
245
+ You can do...
246
+
247
+ ```ruby
248
+ purchase.human_amount #=> "$0"
249
+ ```
250
+
251
+ #### Suffix
252
+
253
+ Useful when you want to define multiple formatters for the same attribute.
254
+
255
+ With...
256
+
257
+ ```ruby
258
+ pruchase = Purchase.new
259
+ purchase.paid = true
260
+ purchase.amount = 20
261
+ ```
262
+
263
+ Having...
264
+
265
+ ```ruby
266
+ class Purchase < ActiveRecord::Base
267
+ humanize :paid, boolean: { suffix: "with_custom_suffix" }
268
+ humanize :amount, currency: { suffix: true }
269
+ end
270
+ ```
271
+
272
+ You can do...
273
+
274
+ ```ruby
275
+ purchase.paid_with_custom_suffix #=> "Yes"
276
+ purchase.amount_to_currency #=> "$20" # default suffix
277
+ ```
278
+
279
+ ### Multiple Formatters
280
+
281
+ With...
282
+
283
+ ```ruby
284
+ pruchase = Purchase.new
285
+ purchase.amount = 20
286
+ ```
287
+
288
+ Having...
289
+
290
+ ```ruby
291
+ class Purchase < ActiveRecord::Base
292
+ humanize :amount, currency: { suffix: true }, percentage: { suffix: true }
293
+ end
294
+ ```
295
+
296
+ You can do...
297
+
298
+ ```ruby
299
+ purchase.amount_to_currency #=> ""
300
+ purchase.amount_to_percentage #=> ""
301
+ ```
302
+
303
+ > Remember to use `:suffix` option to avoid name collisions
304
+
305
+ ### Humanize Active Record Attributes
306
+
307
+ You can generate human representations for all the atributes of your ActiveRecord model like this:
308
+
309
+ ```ruby
310
+ class Purchase < ActiveRecord::Base
311
+ humanize_attributes
312
+ end
313
+ ```
314
+
315
+ The `humanize_attributes` method will infer from the attribute's data type which formatter to choose.
316
+ With our `Purchase` model we will get:
317
+
318
+ ```ruby
319
+ purchase.human_id
320
+ purchase.human_paid
321
+ purchase.human_quantity
322
+ purchase.human_commission
323
+ purchase.human_amount
324
+ purchase.human_expired_at
325
+ purchase.expired_at_to_short_date
326
+ purchase.human_created_at
327
+ purchase.created_at_to_short_date
328
+ purchase.human_updated_at
329
+ purchase.updated_at_to_short_date
330
+ ```
331
+
332
+ > You can pass to `humanize_attributes` the option `only: [:attr1, :attr2]` to humanize specific attributes. The `except` option works in similar way.
333
+
334
+ ### Rake Task
335
+
336
+ You can run, from your terminal, the following task to show defined human attributes for a particular ActiveRecord model.
337
+
338
+ `$ rake human_attrs:show[your-model-name]`
339
+
340
+ So, with...
341
+
342
+ ```ruby
343
+ class Purchase < ActiveRecord::Base
344
+ extend Enumerize
345
+
346
+ STATES = %i{pending canceled finished}
347
+
348
+ enumerize :state, in: STATES, default: :pending
349
+
350
+ humanize_attributes
351
+ humanize :state, enumerize: true
352
+ humanize :commission, percentage: true
353
+ humanize :amount, currency: true
354
+ end
355
+ ```
356
+
357
+ And running `rake human_attrs:show[purchase]`, you will see the following output:
358
+
359
+ ```
360
+ human_id => Purchase: #1
361
+ human_paid => Yes
362
+ human_commission => 1000.990%
363
+ human_quantity => 1
364
+ human_expired_at => Fri, 06 Apr 1984 09:00:00 +0000
365
+ expired_at_to_short_date => 06 Apr 09:00
366
+ human_amount => $2,000,000.95
367
+ human_created_at => Sat, 10 Dec 2016 20:06:28 +0000
368
+ created_at_to_short_date => 10 Dec 20:06
369
+ human_updated_at => Sat, 10 Dec 2016 20:06:28 +0000
370
+ updated_at_to_short_date => 10 Dec 20:06
371
+ human_state => Pending
372
+ ```
373
+
374
+ ## Testing
375
+
376
+ To run the specs you need to execute, **in the root path of the gem**, the following command:
377
+
378
+ ```bash
379
+ bundle exec guard
380
+ ```
381
+
382
+ You need to put **all your tests** in the `/human_attributes/spec/dummy/spec/` directory.
383
+
384
+ ## Contributing
385
+
386
+ 1. Fork it
387
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
388
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
389
+ 4. Push to the branch (`git push origin my-new-feature`)
390
+ 5. Create new Pull Request
391
+
392
+ ## Credits
393
+
394
+ Thank you [contributors](https://github.com/platanus/human_attributes/graphs/contributors)!
395
+
396
+ <img src="http://platan.us/gravatar_with_text.png" alt="Platanus" width="250"/>
397
+
398
+ Human Attributes is maintained by [platanus](http://platan.us).
399
+
400
+ ## License
401
+
402
+ Human Attributes is © 2016 platanus, spa. It is free software and may be redistributed under the terms specified in the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
8
+ load "rails/tasks/engine.rake"
9
+ load "rails/tasks/statistics.rake"
10
+ Bundler::GemHelper.install_tasks