peak_flow_utils 0.0.1 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +5 -5
  2. data/{LICENSE.txt → MIT-LICENSE} +1 -1
  3. data/README.md +22 -13
  4. data/Rakefile +20 -30
  5. data/app/assets/config/peak_flow_utils_manifest.js +2 -0
  6. data/app/assets/javascripts/peak_flow_utils/application.js +14 -0
  7. data/app/assets/stylesheets/peak_flow_utils/application.css +15 -0
  8. data/app/controllers/peak_flow_utils/application_controller.rb +13 -0
  9. data/app/controllers/peak_flow_utils/pings/sidekiq_pings_controller.rb +10 -0
  10. data/app/handlers/peak_flow_utils/application_handler.rb +36 -0
  11. data/app/handlers/peak_flow_utils/devise_handler.rb +126 -0
  12. data/app/handlers/peak_flow_utils/file_handler.rb +42 -0
  13. data/app/handlers/peak_flow_utils/model_handler.rb +123 -0
  14. data/app/handlers/peak_flow_utils/rails_handler.rb +206 -0
  15. data/app/handlers/peak_flow_utils/simple_form_handler.rb +76 -0
  16. data/app/handlers/peak_flow_utils/validations_handler.rb +85 -0
  17. data/app/handlers/peak_flow_utils/will_paginate_handler.rb +59 -0
  18. data/app/helpers/peak_flow_utils/application_helper.rb +2 -0
  19. data/app/jobs/peak_flow_utils/application_job.rb +2 -0
  20. data/app/mailers/peak_flow_utils/application_mailer.rb +4 -0
  21. data/app/migrations/20150902155200_create_translation_keys.rb +8 -0
  22. data/app/migrations/20150907070909_create_groups.rb +12 -0
  23. data/app/migrations/20150907090900_create_handlers.rb +9 -0
  24. data/app/migrations/20150908085500_create_translation_values.rb +13 -0
  25. data/app/migrations/20150908090800_create_handler_texts.rb +22 -0
  26. data/app/migrations/20160411190500_create_scanned_files.rb +10 -0
  27. data/app/migrations/peak_flow_utils/application_migration.rb +5 -0
  28. data/app/models/peak_flow_utils/application_record.rb +9 -0
  29. data/app/models/peak_flow_utils/group.rb +22 -0
  30. data/app/models/peak_flow_utils/handler.rb +14 -0
  31. data/app/models/peak_flow_utils/handler_text.rb +46 -0
  32. data/app/models/peak_flow_utils/scanned_file.rb +2 -0
  33. data/app/models/peak_flow_utils/translation_key.rb +8 -0
  34. data/app/models/peak_flow_utils/translation_value.rb +24 -0
  35. data/app/services/peak_flow_utils/application_service.rb +2 -0
  36. data/app/services/peak_flow_utils/attribute_service.rb +32 -0
  37. data/app/services/peak_flow_utils/configuration_service.rb +11 -0
  38. data/app/services/peak_flow_utils/database_initializer_service.rb +39 -0
  39. data/app/services/peak_flow_utils/erb_inspector.rb +71 -0
  40. data/app/services/peak_flow_utils/erb_inspector/file_inspector.rb +137 -0
  41. data/app/services/peak_flow_utils/erb_inspector/translation_inspector.rb +100 -0
  42. data/app/services/peak_flow_utils/group_service.rb +50 -0
  43. data/app/services/peak_flow_utils/handlers_finder_service.rb +25 -0
  44. data/app/services/peak_flow_utils/model_inspector.rb +133 -0
  45. data/app/services/peak_flow_utils/translation_service.rb +138 -0
  46. data/app/services/peak_flow_utils/translations_parser_service.rb +211 -0
  47. data/app/views/layouts/peak_flow_utils/application.html.erb +14 -0
  48. data/bin/peak_flow_rspec_files +4 -2
  49. data/config/routes.rb +2 -0
  50. data/lib/peak_flow_utils.rb +10 -2
  51. data/lib/peak_flow_utils/engine.rb +7 -0
  52. data/lib/peak_flow_utils/handler_helper.rb +39 -0
  53. data/lib/peak_flow_utils/rspec_helper.rb +162 -25
  54. data/lib/peak_flow_utils/version.rb +3 -0
  55. data/lib/tasks/peak_flow_utils_tasks.rake +6 -0
  56. metadata +121 -42
  57. data/.document +0 -5
  58. data/.rspec +0 -1
  59. data/.rubocop.yml +0 -83
  60. data/Gemfile +0 -20
  61. data/Gemfile.lock +0 -93
  62. data/VERSION +0 -1
  63. data/peak_flow_utils.gemspec +0 -65
  64. data/spec/peak_flow_utils_spec.rb +0 -7
  65. data/spec/spec_helper.rb +0 -12
@@ -0,0 +1,206 @@
1
+ class PeakFlowUtils::RailsHandler < PeakFlowUtils::ApplicationHandler
2
+ def groups
3
+ ArrayEnumerator.new do |yielder|
4
+ groups = %w[date_time errors helpers numbers support]
5
+
6
+ groups.each do |group|
7
+ yielder << PeakFlowUtils::GroupService.new(
8
+ id: group,
9
+ handler: self,
10
+ data: {
11
+ name: group.humanize
12
+ }
13
+ )
14
+ end
15
+ end
16
+ end
17
+
18
+ def translations_for_group(group)
19
+ ArrayEnumerator.new do |yielder|
20
+ dir = Rails.root.join("config", "locales", "awesome_translations", group.id).to_s
21
+ __send__("translations_for_#{group.id}", dir, group, yielder)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def translations_for_errors(dir, group, yielder) # rubocop:disable Metrics/MethodLength
28
+ add_translations_for_hash(
29
+ dir,
30
+ group,
31
+ yielder,
32
+ translations: {
33
+ errors: {
34
+ format: "%{attribute} %{message}",
35
+ messages: {
36
+ accepted: "must be accepted",
37
+ blank: "can't be blank",
38
+ confirmation: "doesn't match %{attribute}",
39
+ empty: "can't be empty",
40
+ equal_to: "must be equal to %{count}",
41
+ even: "must be even",
42
+ exclusion: "is reserved",
43
+ greater_than: "must be greater than %{count}",
44
+ greater_than_or_equal_to: "must be greater than or equal to %{count}",
45
+ inclusion: "is not included in the list",
46
+ invalid: "is invalid",
47
+ less_than: "must be less than %{count}",
48
+ less_than_or_equal_to: "must be less than or equal to %{count}",
49
+ not_a_number: "is not a number",
50
+ not_an_integer: "must be an integer",
51
+ odd: "must be odd",
52
+ record_invalid: "Validation failed: %{errors}",
53
+ restrict_dependent_destroy: {
54
+ one: "Cannot delete record because a dependent %{record} exists",
55
+ other: "Cannot delete record because dependent %{record} exist"
56
+ },
57
+ taken: "has already been taken",
58
+ too_long: {
59
+ one: "is too long (maximum is 1 character)",
60
+ other: "is too long (maximum is %{count} characters)"
61
+ },
62
+ too_short: {
63
+ one: "is too short (minimum is 1 character)",
64
+ other: "is too short (minimum is %{count} characters)"
65
+ },
66
+ wrong_length: {
67
+ one: "is the wrong length (should be 1 character)",
68
+ other: "is the wrong length (should be %{count} characters)"
69
+ }
70
+ },
71
+ template: {
72
+ body: "There were problems with the following fields:",
73
+ header: {
74
+ one: "1 error prohibited this %{model} from being saved",
75
+ other: "%{count} errors prohibited this %{model} from being saved"
76
+ }
77
+ }
78
+ }
79
+ }
80
+ )
81
+ end
82
+
83
+ def translations_for_date_time(dir, group, yielder)
84
+ add_translations_for_hash(
85
+ dir,
86
+ group,
87
+ yielder,
88
+ translations: {
89
+ date: {
90
+ formats: {
91
+ default: "%Y-%m-%d",
92
+ short: "%b %d",
93
+ long: "%B %d, %Y"
94
+ },
95
+ day_names: [0, 1, 2, 3, 4, 5, 6],
96
+ abbr_day_names: [0, 1, 2, 3, 4, 5, 6],
97
+ month_names: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
98
+ abbr_month_names: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
99
+ order: [0, 1, 2]
100
+ },
101
+ time: {
102
+ formats: {
103
+ default: "%a, %d %b %Y %H:%M:%S %z",
104
+ short: "%d %b %H:%M",
105
+ long: "%B %d, %Y %H:%M"
106
+ },
107
+ am: "am",
108
+ pm: "pm"
109
+ }
110
+ }
111
+ )
112
+ end
113
+
114
+ def translations_for_helpers(dir, group, yielder)
115
+ add_translations_for_hash(
116
+ dir,
117
+ group,
118
+ yielder,
119
+ translations: {
120
+ helpers: {
121
+ select: {
122
+ prompt: "Please select"
123
+ },
124
+ submit: {
125
+ create: "Create %{model}",
126
+ submit: "Save %{model}",
127
+ update: "Update %{model}"
128
+ }
129
+ }
130
+ }
131
+ )
132
+ end
133
+
134
+ def translations_for_support(dir, group, yielder)
135
+ add_translations_for_hash(
136
+ dir,
137
+ group,
138
+ yielder,
139
+ translations: {
140
+ array: {
141
+ last_word_connector: " and ",
142
+ two_words_connector: " and ",
143
+ word_connector: ", "
144
+ }
145
+ }
146
+ )
147
+ end
148
+
149
+ def translations_for_numbers(dir, group, yielder) # rubocop:disable Metrics/MethodLength
150
+ add_translations_for_hash(
151
+ dir,
152
+ group,
153
+ yielder,
154
+ translations: {
155
+ number: {
156
+ currency: {
157
+ format: {
158
+ delimiter: ",",
159
+ format: "%n %u",
160
+ separator: ".",
161
+ unit: "$"
162
+ }
163
+ },
164
+ format: {
165
+ delimiter: ",",
166
+ separator: "."
167
+ },
168
+ human: {
169
+ decimal_units: {
170
+ format: "%n %u",
171
+ units: {
172
+ billion: "Billion",
173
+ million: "Million",
174
+ quadrillion: "Quadrillion",
175
+ thousand: "Thousand",
176
+ trillion: "Trillion",
177
+ unit: ""
178
+ }
179
+ },
180
+ format: {
181
+ delimiter: ""
182
+ },
183
+ storage_units: {
184
+ format: "%n %u",
185
+ units: {
186
+ byte: {
187
+ one: "Byte",
188
+ other: "Bytes"
189
+ },
190
+ gb: "GB",
191
+ kb: "KB",
192
+ mb: "MB",
193
+ tb: "TB"
194
+ }
195
+ }
196
+ },
197
+ percentage: {
198
+ format: {
199
+ delimiter: ""
200
+ }
201
+ }
202
+ }
203
+ }
204
+ )
205
+ end
206
+ end
@@ -0,0 +1,76 @@
1
+ class PeakFlowUtils::SimpleFormHandler < PeakFlowUtils::ApplicationHandler
2
+ def enabled?
3
+ ::Object.const_defined?(:SimpleForm)
4
+ end
5
+
6
+ def groups
7
+ ArrayEnumerator.new do |yielder|
8
+ PeakFlowUtils::ModelInspector.model_classes.each do |model_inspector|
9
+ yielder << PeakFlowUtils::GroupService.new(
10
+ id: model_inspector.clazz.name,
11
+ handler: self
12
+ )
13
+ end
14
+ end
15
+ end
16
+
17
+ def translations_for_group(group)
18
+ ArrayEnumerator.new do |yielder|
19
+ model_inspector = PeakFlowUtils::ModelInspector.model_classes.find { |model_inspector_i| model_inspector_i.clazz.name == group.name }
20
+ raise "No inspector by that name: #{group.name}" unless model_inspector
21
+
22
+ simple_form_translations(model_inspector).each { |translation| yielder << translation }
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def dir_path(model_inspector)
29
+ class_name = model_inspector.clazz.name
30
+ class_name = class_name.gsub("::", "_")
31
+ class_name = class_name.underscore
32
+
33
+ dir_path = Rails.root.join("config", "locales", "awesome_translations", "simple_form", class_name)
34
+ dir_path.to_s
35
+ end
36
+
37
+ def simple_form_translations(model_inspector)
38
+ result = []
39
+
40
+ model_inspector.attributes.each do |attribute|
41
+ translations = {}
42
+
43
+ I18n.available_locales.each do |locale|
44
+ translations[locale] = I18n.t(attribute.i18n_key, locale: locale)
45
+ end
46
+
47
+ result << PeakFlowUtils::TranslationService.new(
48
+ key: attribute.simple_form_label_key,
49
+ key_show: "#{attribute.name}.label",
50
+ dir: dir_path(model_inspector)
51
+ )
52
+ result << PeakFlowUtils::TranslationService.new(
53
+ key: attribute.simple_form_hint_key,
54
+ key_show: "#{attribute.name}.hint",
55
+ dir: dir_path(model_inspector)
56
+ )
57
+ result << PeakFlowUtils::TranslationService.new(
58
+ key: attribute.simple_form_placeholder_key,
59
+ key_show: "#{attribute.name}.placeholder",
60
+ dir: dir_path(model_inspector)
61
+ )
62
+ result << PeakFlowUtils::TranslationService.new(
63
+ key: attribute.simple_form_include_blank_key,
64
+ key_show: "#{attribute.name}.include_blank",
65
+ dir: dir_path(model_inspector)
66
+ )
67
+ result << PeakFlowUtils::TranslationService.new(
68
+ key: attribute.simple_form_prompt_key,
69
+ key_show: "#{attribute.name}.prompt",
70
+ dir: dir_path(model_inspector)
71
+ )
72
+ end
73
+
74
+ result
75
+ end
76
+ end
@@ -0,0 +1,85 @@
1
+ class PeakFlowUtils::ValidationsHandler < PeakFlowUtils::ApplicationHandler
2
+ def groups
3
+ ArrayEnumerator.new do |yielder|
4
+ PeakFlowUtils::ModelInspector.model_classes.each do |model_inspector|
5
+ yielder << PeakFlowUtils::GroupService.new(
6
+ id: model_inspector.clazz.name,
7
+ handler: self
8
+ )
9
+ end
10
+ end
11
+ end
12
+
13
+ def translations_for_group(group)
14
+ ArrayEnumerator.new do |yielder|
15
+ model_inspector = PeakFlowUtils::ModelInspector.model_classes.find { |model_inspector_i| model_inspector_i.clazz.name == group.name }
16
+ raise "No inspector by that name: #{group.name}" unless model_inspector
17
+
18
+ model_inspector.clazz._validators.each do |attribute_name, validators|
19
+ validators.each do |validator|
20
+ if validator.is_a?(ActiveModel::Validations::LengthValidator)
21
+ translations_for_length_validator(validator, model_inspector, attribute_name, yielder)
22
+ elsif validator.is_a?(ActiveModel::Validations::FormatValidator)
23
+ translations_for_format_validator(validator, model_inspector, attribute_name, yielder)
24
+ elsif validator.is_a?(ActiveRecord::Validations::UniquenessValidator)
25
+ translations_for_uniqueness_validator(validator, model_inspector, attribute_name, yielder)
26
+ elsif validator.class.name == "ActiveRecord::Validations::PresenceValidator"
27
+ translations_for_presence_validator(validator, model_inspector, attribute_name, yielder)
28
+ elsif validator.class.name == "EmailValidator"
29
+ translations_for_email_validator(validator, model_inspector, attribute_name, yielder)
30
+ elsif validator.class.name == "ActiveModel::Validations::ConfirmationValidator"
31
+ translations_for_confirmation_validator(validator, model_inspector, attribute_name, yielder)
32
+ else
33
+ Rails.logger.error "Unhandeled validator: #{validator.class.name}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def translations_for_length_validator(validator, model_inspector, attribute_name, yielder)
43
+ if validator.options[:minimum].present?
44
+ add_translation("too_short", model_inspector, attribute_name, yielder) # "is too short. The minimum is %{count}"
45
+ end
46
+
47
+ if validator.options[:maximum].present?
48
+ add_translation("too_long", model_inspector, attribute_name, yielder) # "is too long. The maximum is %{count}"
49
+ end
50
+
51
+ if validator.options[:is].present?
52
+ add_translation("wrong_length", model_inspector, attribute_name, yielder) # "is not the correct length: %{count}"
53
+ end
54
+ end
55
+
56
+ def translations_for_format_validator(_validator, model_inspector, attribute_name, yielder)
57
+ add_translation("invalid", model_inspector, attribute_name, yielder) # "is invalid"
58
+ end
59
+
60
+ def translations_for_uniqueness_validator(_validator, model_inspector, attribute_name, yielder)
61
+ add_translation("taken", model_inspector, attribute_name, yielder) # "has already been taken"
62
+ end
63
+
64
+ def translations_for_presence_validator(_validator, model_inspector, attribute_name, yielder)
65
+ add_translation("blank", model_inspector, attribute_name, yielder) # "cannot be blank"
66
+ end
67
+
68
+ def translations_for_email_validator(_validator, model_inspector, attribute_name, yielder)
69
+ add_translation("invalid", model_inspector, attribute_name, yielder) # "is invalid"
70
+ end
71
+
72
+ def translations_for_confirmation_validator(_validator, model_inspector, attribute_name, yielder)
73
+ add_translation("confirmation", model_inspector, "#{attribute_name}_confirmation", yielder)
74
+ end
75
+
76
+ def add_translation(key, model_inspector, attribute_name, yielder)
77
+ snake_clazz_name = model_inspector.clazz.name.underscore
78
+
79
+ yielder << PeakFlowUtils::TranslationService.new(
80
+ key: "activerecord.errors.models.#{snake_clazz_name}.attributes.#{attribute_name}.#{key}",
81
+ key_show: "#{attribute_name}.#{key}",
82
+ dir: "#{Rails.root}/config/locales/awesome_translations/models/#{snake_clazz_name}"
83
+ )
84
+ end
85
+ end
@@ -0,0 +1,59 @@
1
+ class PeakFlowUtils::WillPaginateHandler < PeakFlowUtils::ApplicationHandler
2
+ def enabled?
3
+ ::Object.const_defined?(:WillPaginate)
4
+ end
5
+
6
+ def groups
7
+ ArrayEnumerator.new do |yielder|
8
+ groups = ["will_paginate"]
9
+
10
+ groups.each do |group|
11
+ yielder << PeakFlowUtils::GroupService.new(
12
+ id: group,
13
+ handler: self,
14
+ data: {
15
+ name: group.humanize
16
+ }
17
+ )
18
+ end
19
+ end
20
+ end
21
+
22
+ def translations_for_group(group)
23
+ ArrayEnumerator.new do |yielder|
24
+ dir = Rails.root.join("config", "locales", "awesome_translations", group.id).to_s
25
+ __send__("translations_for_#{group.id}", dir, group, yielder)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def translations_for_will_paginate(dir, group, yielder)
32
+ add_translations_for_hash(
33
+ dir,
34
+ group,
35
+ yielder,
36
+ translations: {
37
+ will_paginate: {
38
+ previous_label: "&#8592; Previous",
39
+ next_label: "Next &#8594;",
40
+ page_gap: "&hellip;",
41
+ page_entries_info: {
42
+ single_page: {
43
+ zero: "No %{model} found",
44
+ one: "Displaying 1 %{model}",
45
+ other: "Displaying all %{count} %{model}"
46
+ },
47
+ single_page_html: {
48
+ zero: "No %{model} found",
49
+ one: "Displaying <b>1</b> %{model}",
50
+ other: "Displaying <b>all&nbsp;%{count}</b> %{model}"
51
+ },
52
+ multi_page: "Displaying %{model} %{from} - %{to} of %{count} in total",
53
+ multi_page_html: "Displaying %{model} <b>%{from}&nbsp;-&nbsp;%{to}</b> of <b>%{count}</b> in total"
54
+ }
55
+ }
56
+ }
57
+ )
58
+ end
59
+ end
@@ -0,0 +1,2 @@
1
+ module PeakFlowUtils::ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class PeakFlowUtils::ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class PeakFlowUtils::ApplicationMailer < ActionMailer::Base
2
+ default from: "from@example.com"
3
+ layout "mailer"
4
+ end
@@ -0,0 +1,8 @@
1
+ class CreateTranslationKeys < PeakFlowUtils::ApplicationMigration
2
+ def change
3
+ create_table :peak_flow_utils_translation_keys do |t|
4
+ t.string :key, index: true, null: false
5
+ t.timestamps
6
+ end
7
+ end
8
+ end