script_core 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -1
  3. data/.gitignore +15 -1
  4. data/.rubocop.yml +10 -2
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +6 -0
  7. data/Gemfile +32 -0
  8. data/README.md +169 -6
  9. data/Rakefile +11 -0
  10. data/Vagrantfile +1 -1
  11. data/bin/rails +25 -0
  12. data/bootstrap.sh +2 -3
  13. data/dev.yml +4 -4
  14. data/ext/enterprise_script_service/Rakefile +5 -4
  15. data/ext/enterprise_script_service/mruby_config.rb +1 -0
  16. data/lib/script_core.rb +17 -15
  17. data/lib/script_core/engine.rb +1 -0
  18. data/lib/script_core/executable.rb +1 -1
  19. data/lib/script_core/railtie.rb +8 -0
  20. data/lib/script_core/spawner.rb +1 -0
  21. data/lib/script_core/version.rb +1 -1
  22. data/lib/tasks/mruby/engine.gembox.example +19 -0
  23. data/lib/tasks/mruby/lib/.keep +0 -0
  24. data/lib/tasks/script_core.rake +111 -0
  25. data/script/regression +7 -5
  26. data/script_core.gemspec +2 -2
  27. data/spec/dummy/Rakefile +8 -0
  28. data/spec/dummy/app/assets/config/manifest.js +3 -0
  29. data/spec/dummy/app/assets/images/.keep +0 -0
  30. data/spec/dummy/app/assets/javascripts/application.es6 +56 -0
  31. data/spec/dummy/app/assets/stylesheets/application.scss +65 -0
  32. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  33. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  34. data/spec/dummy/app/controllers/fields/application_controller.rb +25 -0
  35. data/spec/dummy/app/controllers/fields/options_controller.rb +26 -0
  36. data/spec/dummy/app/controllers/fields/validations_controller.rb +26 -0
  37. data/spec/dummy/app/controllers/forms/application_controller.rb +14 -0
  38. data/spec/dummy/app/controllers/forms/fields_controller.rb +56 -0
  39. data/spec/dummy/app/controllers/forms/formulas/application_controller.rb +19 -0
  40. data/spec/dummy/app/controllers/forms/formulas/playgrounds_controller.rb +29 -0
  41. data/spec/dummy/app/controllers/forms/formulas_controller.rb +56 -0
  42. data/spec/dummy/app/controllers/forms_controller.rb +58 -0
  43. data/spec/dummy/app/controllers/home_controller.rb +7 -0
  44. data/spec/dummy/app/controllers/nested_forms/application_controller.rb +12 -0
  45. data/spec/dummy/app/controllers/nested_forms/fields_controller.rb +50 -0
  46. data/spec/dummy/app/controllers/playgrounds_controller.rb +29 -0
  47. data/spec/dummy/app/decorators/.keep +0 -0
  48. data/spec/dummy/app/helpers/application_helper.rb +21 -0
  49. data/spec/dummy/app/helpers/fields_helper.rb +32 -0
  50. data/spec/dummy/app/helpers/forms_helper.rb +14 -0
  51. data/spec/dummy/app/jobs/application_job.rb +4 -0
  52. data/spec/dummy/app/lib/script_engine.rb +22 -0
  53. data/spec/dummy/app/models/application_record.rb +8 -0
  54. data/spec/dummy/app/models/concerns/.keep +0 -0
  55. data/spec/dummy/app/models/concerns/acts_as_default_value.rb +152 -0
  56. data/spec/dummy/app/models/concerns/enum_attribute_localizable.rb +32 -0
  57. data/spec/dummy/app/models/concerns/fields/validations/acceptance.rb +18 -0
  58. data/spec/dummy/app/models/concerns/fields/validations/confirmation.rb +18 -0
  59. data/spec/dummy/app/models/concerns/fields/validations/exclusion.rb +35 -0
  60. data/spec/dummy/app/models/concerns/fields/validations/format.rb +45 -0
  61. data/spec/dummy/app/models/concerns/fields/validations/inclusion.rb +35 -0
  62. data/spec/dummy/app/models/concerns/fields/validations/length.rb +59 -0
  63. data/spec/dummy/app/models/concerns/fields/validations/numericality.rb +56 -0
  64. data/spec/dummy/app/models/concerns/fields/validations/presence.rb +18 -0
  65. data/spec/dummy/app/models/concerns/form_core/acts_as_default_value.rb +143 -0
  66. data/spec/dummy/app/models/field.rb +67 -0
  67. data/spec/dummy/app/models/field_options.rb +90 -0
  68. data/spec/dummy/app/models/fields.rb +10 -0
  69. data/spec/dummy/app/models/fields/boolean_field.rb +12 -0
  70. data/spec/dummy/app/models/fields/decimal_field.rb +12 -0
  71. data/spec/dummy/app/models/fields/integer_field.rb +20 -0
  72. data/spec/dummy/app/models/fields/multiple_nested_form_field.rb +35 -0
  73. data/spec/dummy/app/models/fields/nested_form_field.rb +37 -0
  74. data/spec/dummy/app/models/fields/options/boolean_field.rb +6 -0
  75. data/spec/dummy/app/models/fields/options/decimal_field.rb +12 -0
  76. data/spec/dummy/app/models/fields/options/integer_field.rb +13 -0
  77. data/spec/dummy/app/models/fields/options/multiple_nested_form_field.rb +6 -0
  78. data/spec/dummy/app/models/fields/options/nested_form_field.rb +6 -0
  79. data/spec/dummy/app/models/fields/options/text_field.rb +7 -0
  80. data/spec/dummy/app/models/fields/text_field.rb +12 -0
  81. data/spec/dummy/app/models/fields/validations/boolean_field.rb +7 -0
  82. data/spec/dummy/app/models/fields/validations/decimal_field.rb +8 -0
  83. data/spec/dummy/app/models/fields/validations/integer_field.rb +8 -0
  84. data/spec/dummy/app/models/fields/validations/multiple_nested_form_field.rb +8 -0
  85. data/spec/dummy/app/models/fields/validations/nested_form_field.rb +7 -0
  86. data/spec/dummy/app/models/fields/validations/text_field.rb +9 -0
  87. data/spec/dummy/app/models/form.rb +8 -0
  88. data/spec/dummy/app/models/formula.rb +8 -0
  89. data/spec/dummy/app/models/metal_form.rb +9 -0
  90. data/spec/dummy/app/models/nested_form.rb +5 -0
  91. data/spec/dummy/app/models/virtual_model.rb +23 -0
  92. data/spec/dummy/app/presenters/application_presenter.rb +11 -0
  93. data/spec/dummy/app/presenters/concerns/fields/presenter_for_number_field.rb +36 -0
  94. data/spec/dummy/app/presenters/fields/boolean_field_presenter.rb +13 -0
  95. data/spec/dummy/app/presenters/fields/composite_field_presenter.rb +13 -0
  96. data/spec/dummy/app/presenters/fields/decimal_field_presenter.rb +7 -0
  97. data/spec/dummy/app/presenters/fields/field_presenter.rb +44 -0
  98. data/spec/dummy/app/presenters/fields/integer_field_presenter.rb +11 -0
  99. data/spec/dummy/app/presenters/fields/multiple_nested_form_field_presenter.rb +9 -0
  100. data/spec/dummy/app/presenters/fields/nested_form_field_presenter.rb +9 -0
  101. data/spec/dummy/app/presenters/fields/text_field_presenter.rb +10 -0
  102. data/spec/dummy/app/views/_form_core/field_options/_decimal_field.html.erb +9 -0
  103. data/spec/dummy/app/views/_form_core/field_options/_integer_field.html.erb +10 -0
  104. data/spec/dummy/app/views/_form_core/field_options/_text_field.html.erb +12 -0
  105. data/spec/dummy/app/views/_form_core/fields/_boolean_field.html.erb +11 -0
  106. data/spec/dummy/app/views/_form_core/fields/_decimal_field.html.erb +9 -0
  107. data/spec/dummy/app/views/_form_core/fields/_integer_field.html.erb +9 -0
  108. data/spec/dummy/app/views/_form_core/fields/_multiple_nested_form_field.html.erb +23 -0
  109. data/spec/dummy/app/views/_form_core/fields/_nested_form.html.erb +37 -0
  110. data/spec/dummy/app/views/_form_core/fields/_nested_form_field.html.erb +42 -0
  111. data/spec/dummy/app/views/_form_core/fields/_text_field.html.erb +13 -0
  112. data/spec/dummy/app/views/_form_core/preview/_form.html.erb +17 -0
  113. data/spec/dummy/app/views/_form_core/preview/_nested_form.html.erb +22 -0
  114. data/spec/dummy/app/views/_form_core/render/_form.html.erb +39 -0
  115. data/spec/dummy/app/views/_form_core/validations/_acceptance.html.erb +12 -0
  116. data/spec/dummy/app/views/_form_core/validations/_confirmation.html.erb +12 -0
  117. data/spec/dummy/app/views/_form_core/validations/_exclusion.html.erb +2 -0
  118. data/spec/dummy/app/views/_form_core/validations/_format.html.erb +37 -0
  119. data/spec/dummy/app/views/_form_core/validations/_inclusion.html.erb +2 -0
  120. data/spec/dummy/app/views/_form_core/validations/_length.html.erb +44 -0
  121. data/spec/dummy/app/views/_form_core/validations/_numericality.html.erb +45 -0
  122. data/spec/dummy/app/views/_form_core/validations/_presence.html.erb +12 -0
  123. data/spec/dummy/app/views/fields/options/_form.html.erb +29 -0
  124. data/spec/dummy/app/views/fields/options/edit.html.erb +6 -0
  125. data/spec/dummy/app/views/fields/validations/_form.html.erb +34 -0
  126. data/spec/dummy/app/views/fields/validations/edit.html.erb +6 -0
  127. data/spec/dummy/app/views/forms/_form.html.erb +41 -0
  128. data/spec/dummy/app/views/forms/edit.html.erb +6 -0
  129. data/spec/dummy/app/views/forms/fields/_form.html.erb +71 -0
  130. data/spec/dummy/app/views/forms/fields/edit.html.erb +6 -0
  131. data/spec/dummy/app/views/forms/fields/index.html.erb +42 -0
  132. data/spec/dummy/app/views/forms/fields/new.html.erb +6 -0
  133. data/spec/dummy/app/views/forms/formulas/_form.html.erb +41 -0
  134. data/spec/dummy/app/views/forms/formulas/edit.html.erb +6 -0
  135. data/spec/dummy/app/views/forms/formulas/index.html.erb +28 -0
  136. data/spec/dummy/app/views/forms/formulas/new.html.erb +6 -0
  137. data/spec/dummy/app/views/forms/formulas/playgrounds/create.html.erb +48 -0
  138. data/spec/dummy/app/views/forms/formulas/playgrounds/show.html.erb +14 -0
  139. data/spec/dummy/app/views/forms/index.html.erb +31 -0
  140. data/spec/dummy/app/views/forms/new.html.erb +6 -0
  141. data/spec/dummy/app/views/home/index.html.erb +5 -0
  142. data/spec/dummy/app/views/layouts/_alert.html.erb +4 -0
  143. data/spec/dummy/app/views/layouts/_footer.html.erb +13 -0
  144. data/spec/dummy/app/views/layouts/_nav.html.erb +11 -0
  145. data/spec/dummy/app/views/layouts/_notice.html.erb +4 -0
  146. data/spec/dummy/app/views/layouts/application.html.erb +25 -0
  147. data/spec/dummy/app/views/layouts/forms.html.erb +33 -0
  148. data/spec/dummy/app/views/nested_forms/fields/_form.html.erb +68 -0
  149. data/spec/dummy/app/views/nested_forms/fields/edit.html.erb +6 -0
  150. data/spec/dummy/app/views/nested_forms/fields/index.html.erb +39 -0
  151. data/spec/dummy/app/views/nested_forms/fields/new.html.erb +6 -0
  152. data/spec/dummy/app/views/playgrounds/_form.html.erb +13 -0
  153. data/spec/dummy/app/views/playgrounds/create.html.erb +51 -0
  154. data/spec/dummy/app/views/playgrounds/show.html.erb +7 -0
  155. data/spec/dummy/bin/bundle +5 -0
  156. data/spec/dummy/bin/rails +6 -0
  157. data/spec/dummy/bin/rake +6 -0
  158. data/spec/dummy/bin/setup +40 -0
  159. data/spec/dummy/bin/update +35 -0
  160. data/spec/dummy/bin/yarn +13 -0
  161. data/spec/dummy/config.ru +7 -0
  162. data/spec/dummy/config/application.rb +35 -0
  163. data/spec/dummy/config/boot.rb +7 -0
  164. data/spec/dummy/config/database.yml +25 -0
  165. data/spec/dummy/config/environment.rb +7 -0
  166. data/spec/dummy/config/environments/development.rb +58 -0
  167. data/spec/dummy/config/environments/production.rb +85 -0
  168. data/spec/dummy/config/environments/test.rb +41 -0
  169. data/spec/dummy/config/initializers/application_controller_renderer.rb +10 -0
  170. data/spec/dummy/config/initializers/assets.rb +16 -0
  171. data/spec/dummy/config/initializers/backtrace_silencers.rb +9 -0
  172. data/spec/dummy/config/initializers/content_security_policy.rb +27 -0
  173. data/spec/dummy/config/initializers/cookies_serializer.rb +7 -0
  174. data/spec/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  175. data/spec/dummy/config/initializers/form_core.rb +4 -0
  176. data/spec/dummy/config/initializers/inflections.rb +18 -0
  177. data/spec/dummy/config/initializers/mime_types.rb +6 -0
  178. data/spec/dummy/config/initializers/wrap_parameters.rb +16 -0
  179. data/spec/dummy/config/locales/en.yml +33 -0
  180. data/spec/dummy/config/puma.rb +36 -0
  181. data/spec/dummy/config/routes.rb +31 -0
  182. data/spec/dummy/config/spring.rb +8 -0
  183. data/spec/dummy/config/storage.yml +34 -0
  184. data/spec/dummy/db/migrate/20180916202025_create_forms.form_core.rb +12 -0
  185. data/spec/dummy/db/migrate/20180916202026_add_columns_to_forms.rb +10 -0
  186. data/spec/dummy/db/migrate/20180916202027_add_attachable_to_forms.rb +9 -0
  187. data/spec/dummy/db/migrate/20180916202030_create_fields.form_core.rb +17 -0
  188. data/spec/dummy/db/migrate/20180916202121_add_columns_to_fields.rb +10 -0
  189. data/spec/dummy/db/migrate/20180916202124_add_position_to_fields.rb +9 -0
  190. data/spec/dummy/db/migrate/20181111111412_create_formulas.rb +13 -0
  191. data/spec/dummy/db/schema.rb +52 -0
  192. data/spec/dummy/lib/assets/.keep +0 -0
  193. data/spec/dummy/lib/monkey_patches/active_support.rb +3 -0
  194. data/spec/dummy/lib/monkey_patches/active_support/concern+prependable.rb +28 -0
  195. data/spec/dummy/lib/monkey_patches/big_decimal.rb +8 -0
  196. data/spec/dummy/log/.keep +0 -0
  197. data/spec/dummy/mruby/engine.gembox +19 -0
  198. data/spec/dummy/mruby/lib/.keep +0 -0
  199. data/spec/dummy/mruby/lib/core_ext.rb +62 -0
  200. data/spec/dummy/mruby/lib/harden.rb +1 -0
  201. data/spec/dummy/mruby/lib/input.rb +20 -0
  202. data/spec/dummy/mruby/lib/io.rb +19 -0
  203. data/spec/dummy/mruby/lib/output.rb +35 -0
  204. data/spec/dummy/mruby/lib/script_kernel.rb +12 -0
  205. data/spec/dummy/package.json +5 -0
  206. data/spec/dummy/public/404.html +67 -0
  207. data/spec/dummy/public/422.html +67 -0
  208. data/spec/dummy/public/500.html +66 -0
  209. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  210. data/spec/dummy/public/apple-touch-icon.png +0 -0
  211. data/spec/dummy/public/favicon.ico +0 -0
  212. data/spec/dummy/storage/.keep +0 -0
  213. data/spec/dummy/test/controllers/.keep +0 -0
  214. data/spec/dummy/test/fixtures/.keep +0 -0
  215. data/spec/dummy/test/models/.keep +0 -0
  216. data/spec/script_core_spec.rb +2 -2
  217. data/spec/spec_helper.rb +2 -2
  218. metadata +199 -5
  219. data/circle.yml +0 -9
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FormsHelper
4
+ def smart_form_fields_path(form)
5
+ case form
6
+ when Form
7
+ form_fields_path(form)
8
+ when NestedForm
9
+ nested_form_fields_path(form)
10
+ else
11
+ raise "Unknown form: #{form.class}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ScriptEngine
4
+ class << self
5
+ def engine
6
+ @engine ||= ScriptCore::Engine.new Rails.root.join("mruby/bin")
7
+ end
8
+
9
+ def eval(string, input: nil, instruction_quota_start: nil, environment_variables: {})
10
+ environment_variables.reverse_merge!("TZ" => Time.zone.name)
11
+ sources = [
12
+ %w[prepare_input prepare_input],
13
+ ["user", string],
14
+ %w[prepare_output prepare_output]
15
+ ]
16
+
17
+ engine.eval sources, input: input,
18
+ instruction_quota_start: instruction_quota_start,
19
+ environment_variables: environment_variables
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+
6
+ include ActsAsDefaultValue
7
+ include EnumAttributeLocalizable
8
+ end
File without changes
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ # https://github.com/FooBarWidget/default_value_for
4
+ #
5
+ # Copyright (c) 2008-2012 Phusion
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+
25
+ module ActsAsDefaultValue
26
+ extend ActiveSupport::Concern
27
+
28
+ class NormalValueContainer
29
+ def initialize(value)
30
+ @value = value
31
+ end
32
+
33
+ def evaluate(_instance)
34
+ if @value.duplicable?
35
+ @value.dup
36
+ else
37
+ @value
38
+ end
39
+ end
40
+ end
41
+
42
+ class BlockValueContainer
43
+ def initialize(block)
44
+ @block = block
45
+ end
46
+
47
+ def evaluate(instance)
48
+ if @block.arity == 0
49
+ @block.call
50
+ else
51
+ @block.call(instance)
52
+ end
53
+ end
54
+ end
55
+
56
+ included do
57
+ after_initialize :set_default_values
58
+ end
59
+
60
+ def initialize(attributes = nil)
61
+ @initialization_attributes = attributes.is_a?(Hash) ? attributes.stringify_keys : {}
62
+ super
63
+ end
64
+
65
+ def set_default_values
66
+ self.class._all_default_attribute_values.each do |attribute, container|
67
+ next unless new_record? || self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)
68
+
69
+ connection_default_value_defined = new_record? && respond_to?("#{attribute}_changed?") && !send("#{attribute}_changed?")
70
+
71
+ column = self.class.columns.detect { |c| c.name == attribute }
72
+ attribute_blank =
73
+ if column && column.type == :boolean
74
+ send(attribute).nil?
75
+ else
76
+ send(attribute).blank?
77
+ end
78
+ next unless connection_default_value_defined || attribute_blank
79
+
80
+ # allow explicitly setting nil through allow nil option
81
+ next if @initialization_attributes.is_a?(Hash) &&
82
+ (
83
+ @initialization_attributes.key?(attribute) ||
84
+ (
85
+ @initialization_attributes.key?("#{attribute}_attributes") &&
86
+ nested_attributes_options.stringify_keys[attribute]
87
+ )
88
+ ) &&
89
+ !self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)
90
+
91
+ send("#{attribute}=", container.evaluate(self))
92
+
93
+ clear_attribute_changes [attribute] if has_attribute?(attribute)
94
+ end
95
+ end
96
+
97
+ def attributes_for_create(attribute_names)
98
+ attribute_names += self.class._all_default_attribute_values.keys.map(&:to_s).find_all do |name|
99
+ self.class.columns_hash.key?(name)
100
+ end
101
+
102
+ super
103
+ end
104
+
105
+ module ClassMethods
106
+ def _default_attribute_values # :nodoc:
107
+ @default_attribute_values ||= {}
108
+ end
109
+
110
+ def _default_attribute_values_not_allowing_nil # :nodoc:
111
+ @default_attribute_values_not_allowing_nil ||= Set.new
112
+ end
113
+
114
+ def _all_default_attribute_values # :nodoc:
115
+ if superclass.respond_to?(:_default_attribute_values)
116
+ superclass._all_default_attribute_values.merge(_default_attribute_values)
117
+ else
118
+ _default_attribute_values
119
+ end
120
+ end
121
+
122
+ def _all_default_attribute_values_not_allowing_nil # :nodoc:
123
+ if superclass.respond_to?(:_default_attribute_values_not_allowing_nil)
124
+ superclass._all_default_attribute_values_not_allowing_nil + _default_attribute_values_not_allowing_nil
125
+ else
126
+ _default_attribute_values_not_allowing_nil
127
+ end
128
+ end
129
+
130
+ # Declares a default value for the given attribute.
131
+ #
132
+ # Sets the default value to the given options parameter
133
+ #
134
+ # The <tt>options</tt> can be used to specify the following things:
135
+ # * <tt>allow_nil (default: true)</tt> - Sets explicitly passed nil values if option is set to true.
136
+ def default_value_for(attribute, value, **options)
137
+ allow_nil = options.fetch(:allow_nil, true)
138
+
139
+ container =
140
+ if value.is_a? Proc
141
+ BlockValueContainer.new(value)
142
+ else
143
+ NormalValueContainer.new(value)
144
+ end
145
+
146
+ _default_attribute_values[attribute.to_s] = container
147
+ _default_attribute_values_not_allowing_nil << attribute.to_s unless allow_nil
148
+
149
+ attribute
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnumAttributeLocalizable
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def human_enum_value(attribute, value, options = {})
8
+ parts = attribute.to_s.split(".")
9
+ attribute = parts.pop.pluralize
10
+ attributes_scope = "#{i18n_scope}.attributes"
11
+
12
+ if parts.any?
13
+ namespace = parts.join("/")
14
+ defaults = lookup_ancestors.map do |klass|
15
+ :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}.#{value}"
16
+ end
17
+ defaults << :"#{attributes_scope}.#{namespace}.#{attribute}.#{value}"
18
+ else
19
+ defaults = lookup_ancestors.map do |klass|
20
+ :"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}.#{value}"
21
+ end
22
+ end
23
+
24
+ defaults << :"attributes.#{attribute}.#{value}"
25
+ defaults << options.delete(:default) if options[:default]
26
+ defaults << value.to_s.humanize
27
+
28
+ options[:default] = defaults
29
+ I18n.translate(defaults.shift, options)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Concerns::Fields
4
+ module Validations::Acceptance
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attribute :acceptance, :boolean, default: false
9
+ end
10
+
11
+ def interpret_to(model, field_name, _accessibility, _options = {})
12
+ super
13
+ return unless acceptance
14
+
15
+ model.validates field_name, acceptance: true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Concerns::Fields
4
+ module Validations::Confirmation
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attribute :confirmation, :boolean, default: false
9
+ end
10
+
11
+ def interpret_to(model, field_name, _accessibility, _options = {})
12
+ super
13
+ return unless confirmation
14
+
15
+ model.validates field_name, confirmation: true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Concerns::Fields
4
+ module Validations::Exclusion
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ embeds_one :exclusion, class_name: "Concerns::Fields::Validations::Exclusion::ExclusionOptions"
9
+ accepts_nested_attributes_for :exclusion
10
+
11
+ after_initialize do
12
+ build_exclusion unless exclusion
13
+ end
14
+ end
15
+
16
+ def interpret_to(model, field_name, accessibility, options = {})
17
+ super
18
+ exclusion&.interpret_to model, field_name, accessibility, options
19
+ end
20
+
21
+ class ExclusionOptions < FieldOptions
22
+ attribute :message, :string, default: ""
23
+ attribute :in, :string, default: [], array: true
24
+
25
+ def interpret_to(model, field_name, _accessibility, _options = {})
26
+ return if self.in.empty?
27
+
28
+ options = {in: self.in}
29
+ options[:message] = message if message.present?
30
+
31
+ model.validates field_name, exclusion: options, allow_blank: true
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Concerns::Fields
4
+ module Validations::Format
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ embeds_one :format, class_name: "Concerns::Fields::Validations::Format::FormatOptions"
9
+ accepts_nested_attributes_for :format
10
+
11
+ after_initialize do
12
+ build_format unless format
13
+ end
14
+ end
15
+
16
+ def interpret_to(model, field_name, accessibility, options = {})
17
+ super
18
+ format&.interpret_to model, field_name, accessibility, options
19
+ end
20
+
21
+ class FormatOptions < FieldOptions
22
+ attribute :with, :string, default: ""
23
+ attribute :message, :string, default: ""
24
+
25
+ validate do
26
+ begin
27
+ Regexp.new(with) if with.present?
28
+ rescue RegexpError
29
+ errors.add :with, :invalid
30
+ end
31
+ end
32
+
33
+ def interpret_to(model, field_name, _accessibility, _options = {})
34
+ return if with.blank?
35
+
36
+ with = Regexp.new(self.with)
37
+
38
+ options = {with: with}
39
+ options[:message] = message if message.present?
40
+
41
+ model.validates field_name, format: options, allow_blank: true
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Concerns::Fields
4
+ module Validations::Inclusion
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ embeds_one :inclusion, class_name: "Concerns::Fields::Validations::Inclusion::InclusionOptions"
9
+ accepts_nested_attributes_for :inclusion
10
+
11
+ after_initialize do
12
+ build_inclusion unless inclusion
13
+ end
14
+ end
15
+
16
+ def interpret_to(model, field_name, accessibility, options = {})
17
+ super
18
+ inclusion&.interpret_to model, field_name, accessibility, options
19
+ end
20
+
21
+ class InclusionOptions < FieldOptions
22
+ attribute :message, :string, default: ""
23
+ attribute :in, :string, default: [], array: true
24
+
25
+ def interpret_to(model, field_name, _accessibility, _options = {})
26
+ return if self.in.empty?
27
+
28
+ options = {in: self.in}
29
+ options[:message] = message if message.present?
30
+
31
+ model.validates field_name, inclusion: options, allow_blank: true
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Concerns::Fields
4
+ module Validations::Length
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ embeds_one :length, class_name: "Concerns::Fields::Validations::Length::LengthOptions"
9
+ accepts_nested_attributes_for :length
10
+
11
+ after_initialize do
12
+ build_length unless length
13
+ end
14
+ end
15
+
16
+ def interpret_to(model, field_name, accessibility, options = {})
17
+ super
18
+ length&.interpret_to model, field_name, accessibility, options
19
+ end
20
+
21
+ class LengthOptions < FieldOptions
22
+ attribute :minimum, :integer, default: 0
23
+ attribute :maximum, :integer, default: 0
24
+ attribute :is, :integer, default: 0
25
+
26
+ validates :minimum, :maximum, :is,
27
+ numericality: {
28
+ greater_than_or_equal_to: 0
29
+ }
30
+ validates :maximum,
31
+ numericality: {
32
+ greater_than: :minimum
33
+ },
34
+ if: proc { |record| record.maximum <= record.minimum && record.maximum.positive? }
35
+
36
+ validates :is,
37
+ numericality: {
38
+ equal_to: 0
39
+ },
40
+ if: proc { |record| !record.maximum.zero? || !record.minimum.zero? }
41
+
42
+ def interpret_to(model, field_name, _accessibility, _options = {})
43
+ return if minimum.zero? && maximum.zero? && is.zero?
44
+
45
+ if is.positive?
46
+ model.validates field_name, length: {is: is}, allow_blank: true
47
+ return
48
+ end
49
+
50
+ options = {}
51
+ options[:minimum] = minimum if minimum.positive?
52
+ options[:maximum] = maximum if maximum.positive?
53
+ return if options.empty?
54
+
55
+ model.validates field_name, length: options, allow_blank: true
56
+ end
57
+ end
58
+ end
59
+ end