acts_enum_translable 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: 7466fd38a12ebb2684ef2cc7bd23ec0e03510078
4
+ data.tar.gz: 60c2786969fe6e2a5c2a91e14e015c0e44b46803
5
+ SHA512:
6
+ metadata.gz: 1ba611948912654dddfe9097d22e8d385055c3b842ac797acba22f09da3d40b2d1317ead7f39f2f76367d9030e8f19645214b66d07bbf814270b771f8c8657e8
7
+ data.tar.gz: 627d4752ab6b972589ef21ef0e963aad8e5e08f52879dcc2ca17e63c21ecaeadf960cfa1980613d1dfddfac2a3a270e90201a1b053b1afb1364e7d0a9fc9a545
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Thadeu Esteves Jr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,154 @@
1
+ # ActsEnumTranslable
2
+ ActsEnumTranslable convention translate enums with .yml file.
3
+
4
+ ## Usage
5
+ * Install Gem
6
+ * Configure enum native Rails
7
+ * Add acts_enum_translable in Model class
8
+ * Configure active_record attributes in file .yml
9
+ * Use methods
10
+
11
+ ## Install Gem for Ruby on Rails
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'acts_enum_translable'
16
+ ```
17
+
18
+ After:
19
+ ```bash
20
+ $ bundle install
21
+ ```
22
+
23
+ ## Configure enum native Rails
24
+ More details, please visit [ActiveRecord::Enum](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html)
25
+
26
+ ```ruby
27
+ # example model
28
+ # user.rb
29
+
30
+ class User < ApplicationRecord
31
+ enum status: { inactive: 0, active: 1, blocked: 2 }
32
+ enum visible: [:hide, :show]
33
+ end
34
+ ```
35
+ ## Add acts_enum_translable in Model class
36
+
37
+ Add in Model for inherit methods
38
+
39
+ ```ruby
40
+ # exemple model
41
+ # user.rb
42
+
43
+ class User < ApplicationRecord
44
+ enum status: { inactive: 0, active: 1, blocked: 2 }
45
+ enum visible: [:hide, :show]
46
+
47
+ # Add in Model for inherit methods
48
+ acts_enum_translable
49
+ end
50
+ ```
51
+
52
+ ## Configure active_record attributes in file .yml
53
+
54
+ More details, please visit section [4.5 Translations for Active Record Models](http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models)
55
+
56
+ Configure *Locale English* for exemple
57
+
58
+ ```ruby
59
+ # config/locales/en.activerecord.yml
60
+ en:
61
+ activerecord:
62
+ attributes:
63
+ users:
64
+ status:
65
+ inactive: "Inactive"
66
+ active: "Active"
67
+ blocked: "Blocked"
68
+ visible:
69
+ hide: 'Hide'
70
+ show: 'Show'
71
+ ```
72
+
73
+ Configure *Locale Portuguese* for exemple
74
+
75
+ ```ruby
76
+ # config/locales/pt.activerecord.yml
77
+ pt:
78
+ activerecord:
79
+ attributes:
80
+ users:
81
+ status:
82
+ inactive: "Inativo"
83
+ active: "Ativo"
84
+ blocked: "Bloqueado"
85
+ visible:
86
+ hide: 'Oculto'
87
+ show: 'Visível'
88
+ ```
89
+
90
+ ## Use methods sufix with _translable for enums key
91
+ ```ruby
92
+ # print status
93
+ user = User.new(status: :blocked, visible: :show)
94
+ user.status_translable
95
+ # english: Blocked
96
+ # portuguese: Bloqueado
97
+
98
+ # print visible
99
+ user.visible_translable
100
+ # english: Show
101
+ # portuguese: Visível
102
+ ```
103
+
104
+ ## Use methods in Model class get enums for key
105
+
106
+ _Required key param._
107
+
108
+ ```ruby
109
+ # return Array with Values
110
+ # in english
111
+ # I18n.locale = :en
112
+ User.enum_list(:status)
113
+ # => ["Inactive", "Active", "Blocked"]
114
+
115
+ # in portuguese
116
+ # I18n.locale = :pt
117
+ User.enum_list(:status)
118
+ # => ["Inativo", "Ativo", "Bloqueado"]
119
+ ```
120
+
121
+ Now return array with values and keys or reverse
122
+
123
+ ```ruby
124
+ # return Arrary with par [value, key]
125
+ # in english
126
+ # I18n.locale = :en
127
+ User.enum_with_keys(:status)
128
+ # => [["Inactive", 0], ["Active", 1], ["Blocked", 2]]
129
+
130
+ # in portuguese
131
+ # I18n.locale = :pt
132
+ User.enum_with_keys(:status)
133
+ # => [["Inativo", 0], ["Ativo", 1], ["Bloqueado", 2]]
134
+
135
+ # return par array [key, value]
136
+ User.enum_form(:status)
137
+ # => [[0, "Inativo"], [1, "Ativo"], [2, "Bloqueado"]]
138
+ ```
139
+
140
+ ### Use SimpleForm too
141
+
142
+ Easy return enums translated.
143
+
144
+ ```ruby
145
+ # with I18n.locale = :pt or :en
146
+ <%= f.select :status, User.enum_form(:status), include_blank: true %>
147
+ ```
148
+
149
+ ## Contributing
150
+
151
+ We have a long list of valued contributors. Check them all at: https://github.com/Thadeu/acts_enum_translable.
152
+
153
+ ## License
154
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
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
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsEnumTranslable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+ require 'rake/testtask'
19
+
20
+ Rake::TestTask.new(:test) do |t|
21
+ t.libs << 'lib'
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,8 @@
1
+ require 'acts_enum_translable/support/runtime_error'
2
+ require 'acts_enum_translable/support/i18n'
3
+ require 'acts_enum_translable/active_model/model'
4
+ require 'acts_enum_translable/active_model/concern'
5
+ require 'acts_enum_translable/engine'
6
+
7
+ module ActsEnumTranslable
8
+ end
@@ -0,0 +1,42 @@
1
+ # @author Thadeu Esteves
2
+ module ActsEnumTranslable
3
+ # ActiveModel
4
+ module ActiveModel
5
+ # Concern for ActiveModel
6
+ module Concern
7
+ extend ActiveSupport::Concern
8
+ include ActsEnumTranslable::ActiveModel::Model
9
+
10
+ module ClassMethods
11
+ # Pega o nome da class corrente, verifica se tem enums
12
+ # e cria os metodos em tempo de execucao
13
+ # == Returns:
14
+ # @return [Method] method_translable
15
+ def acts_enum_translable(options = {})
16
+ cattr_accessor :class_name
17
+ klass = (options[:class_name] || self)
18
+ add_runtime_methods(klass) if klass.defined_enums?
19
+ end
20
+
21
+ private
22
+
23
+ # Cria metodos em tempo de execucao com sufixo _translable
24
+ #
25
+ # == Parameters:
26
+ # @param klass [Array] recebe um array com os enums definidos em uma class/model
27
+ #
28
+ # == Returns:
29
+ # @return [Method] retorna methods com sufixo _translable baseados nas keys dos enums
30
+ def add_runtime_methods(klass)
31
+ klass.defined_enums.keys.each do |enum_key|
32
+ define_method :"#{enum_key}_translable" do
33
+ trans_key = self.class.i18n_key(enum_key, send(enum_key))
34
+ return I18n.t(trans_key, locale: I18n.locale) if I18n.exists? trans_key
35
+ return send(enum_key).humanize
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ # @author Thadeu Esteves
2
+ module ActsEnumTranslable
3
+ module ActiveModel
4
+ module Model
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ extend ActsEnumTranslable::Support::RuntimeError
9
+ extend ActsEnumTranslable::Support::I18n
10
+ # Busca todos os enums do model corrente
11
+ # @return [Array] ["Nao Realizado", "Realizado", "Arquivado"]
12
+ def self.enum_list(enum_key = nil)
13
+ # runtime_error exception
14
+ runtime_error enum_key
15
+ defined_enums[enum_key.to_s].map do |enum|
16
+ trans_key = i18n_key(enum_key, enum.first)
17
+ # procura nos arquivo de traducao
18
+ # caso nao encontre humanize o proprio enum
19
+ if I18n.exists? trans_key
20
+ I18n.t(trans_key, locale: I18n.locale)
21
+ else
22
+ # original key
23
+ enum.first.humanize
24
+ end
25
+ end
26
+ end
27
+
28
+ # Busca todos os metodos do model corrente
29
+ #
30
+ # @return [Array] [["Nao Realizado", 0], ["Realizado", 1], ["Arquivado", 2]]
31
+ def self.enum_with_keys(enum_key = nil)
32
+ # runtime_error exception
33
+ runtime_error enum_key
34
+
35
+ defined_enums[enum_key.to_s].map do |enum|
36
+ original_key = enum.first.humanize
37
+ trans_key = i18n_key(enum_key, enum.first)
38
+
39
+ # procura nos arquivo de traducao
40
+ # caso nao encontre humanize o proprio enum
41
+ if I18n.exists? trans_key
42
+ [I18n.t(trans_key, locale: I18n.locale), enum.second]
43
+ else
44
+ [original_key, enum.second]
45
+ end
46
+ end
47
+ end
48
+
49
+ # Use with radio simple_form
50
+ #
51
+ # @return [Array] [["Nao Realizado", 0], ["Realizado", 1], ["Arquivado", 2]]
52
+ def self.enum_form(enum_key = nil)
53
+ # runtime_error exception
54
+ runtime_error enum_key
55
+
56
+ defined_enums[enum_key.to_s].map do |enum|
57
+ original_key = enum.first.humanize
58
+ trans_key = i18n_key(enum_key, enum.first)
59
+
60
+ # procura nos arquivo de traducao
61
+ # caso nao encontre humanize o proprio enum
62
+ if I18n.exists? trans_key
63
+ [enum.second, I18n.t(trans_key, locale: I18n.locale)]
64
+ else
65
+ [enum.second, original_key]
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ module ActsEnumTranslable
2
+ module Engine
3
+ ActiveSupport.on_load :active_record do
4
+ ActiveRecord::Base.send :include, ActsEnumTranslable::ActiveModel::Concern
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # @author Thadeu Esteves
2
+ module ActsEnumTranslable
3
+ module Support
4
+ module I18n
5
+ # based in translations for active record models
6
+ # http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
7
+ # @return [String] activerecord.attributes.model_name.attribute_name.field_name
8
+ def i18n_key(i18n_key, key)
9
+ "activerecord.attributes.#{name.underscore.pluralize}.#{i18n_key}.#{key}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # @author Thadeu Esteves
2
+ module ActsEnumTranslable
3
+ module Support
4
+ module RuntimeError
5
+ # Exception Method
6
+ # TODO: We need improve method
7
+ # @return [Exception] Enum key required for translation enums
8
+ def runtime_error(enum_key)
9
+ return raise 'Enum key required for translation enums' unless enum_key.present?
10
+ return raise 'Enum key must be type symbol' unless enum_key.class == Symbol
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ActsEnumTranslable
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_enum_translable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thadeu Esteves Jr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'ActsEnumTranslable convention translate enums with .yml file. '
56
+ email:
57
+ - tadeuu@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/acts_enum_translable.rb
66
+ - lib/acts_enum_translable/active_model/concern.rb
67
+ - lib/acts_enum_translable/active_model/model.rb
68
+ - lib/acts_enum_translable/engine.rb
69
+ - lib/acts_enum_translable/support/i18n.rb
70
+ - lib/acts_enum_translable/support/runtime_error.rb
71
+ - lib/acts_enum_translable/version.rb
72
+ homepage: http://github.com/thadeu/acts_enum_translable
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: ActiveRecord::Enum Translate with i18n
96
+ test_files: []