rails_extend 1.0.0 → 1.0.1

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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +20 -0
  3. data/README.md +46 -0
  4. data/config/locales/en.datetime.yml +17 -0
  5. data/config/locales/zh.datetime.yml +145 -0
  6. data/config/locales/zh.support.yml +52 -0
  7. data/lib/generators/rails_extend/ignore_generator.rb +27 -0
  8. data/lib/generators/rails_extend/migrations_generator.rb +33 -0
  9. data/lib/generators/rails_extend/remove_table_generator.rb +29 -0
  10. data/lib/generators/rails_extend/rename_module_generator.rb +29 -0
  11. data/lib/generators/rails_extend/templates/README.md +2 -0
  12. data/lib/generators/rails_extend/templates/initializer.rb.tt +10 -0
  13. data/lib/generators/rails_extend/templates/migration.rb.tt +35 -0
  14. data/lib/generators/rails_extend/templates/remove_table.rb.tt +9 -0
  15. data/lib/generators/rails_extend/templates/rename_module.rb.tt +9 -0
  16. data/lib/rails_extend/active_model/type_value.rb +26 -0
  17. data/lib/rails_extend/active_model.rb +3 -0
  18. data/lib/rails_extend/active_record/enum.rb +74 -0
  19. data/lib/rails_extend/active_record/extend.rb +200 -0
  20. data/lib/rails_extend/active_record/i18n.rb +31 -0
  21. data/lib/rails_extend/active_record/include.rb +29 -0
  22. data/lib/rails_extend/active_record/taxon.rb +29 -0
  23. data/lib/rails_extend/active_record/translation.rb +41 -0
  24. data/lib/rails_extend/active_record.rb +8 -0
  25. data/lib/rails_extend/active_storage/attachment.rb +16 -0
  26. data/lib/rails_extend/active_storage/blob_prepend.rb +40 -0
  27. data/lib/rails_extend/active_storage/variant.rb +16 -0
  28. data/lib/rails_extend/active_storage.rb +5 -0
  29. data/lib/rails_extend/config.rb +19 -0
  30. data/lib/rails_extend/core/array.rb +79 -0
  31. data/lib/rails_extend/core/date.rb +32 -0
  32. data/lib/rails_extend/core/duration.rb +12 -0
  33. data/lib/rails_extend/core/hash.rb +106 -0
  34. data/lib/rails_extend/core/module.rb +7 -0
  35. data/lib/rails_extend/core/nil.rb +11 -0
  36. data/lib/rails_extend/core/numeric.rb +11 -0
  37. data/lib/rails_extend/core/pathname.rb +15 -0
  38. data/lib/rails_extend/core/string.rb +15 -0
  39. data/lib/rails_extend/core/time_format.rb +20 -0
  40. data/lib/rails_extend/core.rb +17 -0
  41. data/lib/rails_extend/engine.rb +13 -0
  42. data/lib/rails_extend/env.rb +10 -0
  43. data/lib/rails_extend/generators/named_base.rb +22 -0
  44. data/lib/rails_extend/generators.rb +3 -0
  45. data/lib/rails_extend/models.rb +120 -0
  46. data/lib/rails_extend/quiet_logs.rb +22 -0
  47. data/lib/rails_extend/routes.rb +81 -0
  48. data/lib/rails_extend/type/i18n.rb +23 -0
  49. data/lib/rails_extend/type/taxon.rb +15 -0
  50. data/lib/rails_extend/type.rb +4 -0
  51. data/lib/rails_extend.rb +19 -0
  52. data/lib/templates/test_unit/model/fixtures.yml.tt +15 -0
  53. data/lib/templates/test_unit/model/unit_test.rb.tt +7 -0
  54. metadata +56 -5
  55. data/lib/active_model/type_value.rb +0 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44da1bf70eac9a513d304650420ecde6fa0b5c0f9704cab8b9fd25c37adbe2aa
4
- data.tar.gz: 5a3f14a91bfa7d658a5b711f39dafc5b74e6c7e04c6fbf4b2f144e7d80cfc33f
3
+ metadata.gz: 14d1782a27b364e822118d37e190402e57785f1d797da73ac4307b94534caeea
4
+ data.tar.gz: 6f65c751e6b69b1093f53b7c4b646b14a0b9d5df70ac8feefba7cb718affebf8
5
5
  SHA512:
6
- metadata.gz: 7b0f5e3e417104b54cea02b470fe876bed7fe700af15229a0e0f9a71357b6bb60a8220d5019036e22ca802469bf478e3dbe9cb8bc64a05e91d0682a9e6196835
7
- data.tar.gz: d33802bafb444a11f8966eca5c96ccc390816d8b83820a89729dd203d5de574a364eff48a1d6406f76491dfc4b91754deb4086fe59e5021a2582ad81d6ce42e6
6
+ metadata.gz: 527e0bedcc3e428fa42d9b6a7cec61a0948f486d48708e4b2066552ab144bf2d52fb0f20fcb6f763fc952ea8070db36d9b8c759905939e3c796fa4354bc91bfc
7
+ data.tar.gz: 916b0e47282b51940ab35aff249d54122530e13b2f155dee4e0de52b7d83c4a18d236ce1321eea3bf08ddebfe29f6096100887c89a395c020e6822feb0750c6b
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2021-Present Mingyuan Qin <mingyuan0715@foxmail.com>
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.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # RailsExtend
2
+
3
+ [![测试](https://github.com/work-design/rails_extend/actions/workflows/test.yml/badge.svg)](https://github.com/work-design/rails_extend/actions/workflows/test.yml)
4
+ [![Docker构建](https://github.com/work-design/rails_extend/actions/workflows/cd.yml/badge.svg)](https://github.com/work-design/rails_extend/actions/workflows/cd.yml)
5
+ [![Gem](https://github.com/work-design/rails_extend/actions/workflows/gempush.yml/badge.svg)](https://github.com/work-design/rails_extend/actions/workflows/gempush.yml)
6
+
7
+ Rails 通用基础库,对 Rails 的各个组件进行了扩展。
8
+
9
+ ## 功能模块
10
+ * Ruby 核心类扩展,[链接](lib/rails_extend/core)
11
+ * Rails 核心类扩展
12
+ * ActiveStorage:[链接](lib/rails_com/active_storage)
13
+ * 通过 url 同步文件;
14
+ * 将文件复制到镜像服务器;
15
+ * Rails 元信息:
16
+ * [Model](lib/rails_extend/models.rb)
17
+ * [Routes / Controllers](lib/rails_extend/routes)
18
+
19
+ ## 支持 enum
20
+ ```yaml
21
+ # zh.yml
22
+ activerecord:
23
+ enum:
24
+ notification:
25
+ receiver_type:
26
+ User: 全体用户
27
+ Member: 成员
28
+ ```
29
+
30
+ ```ruby
31
+ t.select :receiver_type, options_for_select(Notification.options_i18n(:receiver_type))
32
+ ```
33
+
34
+ * Override
35
+ ```yaml
36
+ activerecord:
37
+ enum:
38
+ notification:
39
+ receiver_type:
40
+ User: 全体用户
41
+ Member: # remain this blank
42
+ ```
43
+
44
+
45
+ ## 版权
46
+ 遵循 [MIT](https://opensource.org/licenses/MIT) 协议
@@ -0,0 +1,17 @@
1
+ en:
2
+ durations:
3
+ years: years
4
+ months: months
5
+ weeks: weeks
6
+ days: days
7
+ hours: hours
8
+ minutes: minutes
9
+ seconds: seconds
10
+ duration:
11
+ years: year
12
+ months: month
13
+ weeks: week
14
+ days: day
15
+ hours: hour
16
+ minutes: minute
17
+ seconds: second
@@ -0,0 +1,145 @@
1
+ zh:
2
+ date:
3
+ formats:
4
+ default: '%Y-%m-%d'
5
+ short: '%b %d'
6
+ long: '%B %d, %Y'
7
+ day_names: [星期天, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六]
8
+ abbr_day_names: [周日, 周一, 周二, 周三, 周四, 周五, 周六]
9
+ month_names: [~, 一月份, 二月份, 三月份, 四月份, 五月份, 六月份, 七月份, 八月份, 九月份, 十月份, 十一月, 十二月]
10
+ abbr_month_names: [~, 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月]
11
+ week_day:
12
+ monday:
13
+ name: 星期一
14
+ abbr: 周一
15
+ tuesday:
16
+ name: 星期二
17
+ abbr: 周二
18
+ wednesday:
19
+ name: 星期三
20
+ abbr: 周三
21
+ thursday:
22
+ name: 星期四
23
+ abbr: 周四
24
+ friday:
25
+ name: 星期五
26
+ abbr: 周五
27
+ saturday:
28
+ name: 星期六
29
+ abbr: 周六
30
+ sunday:
31
+ name: 星期日
32
+ abbr: 周日
33
+ month_day:
34
+ january:
35
+ name: 一月份
36
+ abbr: 一月
37
+ february:
38
+ name: 二月份
39
+ abbr: 二月
40
+ march:
41
+ name: 三月份
42
+ abbr: 三月
43
+ april:
44
+ name: 四月份
45
+ abbr: 四月
46
+ may:
47
+ name: 五月份
48
+ abbr: 五月
49
+ june:
50
+ name: 六月份
51
+ abbr: 六月
52
+ july:
53
+ name: 七月份
54
+ abbr: 七月
55
+ august:
56
+ name: 八月份
57
+ abbr: 八月
58
+ september:
59
+ name: 九月份
60
+ abbr: 九月
61
+ october:
62
+ name: 十月份
63
+ abbr: 十月
64
+ november:
65
+ name: 十一月份
66
+ abbr: 十一月
67
+ december:
68
+ name: 十二月份
69
+ abbr: 十二月
70
+ order:
71
+ - 年
72
+ - 月
73
+ - 天
74
+ distance_in_words:
75
+ zero: 今天
76
+ one: 明天
77
+ two: 后天
78
+ three: 大后天
79
+ other: '%{count} 天后'
80
+ time:
81
+ formats:
82
+ default: '%a, %d %b %Y %H:%M:%S %z'
83
+ short: '%d %b %H:%M'
84
+ long: '%B %d, %Y %H:%M'
85
+ am: 上午
86
+ pm: 下午
87
+ datetime:
88
+ distance_in_words:
89
+ half_a_minute: 半分钟
90
+ less_than_x_seconds:
91
+ one: 不到1秒
92
+ other: '不到%{count}秒'
93
+ x_seconds:
94
+ one: 1秒
95
+ other: '%{count}秒'
96
+ less_than_x_minutes:
97
+ one: 不到1分钟
98
+ other: '不到%{count}分钟'
99
+ x_minutes:
100
+ one: 1分钟
101
+ other: '%{count}分钟'
102
+ about_x_hours:
103
+ one: 1小时
104
+ other: '%{count}小时'
105
+ x_days:
106
+ one: 1天
107
+ other: '%{count}天'
108
+ about_x_months:
109
+ one: 1个月
110
+ other: '%{count}个月'
111
+ x_months:
112
+ one: 1个月
113
+ other: '%{count} months'
114
+ about_x_years:
115
+ one: 1年
116
+ other: '%{count} years'
117
+ over_x_years:
118
+ one: '超过1年'
119
+ other: '%{count} 年'
120
+ almost_x_years:
121
+ one: 'almost 1 year'
122
+ other: 'almost %{count} years'
123
+ prompts:
124
+ year: 年
125
+ month: 月
126
+ day: 日
127
+ hour: 小时
128
+ minute: 分钟
129
+ second: 秒
130
+ durations:
131
+ years: 年
132
+ months: 个月
133
+ weeks: 个周
134
+ days: 天
135
+ hours: 小时
136
+ minutes: 分钟
137
+ seconds: 秒
138
+ duration:
139
+ years: 年
140
+ months: 月
141
+ weeks: 周
142
+ days: 天
143
+ hours: 小时
144
+ minutes: 分钟
145
+ seconds: 秒
@@ -0,0 +1,52 @@
1
+ zh:
2
+ number:
3
+ human:
4
+ format:
5
+ # These five are to override number.format and are optional
6
+ # separator:
7
+ delimiter: ""
8
+ precision: 0
9
+ significant: false
10
+ strip_insignificant_zeros: false
11
+ # Used in number_to_human_size()
12
+ storage_units:
13
+ # Storage units output formatting.
14
+ # %u is the storage unit, %n is the number (default: 2 MB)
15
+ format: "%n %u"
16
+ units:
17
+ byte:
18
+ one: 字节
19
+ other: 字节
20
+ kb: KB
21
+ mb: MB
22
+ gb: GB
23
+ tb: TB
24
+ pb: PB
25
+ eb: EB
26
+ # Used in NumberHelper.number_to_human()
27
+ decimal_units:
28
+ format: "%n %u"
29
+ units:
30
+ femto: Quadrillionth
31
+ pico: Trillionth
32
+ nano: Billionth
33
+ micro: Millionth
34
+ mili: Thousandth
35
+ centi: 分
36
+ deci: 角
37
+ unit: ""
38
+ ten:
39
+ one: 拾
40
+ other: 拾
41
+ hundred: 佰
42
+ thousand: 仟
43
+ ten_thousand: 万
44
+ million: 佰万
45
+ billion: Billion
46
+ trillion: Trillion
47
+ quadrillion: Quadrillion
48
+ support:
49
+ array:
50
+ words_connector: ' '
51
+ two_words_connector: ' '
52
+ last_word_connector: ' '
@@ -0,0 +1,27 @@
1
+ module RailsExtend
2
+ module Generators
3
+ class IgnoreGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ attr_reader :ignore_models
6
+
7
+ def copy_initializer_file
8
+ puts "#{'*-' * 40}"
9
+ Models.ignore_models.each do |count, models|
10
+ puts "#{count.to_s.rjust(2, ' ')}: #{models.inspect}"
11
+ end
12
+ puts "#{'*-' * 40}"
13
+
14
+ limit = args[0].to_i
15
+ @ignore_models = Models.ignore_models.select(&->(k, v){ k <= limit })
16
+
17
+ template 'initializer.rb', 'config/initializers/rails_extend.rb'
18
+ end
19
+
20
+ def show_readme
21
+ if behavior == :invoke
22
+ readme 'README.md'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ # bin/rails g rails_com:migrations
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ class RailsExtend::MigrationsGenerator < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+ source_root File.expand_path('templates', __dir__)
8
+ attr_reader :tables
9
+
10
+ def create_migration_file
11
+ file_name = "extend_migration_#{file_index}"
12
+
13
+ RailsExtend::Models.db_tables_hash.each do |mig_paths, tables|
14
+ next if tables.blank?
15
+ @tables = tables
16
+ path = Array(mig_paths)[0]
17
+ migration_template 'migration.rb', File.join(path, "#{file_name}.rb")
18
+ end
19
+ end
20
+
21
+ def file_index
22
+ ups = ActiveRecord::Base.connection.migration_context.migrations_status.select do |status, version, name|
23
+ status == 'up' && name.start_with?('Extend migration ')
24
+ end
25
+ if ups.present?
26
+ index = ups[-1][-1].delete_prefix 'Extend migration '
27
+ index.to_i + 1
28
+ else
29
+ 1
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ # bin/rails g rails_com:rename_module new old
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ class RailsExtend::RemoveTableGenerator < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+ source_root File.expand_path('templates', __dir__)
8
+ attr_reader :tables
9
+
10
+ def create_migration_file
11
+ @tables = RailsExtend::Models.unbound_tables
12
+ file_name = "extend_remove_table_#{file_index}"
13
+
14
+ migration_template 'remove_table.rb', File.join(db_migrate_path, "#{file_name}.rb")
15
+ end
16
+
17
+ def file_index
18
+ ups = ActiveRecord::Base.connection.migration_context.migrations_status.select do |status, version, name|
19
+ status == 'up' && name.start_with?('Extend remove table ')
20
+ end
21
+ if ups.present?
22
+ index = ups[-1][-1].delete_prefix 'Extend remove table '
23
+ index.to_i + 1
24
+ else
25
+ 1
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ # bin/rails g rails_com:rename_module new old
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ class RailsExtend::RenameModuleGenerator < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+ source_root File.expand_path('templates', __dir__)
8
+ attr_reader :tables
9
+
10
+ def create_migration_file
11
+ @tables = RailsExtend::Models.migrate_modules_hash.invert
12
+ file_name = "extend_rename_module_#{file_index}"
13
+
14
+ migration_template 'rename_module.rb', File.join(db_migrate_path, "#{file_name}.rb")
15
+ end
16
+
17
+ def file_index
18
+ ups = ActiveRecord::Base.connection.migration_context.migrations_status.select do |status, version, name|
19
+ status == 'up' && name.start_with?('Extend rename module ')
20
+ end
21
+ if ups.present?
22
+ index = ups[-1][-1].delete_prefix 'Extend rename module '
23
+ index.to_i + 1
24
+ else
25
+ 1
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,2 @@
1
+
2
+ 已创建配置文件: config/initializers/rails_extend.rb, 忽略自动 migration 的 model 。
@@ -0,0 +1,10 @@
1
+ RailsExtend.configure do |config|
2
+ config.ignore_models = [
3
+ <%- ignore_models.each do |count, models| -%>
4
+ # 已定义 <%= count %> 个 attribute
5
+ <%- models.each do |model| -%>
6
+ '<%= model %>',
7
+ <%- end -%>
8
+ <%- end -%>
9
+ ]
10
+ end
@@ -0,0 +1,35 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+
3
+ def change
4
+ <% tables.each do |table_name, attributes| -%>
5
+
6
+ <%- if attributes[:table_exists] -%>
7
+ <%- attributes[:add_references].each do |reference, options| -%>
8
+ add_belongs_to :<%= table_name %>, :<%= options[:name] %><%= options[:reference_options] %>
9
+ <%- end -%>
10
+ <%- attributes[:add_attributes].each do |attribute, options| -%>
11
+ add_column :<%= table_name %>, :<%= attribute %>, :<%= options[:migrate_type] %><%= options[:attribute_options] %>
12
+ <%- end -%>
13
+ <%- attributes[:remove_attributes].each do |attribute, options| -%>
14
+ remove_column :<%= table_name %>, :<%= attribute %>, :<%= options[:migrate_type] %><%= options[:attribute_options] %>
15
+ <%- end -%>
16
+ <%- else -%>
17
+ create_table :<%= table_name %> do |t|
18
+ <%- attributes[:add_references].each do |reference, options| -%>
19
+ t.belongs_to :<%= options[:name] %><%= options[:reference_options] %>
20
+ <%- end -%>
21
+ <%- attributes[:add_attributes].each do |attribute, options| -%>
22
+ t.<%= options[:migrate_type] %> :<%= attribute %><%= options[:attribute_options] %>
23
+ <%- end -%>
24
+ <%- if attributes[:timestamps].blank? -%>
25
+ t.timestamps
26
+ <%- end -%>
27
+ <%- attributes[:indexes].each do |index| -%>
28
+ t.index <%= index[:index].inspect %><%= index[:index_options] %>
29
+ <%- end -%>
30
+ end
31
+ <%- end -%>
32
+ <%- end -%>
33
+ end
34
+
35
+ end
@@ -0,0 +1,9 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+
3
+ def change
4
+ <%- tables.each do |table| -%>
5
+ drop_table :<%= table %>
6
+ <%- end -%>
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+
3
+ def change
4
+ <%- tables.each do |old_name, new_name| -%>
5
+ rename_table :<%= old_name %>, :<%= new_name %>
6
+ <%- end -%>
7
+ end
8
+
9
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsExtend::ActiveModel
4
+ module TypeValue
5
+ attr_reader :options
6
+
7
+ # 让 attribute 方法支持更多选项
8
+ def initialize(precision: nil, limit: nil, scale: nil, **options)
9
+ @options = options
10
+ super(precision: precision, limit: scale, scale: limit)
11
+ end
12
+
13
+ def input_type
14
+ type
15
+ end
16
+
17
+ def migrate_type
18
+ return type if type
19
+ return subtype.type if respond_to?(:subtype) && subtype.type
20
+ :string
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ ActiveModel::Type::Value.prepend RailsExtend::ActiveModel::TypeValue
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_extend/active_model/type_value'
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsExtend::ActiveRecord
4
+ module Enum
5
+
6
+ def options_i18n(attribute)
7
+ h = ::I18n.t enum_key(attribute), default: {}
8
+ h.compact!
9
+
10
+ if h.is_a?(Hash) && h.present?
11
+ return h.invert
12
+ end
13
+
14
+ if h.blank?
15
+ name = attribute.to_s.pluralize
16
+ if respond_to?(name)
17
+ enum_hash = public_send(name)
18
+ h = enum_hash.keys.map { |i| [i.humanize, i] }.to_h
19
+ end
20
+ end
21
+
22
+ h
23
+ end
24
+
25
+ def help_i18n(attribute)
26
+ return nil if attribute.blank?
27
+ help_key = RailsExtend.config.help_key.call(self, attribute)
28
+ ::I18n.t help_key, default: nil
29
+ end
30
+
31
+ def enum_i18n(attribute, value)
32
+ h = ::I18n.t enum_key(attribute), default: {}
33
+ h.compact!
34
+
35
+ v = nil
36
+ if h.is_a?(Hash)
37
+ v = h[value] ? h[value] : h[value.to_s.to_sym]
38
+ end
39
+
40
+ if v.nil? && value.blank?
41
+ v = value.to_s
42
+ end
43
+
44
+ if v.nil?
45
+ v = human_attribute_name(value)
46
+ end
47
+
48
+ v
49
+ end
50
+
51
+ def enum_key(attribute)
52
+ RailsExtend.config.enum_key.call(self, attribute)
53
+ end
54
+
55
+ def self.extended(mod)
56
+ mod.attribute_method_suffix '_i18n'
57
+
58
+ mod.class_exec do
59
+ def attribute_i18n(attr)
60
+ if [:json, :jsonb].include? self.class.columns_hash[attr]&.type
61
+ send(attr)&.transform_keys! { |key| self.class.human_attribute_name(key) }
62
+ else
63
+ self.class.enum_i18n attr, send(attr)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+
72
+ ActiveSupport.on_load :active_record do
73
+ extend RailsExtend::ActiveRecord::Enum
74
+ end