activerecord_humanized_enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d23f429a730db1db20b31f11e7a6eb59dd699887
4
+ data.tar.gz: 04736ed5c55bad609e36a82d8a564e7986422345
5
+ SHA512:
6
+ metadata.gz: 771ece8084585ab2da7489748e6164cf02b3319612477e874f11c64ceb1cbfcd3b2dd001b26b0afc8405153f89b08970401bb013a0ff60eaa611864cf173a001
7
+ data.tar.gz: 91eb0af48ac734ce7fe7632012f1c08ac58c63ebbeb7ae210996fd470a382f21ccefd89b171e062212174ee458aa6064e714c938517cb5ec78a56c606c7d07dc
@@ -0,0 +1,13 @@
1
+ # http://editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
11
+
12
+ [*.md]
13
+ trim_trailing_whitespace = false
@@ -0,0 +1,4 @@
1
+ pkg
2
+ doc
3
+ log
4
+ *.lock
@@ -0,0 +1,39 @@
1
+ # Contributing
2
+
3
+ ## Issues
4
+
5
+ If you want to request a feature or report a bug, please use the following template.
6
+
7
+ ```markdown
8
+ ## Description
9
+
10
+ [Add feature/bug description here]
11
+
12
+ ## How to reproduce
13
+
14
+ [Add steps on how to reproduce this issue]
15
+
16
+ ## What do you expect
17
+
18
+ [Describe what do you expect to happen]
19
+
20
+ ## What happened instead
21
+
22
+ [Describe the actual results]
23
+
24
+ ## Gem version:
25
+
26
+ Rescuable version: [Add gem version here]
27
+ ```
28
+
29
+ ## Writing code
30
+
31
+ Once you've made your great commits (include tests, please):
32
+
33
+ 1. [Fork](http://help.github.com/forking/) the [original repository](http://github.com/dhyegofernando/rescuable)
34
+ 2. Create a topic branch - `git checkout -b my_branch`
35
+ 3. Push to your branch - `git push origin my_branch`
36
+ 4. [Create an Issue](http://github.com/dhyegofernando/rescuable/issues) with a link to your branch
37
+ 5. That's it!
38
+
39
+ Please respect the code style using [editorconfig](http://editorconfig.org/) in your text editor.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rescuable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dhyego Fernando
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.
@@ -0,0 +1,49 @@
1
+ # ActiveRecord Humanized Enum (I18n)
2
+ Easily translate your Active Record\'s enums.
3
+
4
+ ## Installation
5
+ Include to your Gemfile
6
+ ```ruby
7
+ gem 'activerecord_humanized_enum', require: 'active_record/humanized_enum'
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ### ActiveRecord integration
13
+ The installation automatically self-includes in the ```ActiveRecord::Base```.
14
+
15
+ ### How to use
16
+ 1. Call the `enum` macro method just like you already do:
17
+ ```ruby
18
+ class User
19
+ enum status: [:enabled, :disabled]
20
+ end
21
+ ```
22
+
23
+ 2. Translate the enums in your YML translation files:
24
+ ```yaml
25
+ pt-BR:
26
+ activerecord:
27
+ attributes:
28
+ user:
29
+ status: Estado
30
+ statuses:
31
+ enabled: Ativo
32
+ disabled: Inativo
33
+ ```
34
+
35
+ 3. Will be available the following methods:
36
+ ```ruby
37
+ User.humanized_status(:enabled) # Ativo
38
+ User.humanized_status(:disnabled) # Inativo
39
+
40
+ user = User.first
41
+ puts user.status # enabled
42
+ puts user.humanized_status # Ativo
43
+
44
+ user.status = :disabled
45
+ puts user.humanized_status # Inativo
46
+ ```
47
+
48
+ ## Maintainer
49
+ [Dhyego Fernando](https://github.com/dhyegofernando)
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['./test/**/*_test.rb']
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,26 @@
1
+ require './lib/active_record/humanized_enum/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'activerecord_humanized_enum'
5
+ spec.version = ActiveRecord::HumanizedEnum::Version::STRING
6
+ spec.authors = ['Dhyego Fernando']
7
+ spec.email = ['dhyegofernando@gmail.com']
8
+
9
+ spec.summary = 'Easily translate your ActiveRecord\'s enums.'
10
+ spec.description = spec.summary
11
+ spec.homepage = 'http://github.com/dhyegofernando/activerecord-enum-i18n'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+
16
+ spec.add_dependency 'railties', '~> 4.0'
17
+ spec.add_dependency 'activesupport', '~> 4.0'
18
+ spec.add_dependency 'activerecord', '~> 4.0'
19
+ spec.add_dependency 'i18n'
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency 'minitest-utils'
25
+ spec.add_development_dependency 'pry-meta'
26
+ end
@@ -0,0 +1,82 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/lazy_load_hooks'
3
+
4
+ module ActiveRecord
5
+ module HumanizedEnum
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ ENUM_HUMANIZABLE_CONFLICT_MESSAGE = \
10
+ "You tried to humanize an enum named \"%{enum}\" on the model \"%{klass}\", but " \
11
+ "this will generate a %{type} method \"%{method}\", which is already defined " \
12
+ "by %{source}."
13
+
14
+ def enum(definitions)
15
+ super(definitions)
16
+ humanized_enum(*definitions.keys)
17
+ end
18
+
19
+ def humanized_enum(*enums)
20
+ klass = self
21
+ enums.each do |enum|
22
+ enum = enum.to_s
23
+ method_name = "humanized_#{enum}"
24
+
25
+ # def self.humanized_status(status) ... end
26
+ detect_humanized_enum_conflict! enum, method_name, true
27
+ klass.singleton_class.send :define_method, method_name do |enum_value|
28
+ I18n.t enum_value.to_sym, scope: [:activerecord, :attributes, klass.model_name.i18n_key, enum.pluralize.to_sym], default: enum_value.to_s.humanize if enum_value.present?
29
+ end
30
+
31
+ # def humanized_status ... end
32
+ detect_humanized_enum_conflict! enum, method_name
33
+ define_method method_name do
34
+ klass.send method_name.to_sym, send(enum.to_sym)
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def detect_humanized_enum_conflict!(enum_name, method_name, klass_method = false)
42
+ if klass_method && dangerous_class_method?(method_name)
43
+ raise ArgumentError, ENUM_HUMANIZABLE_CONFLICT_MESSAGE % {
44
+ enum: enum_name,
45
+ klass: self.name,
46
+ type: 'class',
47
+ method: method_name,
48
+ source: 'Active Record'
49
+ }
50
+ elsif !klass_method && dangerous_attribute_method?(method_name)
51
+ raise ArgumentError, ENUM_HUMANIZABLE_CONFLICT_MESSAGE % {
52
+ enum: enum_name,
53
+ klass: self.name,
54
+ type: 'instance',
55
+ method: method_name,
56
+ source: 'Active Record'
57
+ }
58
+ elsif klass_method && respond_to?(method_name)
59
+ raise ArgumentError, ENUM_HUMANIZABLE_CONFLICT_MESSAGE % {
60
+ enum: enum_name,
61
+ klass: self.name,
62
+ type: 'class',
63
+ method: method_name,
64
+ source: 'another humanized enum'
65
+ }
66
+ elsif !klass_method && method_defined?(method_name)
67
+ raise ArgumentError, ENUM_HUMANIZABLE_CONFLICT_MESSAGE % {
68
+ enum: enum_name,
69
+ klass: self.name,
70
+ type: 'instance',
71
+ method: method_name,
72
+ source: 'another humanized enum'
73
+ }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ ActiveSupport.on_load(:active_record) do
81
+ include ActiveRecord::HumanizedEnum
82
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveRecord
2
+ module HumanizedEnum
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 0
7
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_humanized_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dhyego Fernando
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-utils
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-meta
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Easily translate your ActiveRecord's enums.
140
+ email:
141
+ - dhyegofernando@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".editorconfig"
147
+ - ".gitignore"
148
+ - CONTRIBUTING.md
149
+ - Gemfile
150
+ - LICENSE
151
+ - README.md
152
+ - Rakefile
153
+ - activerecord_enum_i18n.gemspec
154
+ - lib/active_record/humanized_enum.rb
155
+ - lib/active_record/humanized_enum/version.rb
156
+ homepage: http://github.com/dhyegofernando/activerecord-enum-i18n
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.4.8
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Easily translate your ActiveRecord's enums.
180
+ test_files: []