tramway-core 1.17.2 → 1.17.2.5

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/app/controllers/tramway/core/application_controller.rb +8 -12
  4. data/app/decorators/tramway/core/application_decorator.rb +12 -26
  5. data/app/decorators/tramway/core/associations/class_helper.rb +8 -14
  6. data/app/decorators/tramway/core/associations/object_helper.rb +31 -3
  7. data/app/decorators/tramway/core/attributes/view_helper.rb +26 -0
  8. data/app/decorators/tramway/core/concerns/attributes_decorator_helper.rb +47 -26
  9. data/app/decorators/tramway/core/delegating/class_helper.rb +2 -0
  10. data/app/forms/tramway/core/application_form.rb +87 -146
  11. data/app/forms/tramway/core/application_forms/association_object_helpers.rb +27 -0
  12. data/app/forms/tramway/core/application_forms/constant_class_actions.rb +7 -0
  13. data/app/forms/tramway/core/application_forms/constant_object_actions.rb +13 -0
  14. data/app/forms/tramway/core/application_forms/properties_object_helper.rb +23 -0
  15. data/app/forms/tramway/core/extendable_form.rb +3 -70
  16. data/app/forms/tramway/core/extendable_forms_helpers/class_builder.rb +34 -0
  17. data/app/forms/tramway/core/extendable_forms_helpers/ignored_properties_helper.rb +11 -0
  18. data/app/forms/tramway/core/extendable_forms_helpers/more_properties_helper.rb +27 -0
  19. data/app/forms/tramway/core/extendable_forms_helpers/properties_helper.rb +16 -0
  20. data/app/forms/tramway/core/extendable_forms_helpers/submit/class_helpers.rb +18 -0
  21. data/app/forms/tramway/core/extendable_forms_helpers/submit/object_helpers.rb +7 -0
  22. data/app/forms/tramway/core/extendable_forms_helpers/validators.rb +40 -0
  23. data/app/helpers/tramway/core/copy_to_clipboard_helper.rb +6 -10
  24. data/app/helpers/tramway/core/title_helper.rb +17 -22
  25. data/app/models/tramway/core/application_record.rb +38 -40
  26. data/app/uploaders/image_defaults.rb +2 -1
  27. data/app/uploaders/photo_uploader.rb +8 -8
  28. data/config/initializers/plurals.rb +2 -2
  29. data/lib/tramway/collection.rb +4 -6
  30. data/lib/tramway/collections.rb +4 -0
  31. data/lib/tramway/collections/helper.rb +15 -14
  32. data/lib/tramway/core.rb +22 -22
  33. data/lib/tramway/core/engine.rb +4 -8
  34. data/lib/tramway/core/generators.rb +6 -0
  35. data/lib/tramway/core/generators/install_generator.rb +17 -17
  36. data/lib/tramway/core/version.rb +1 -1
  37. data/lib/tramway/error.rb +12 -1
  38. data/lib/yaml/errors.yml +35 -0
  39. metadata +19 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c40b8698ddbb5a6d4a3bcd28c91178aea7c2b69ecbd91a3aa0c1c52935acff9
4
- data.tar.gz: fa1d9920ee66893ad98b227d48806ba71e932216f927e33124ebd0c98be4d4b3
3
+ metadata.gz: e1fd2dbf4e83157c214af26cda3f420c2262424d412e0be024370276a376c9f1
4
+ data.tar.gz: 0d7183e4be0137e326e4c65e01af71c3d9d43a8085245dceb67d5903332a616e
5
5
  SHA512:
6
- metadata.gz: 289701396c7578d7202d1bbf7bcfd794e360ef258c0396b82b98eacc76049a974a75e122a63e48893bb1cc8fd966e921c0fcc8a642c7dcfde2e45c8bb39b1eb7
7
- data.tar.gz: f69609928571b316461d3ddf7d25fc59b5abadb0d0d788c53572d146be06f585400a8736f44d64adab5b7935eca4c3e6387086890f3e3ee2a47bb3f80c1896e6
6
+ metadata.gz: a757831c656315d7fe43f5b76a0fff363170d55b2042a1f520bda007c57605f93f36a808e6f82d82b0f7aa94f523c12b552efae501fc076165f2bc88bf4ea94f
7
+ data.tar.gz: ca9475b9bfaa9bd0c80fdfd14d502dbec4e30712d6c2260e9ac6b8607d16851d4eac117e795301d102c3a73d07fa802a3ff57e9c8f78e125e52dce13493a8269
data/README.md CHANGED
@@ -203,8 +203,10 @@ Tramway::Admin.set_singleton_models Organization, project: :organization # now y
203
203
  * models - часто используемые в моделях слова
204
204
  * state_machines - локализация состояний
205
205
 
206
- ## Contributing
207
- Contribution directions go here.
206
+ ## Contributors
207
+
208
+ * [Pavel Kalashnikov](https://github.com/kalashnikovisme)
209
+ * [moshinaan](https://github.com/moshinaan)
208
210
 
209
211
  ## License
210
212
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,18 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Tramway
4
- module Core
5
- class ApplicationController < ActionController::Base
6
- before_action :application
7
- before_action :load_extensions
3
+ class Tramway::Core::ApplicationController < ActionController::Base
4
+ before_action :application
5
+ before_action :load_extensions
8
6
 
9
- def application
10
- @application = ::Tramway::Core.application_object
11
- end
7
+ def application
8
+ @application = ::Tramway::Core.application_object
9
+ end
12
10
 
13
- def load_extensions
14
- ::Tramway::Extensions.load if defined? ::Tramway::Extensions
15
- end
16
- end
11
+ def load_extensions
12
+ ::Tramway::Extensions.load if defined? ::Tramway::Extensions
17
13
  end
18
14
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tramway/error'
4
+ require 'tramway/helpers/class_name_helpers'
4
5
 
5
6
  class Tramway::Core::ApplicationDecorator
6
7
  include ActionView::Helpers
@@ -8,6 +9,8 @@ class Tramway::Core::ApplicationDecorator
8
9
  include ::FontAwesome5::Rails::IconHelper
9
10
  include ::Tramway::Core::CopyToClipboardHelper
10
11
  include ::Tramway::Core::Associations::ObjectHelper
12
+ include ::Tramway::Core::Attributes::ViewHelper
13
+ include ::Tramway::ClassNameHelpers
11
14
 
12
15
  def initialize(object)
13
16
  @object = object
@@ -18,8 +21,10 @@ class Tramway::Core::ApplicationDecorator
18
21
  end
19
22
 
20
23
  def title
21
- error = Tramway::Error.new(plugin: :core, method: :title, message: "Please, implement `title` method in a #{self.class} or #{object.class}")
22
- raise error.message
24
+ Tramway::Error.raise_error(
25
+ :tramway, :core, :application_decorator, :title, :please_implement_title,
26
+ class_name: self.class, object_class: object.class
27
+ )
23
28
  end
24
29
 
25
30
  delegate :id, to: :object
@@ -65,17 +70,11 @@ class Tramway::Core::ApplicationDecorator
65
70
  end
66
71
  end
67
72
 
68
-
69
73
  def link
70
74
  if object.try :file
71
75
  object.file.url
72
76
  else
73
- error = Tramway::Error.new(
74
- plugin: :core,
75
- method: :link,
76
- message: "Method `link` uses `file` attribute of the decorated object. If decorated object doesn't contain `file`, you shouldn't use `link` method."
77
- )
78
- raise error.message
77
+ Tramway::Error.raise_error :tramway, :core, :application_decorator, :link, :method_link_uses_file_attribute
79
78
  end
80
79
  end
81
80
 
@@ -98,25 +97,12 @@ class Tramway::Core::ApplicationDecorator
98
97
  def attributes
99
98
  object.attributes.reduce({}) do |hash, attribute|
100
99
  if attribute[0].in? RESERVED_WORDS
101
- error = Tramway::Error.new(
102
- plugin: :core,
103
- method: :attributes,
104
- message: "Method `#{attribute[0]}` is reserved word. Please, create or delegate method in #{self.class.name} with another name."
100
+ Tramway::Error.raise_error(
101
+ :tramway, :core, :application_decorator, :attributes, :method_is_reserved_word,
102
+ attribute_name: attribute[0], class_name: self.class.name
105
103
  )
106
- raise error.message
107
- end
108
- value = try(attribute[0]) ? send(attribute[0]) : object.send(attribute[0])
109
- if attribute[0].to_s.in? object.class.state_machines.keys.map(&:to_s)
110
- hash.merge! attribute[0] => state_machine_view(object, attribute[0])
111
- elsif value.class.in? [ActiveSupport::TimeWithZone, DateTime, Time]
112
- hash.merge! attribute[0] => datetime_view(attribute[1])
113
- elsif value.class.superclass == ApplicationUploader
114
- hash.merge! attribute[0] => image_view(object.send(attribute[0]))
115
- elsif value.is_a? Enumerize::Value
116
- hash.merge! attribute[0] => enumerize_view(value)
117
- else
118
- hash.merge! attribute[0] => value
119
104
  end
105
+ hash.merge! attribute[0] => build_viewable_value(object, attribute)
120
106
  end
121
107
  end
122
108
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Tramway::Core::Associations::ClassHelper
2
4
  def decorate_association(association_name, decorator: nil, as: nil, state_machines: [])
3
5
  @@decorated_associations ||= []
@@ -14,7 +16,7 @@ module Tramway::Core::Associations::ClassHelper
14
16
  end
15
17
 
16
18
  define_method "add_#{association_name}_form" do
17
- "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form".constantize.new object
19
+ add_association_form_class_name(object, association_name).new object
18
20
  end
19
21
  end
20
22
 
@@ -24,23 +26,15 @@ module Tramway::Core::Associations::ClassHelper
24
26
  end
25
27
  end
26
28
 
27
-
28
29
  def define_main_association_method(association_name, decorator)
29
30
  define_method association_name do
30
31
  association = object.class.reflect_on_association(association_name)
31
- if association.nil?
32
- error = Tramway::Error.new(plugin: :core, method: :decorate_association, message: "Model #{object.class} does not have association named `#{association_name}`")
33
- raise error.message
34
- end
35
- decorator_class_name = decorator || "#{class_name(association).to_s.singularize}Decorator".constantize
36
- if association.class.in? [ ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasAndBelongsToManyReflection ]
37
- return object.send(association_name).active.map do |association_object|
38
- decorator_class_name.decorate association_object
39
- end
40
- end
41
- if association.class == ActiveRecord::Reflection::BelongsToReflection
42
- return decorator_class_name.decorate object.send association_name
32
+ check_association object, association_name, association
33
+ decorator_class_name = decorator || decorator_class_name(class_name(association))
34
+ if association_type(association).in? %i[has_many has_and_belongs_to_many]
35
+ return associations_collection(object, association_name, decorator_class_name)
43
36
  end
37
+ return decorator_class_name.decorate object.send association_name if association_type(association) == :belongs_to
44
38
  end
45
39
  end
46
40
  end
@@ -1,13 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Tramway::Core::Associations::ObjectHelper
2
4
  def class_name(association)
3
5
  if association.polymorphic?
4
- object.send(association_name).class
6
+ object.send(association.name).class
5
7
  else
6
8
  unless association.options[:class_name]
7
- error = Tramway::Error.new(plugin: :core, method: :decorate_association, message: "Please, specify `#{association_name}` association class_name in #{object.class} model. For example: `has_many :#{association_name}, class_name: '#{association_name.to_s.singularize.camelize}'`")
8
- raise error.message
9
+ Tramway::Error.raise_error(
10
+ :tramway, :core, :associations, :object_helper, :please_specify_association_name,
11
+ association_name: association.name, object_class: object.class,
12
+ association_class_name: association.name.to_s.singularize.camelize
13
+ )
9
14
  end
10
15
  association.options[:class_name]
11
16
  end
12
17
  end
18
+
19
+ def check_association(object, association_name, association)
20
+ return unless association.nil?
21
+
22
+ Tramway::Error.raise_error(
23
+ :tramway, :core, :associations, :class_helper, :model_does_not_have_association,
24
+ object_class: object.class, association_name: association_name
25
+ )
26
+ end
27
+
28
+ def association_type(association)
29
+ association.class.to_s.split('::').last.sub(/Reflection$/, '').underscore.to_sym
30
+ end
31
+
32
+ def associations_collection(object, association_name, decorator_class_name)
33
+ object.send(association_name).active.map do |association_object|
34
+ decorator_class_name.decorate association_object
35
+ end
36
+ end
37
+
38
+ def add_association_form_class_name(object, association_name)
39
+ "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form".constantize
40
+ end
13
41
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway::Core::Attributes::ViewHelper
4
+ def build_viewable_value(object, attribute)
5
+ value = try(attribute[0]) ? send(attribute[0]) : object.send(attribute[0])
6
+ return state_machine_view(object, attribute[0]) if state_machine? object, attribute[0]
7
+
8
+ view_by_value object, value, attribute
9
+ end
10
+
11
+ def state_machine?(object, attribute_name)
12
+ attribute_name.to_s.in? object.class.state_machines.keys.map(&:to_s)
13
+ end
14
+
15
+ def view_by_value(object, value, attribute)
16
+ if value.class.in? [ActiveSupport::TimeWithZone, DateTime, Time]
17
+ datetime_view(attribute[1])
18
+ elsif value.class.superclass == ApplicationUploader
19
+ image_view(object.send(attribute[0]))
20
+ elsif value.is_a? Enumerize::Value
21
+ enumerize_view(value)
22
+ else
23
+ value
24
+ end
25
+ end
26
+ end
@@ -13,37 +13,58 @@ module Tramway::Core::Concerns::AttributesDecoratorHelper
13
13
  object.send "human_#{attribute_name}_name"
14
14
  end
15
15
 
16
+ BASE64_REGEXP = %r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|
17
+ (?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$}x.freeze
18
+
16
19
  def image_view(original, thumb: nil, filename: nil)
17
- if original.present?
18
- thumb ||= original.is_a?(CarrierWave::Uploader::Base) ? original.small : nil
19
- filename ||= original.is_a?(CarrierWave::Uploader::Base) ? original.path&.split('/')&.last : nil
20
- src_thumb = if thumb&.is_a?(CarrierWave::Uploader::Base)
21
- thumb.url
22
- elsif thumb&.match(%r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$})
23
- "data:image/jpeg;base64,#{thumb}"
24
- else
25
- thumb
26
- end
27
- src_original = if original.is_a?(CarrierWave::Uploader::Base)
28
- original.url
29
- elsif original.match(%r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$})
30
- "data:image/jpeg;base64,#{original}"
31
- else
32
- original
33
- end
34
- content_tag(:div) do
35
- begin
36
- concat image_tag src_thumb || src_original
37
- rescue NoMethodError => e
38
- error = Tramway::Error.new plugin: :core, method: :image_view, message: "You should mount PhotoUploader to your model. Just add `mount_uploader \#{attribute_name}, PhotoUploader` to your model. #{e.message}"
39
- raise error.message
40
- end
41
- concat link_to(fa_icon(:download), src_original, class: 'btn btn-success', download: filename) if filename
42
- end
20
+ return unless original.present?
21
+
22
+ filename ||= build_filename(original)
23
+ content_tag(:div) do
24
+ build_div_content src_original(original), src_thumb(original, thumb), filename || build_filename(original)
43
25
  end
44
26
  end
45
27
 
46
28
  def enumerize_view(value)
47
29
  value.text
48
30
  end
31
+
32
+ private
33
+
34
+ def src_thumb(original, thumb)
35
+ thumb ||= original.is_a?(CarrierWave::Uploader::Base) ? original.small : nil
36
+ if thumb&.is_a?(CarrierWave::Uploader::Base)
37
+ thumb.url
38
+ elsif thumb&.match(BASE64_REGEXP)
39
+ "data:image/jpeg;base64,#{thumb}"
40
+ else
41
+ thumb
42
+ end
43
+ end
44
+
45
+ def src_original(original)
46
+ if original.is_a?(CarrierWave::Uploader::Base)
47
+ original.url
48
+ elsif original.match(BASE64_REGEXP)
49
+ "data:image/jpeg;base64,#{original}"
50
+ else
51
+ original
52
+ end
53
+ end
54
+
55
+ def build_filename(original)
56
+ original.is_a?(CarrierWave::Uploader::Base) ? original.path&.split('/')&.last : nil
57
+ end
58
+
59
+ def build_div_content(original, thumb, filename)
60
+ begin
61
+ concat image_tag src_thumb(original, thumb) || src_original(original)
62
+ rescue NoMethodError => e
63
+ Tramway::Error.raise_error(
64
+ :tramway, :core, :concerns, :attributes_decorator_helper, :you_should_mount_photo_uploader,
65
+ message: e.message, attribute_name: attribute_name
66
+ )
67
+ end
68
+ concat link_to(fa_icon(:download), src_original(original), class: 'btn btn-success', download: filename) if filename
69
+ end
49
70
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Tramway::Core::Delegating::ClassHelper
2
4
  def delegate_attributes(*attributes)
3
5
  attributes.each do |attr|
@@ -1,179 +1,120 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Tramway::Core
4
- class ApplicationForm < ::Reform::Form
5
- def initialize(object = nil)
6
- object ||= self.class.model_class.new
7
- super(object).tap do
8
- @@model_class = object.class
9
- @@enumerized_attributes = object.class.try :enumerized_attributes
10
- @@associations ||= []
11
-
12
- self.class.full_class_name_associations.each do |association, class_name|
13
- if class_name.is_a? Array
14
- self.class.send(:define_method, "#{association}=") do |value|
15
- association_class = value.split('_')[0..-2].join('_').camelize
16
- association_class = association_class.constantize if association_class.is_a? String
17
- if association_class.nil?
18
- raise Tramway::Error.new(plugin: :core, method: :initialize, message: 'Polymorphic association class is nil. Maybe, you should write `assocation #{association_name}` after `properties #{association_name}_id, #{association_name}_type`')
19
- else
20
- super association_class.find value.split('_')[-1]
21
- send "#{association}_type=", association_class.to_s
22
- end
23
- end
24
- else
25
- self.class.send(:define_method, "#{association}=") do |value|
26
- super class_name.find value
27
- end
28
- end
29
- end
30
-
31
- delegating object
3
+ class Tramway::Core::ApplicationForm < ::Reform::Form
4
+ include Tramway::Core::ApplicationForms::AssociationObjectHelpers
5
+ include Tramway::Core::ApplicationForms::ConstantObjectActions
6
+ include Tramway::Core::ApplicationForms::PropertiesObjectHelper
7
+
8
+ def initialize(object = nil)
9
+ object ||= self.class.model_class.new
10
+ super(object).tap do
11
+ @@model_class = object.class
12
+ @@enumerized_attributes = object.class.try :enumerized_attributes
13
+ @@associations ||= []
14
+
15
+ self.class.full_class_name_associations&.each do |association, class_name|
16
+ define_association_method association, class_name
32
17
  end
33
- end
34
18
 
35
- def submit(params)
36
- if params
37
- if validate params
38
- begin
39
- save
40
- rescue StandardError => e
41
- error = Tramway::Error.new(plugin: :core, method: :submit, message: "Looks like you have method `#{e.name.to_s.gsub('=', '')}` in #{@@model_class}. You should rename it or rename property in #{self.class}")
42
- raise error.message
43
- end
44
- else
45
- @@associations.each do |association|
46
- if errors.details[association] == [{ error: :blank }]
47
- model.send("#{association}=", send(association))
48
- end
49
- end
50
- end
51
- else
52
- error = Tramway::Error.new(plugin: :core, method: :submit, message: 'ApplicationForm::Params should not be nil')
53
- raise error.message
54
- end
19
+ delegating object
55
20
  end
21
+ end
56
22
 
57
- def model_name
58
- @@model_class.model_name
23
+ def submit(params)
24
+ if params
25
+ validate(params) ? save : collecting_associations_errors
26
+ else
27
+ Tramway::Error.raise_error(:tramway, :core, :application_form, :submit, :params_should_not_be_nil)
59
28
  end
29
+ end
60
30
 
61
- def associations
62
- @@associations
63
- end
31
+ def model_name
32
+ @@model_class.model_name
33
+ end
64
34
 
65
- def form_properties(**args)
66
- @form_properties = args
67
- end
35
+ def associations
36
+ @@associations
37
+ end
68
38
 
69
- def form_properties_additional(**args)
70
- @form_properties_additional = args
71
- end
39
+ class << self
40
+ include Tramway::Core::ApplicationForms::ConstantClassActions
72
41
 
73
- def properties
74
- return @form_properties if @form_properties
42
+ delegate :defined_enums, to: :model_class
75
43
 
76
- yaml_config_file_path = Rails.root.join('app', 'forms', "#{self.class.name.underscore}.yml")
77
- if File.exist? yaml_config_file_path
78
- @form_properties = YAML.load_file(yaml_config_file_path).deep_symbolize_keys
79
- @form_properties.deep_merge! @form_properties_additional if @form_properties_additional
80
- @form_properties
81
- else
82
- []
83
- end
44
+ def association(property)
45
+ properties property
46
+ @@associations = ((defined?(@@associations) && @@associations) || []) + [property]
47
+ end
48
+
49
+ def associations(*properties)
50
+ properties.each { |property| association property }
84
51
  end
85
52
 
86
- def build_errors; end
53
+ def full_class_name_associations
54
+ @@associations&.reduce({}) do |hash, association|
55
+ options = @@model_class.reflect_on_all_associations(:belongs_to).select do |a|
56
+ a.name == association.to_sym
57
+ end.first&.options
87
58
 
88
- def delegating(object)
89
- methods = %i[to_key errors]
90
- methods.each do |method|
91
- self.class.send(:define_method, method) do
92
- object.send method
59
+ if options&.dig(:polymorphic)
60
+ hash.merge! association => @@model_class.send("#{association}_type").values
61
+ elsif options
62
+ hash.merge!(association => (options[:class_name] || association.to_s.camelize).constantize)
93
63
  end
64
+ hash
94
65
  end
95
66
  end
96
67
 
97
- def attributes
98
- properties.reduce({}) do |hash, property|
99
- hash.merge! property.first => model.values[property.first.to_s]
100
- end
68
+ def full_class_name_association(association_name)
69
+ full_class_name_associations[association_name]
101
70
  end
102
71
 
103
- class << self
104
- delegate :defined_enums, to: :model_class
105
-
106
- def association(property)
107
- properties property
108
- @@associations ||= []
109
- @@associations << property
110
- end
72
+ def reflect_on_association(*args)
73
+ @@model_class.reflect_on_association(*args)
74
+ end
111
75
 
112
- def associations(*properties)
113
- properties.each do |property|
114
- association property
115
- end
116
- end
76
+ def enumerized_attributes
77
+ @@enumerized_attributes
78
+ end
117
79
 
118
- def full_class_name_associations
119
- @@associations&.reduce({}) do |hash, association|
120
- options = @@model_class.reflect_on_all_associations(:belongs_to).select do |a|
121
- a.name == association.to_sym
122
- end.first&.options
123
-
124
- if options
125
- if options[:polymorphic]
126
- hash.merge! association => @@model_class.send("#{association}_type").values
127
- else
128
- class_name = options[:class_name] || association.to_s.camelize
129
- hash.merge!(association => class_name.constantize)
130
- end
131
- end
132
- hash
80
+ def model_class
81
+ if defined?(@@model_class) && @@model_class
82
+ @@model_class
83
+ else
84
+ model_class_name ||= name.to_s.sub(/Form$/, '')
85
+ begin
86
+ @@model_class = model_class_name.constantize
87
+ rescue StandardError
88
+ Tramway::Error.raise_error :tramway, :core, :application_form, :model_class, :there_is_not_model_class,
89
+ name: name, model_class_name: model_class_name
133
90
  end
134
91
  end
92
+ end
135
93
 
136
- def full_class_name_association(association_name)
137
- full_class_name_associations[association_name]
138
- end
139
-
140
- def enumerized_attributes
141
- @@enumerized_attributes
142
- end
143
-
144
- def reflect_on_association(*args)
145
- @@model_class.reflect_on_association(*args)
146
- end
147
-
148
- def model_class
149
- if defined?(@@model_class) && @@model_class
150
- @@model_class
151
- else
152
- model_class_name ||= name.to_s.sub(/Form$/, '')
153
- begin
154
- @@model_class = model_class_name.constantize
155
- rescue StandardError
156
- error = Tramway::Error.new(plugin: :core, method: :model_class, message: "There is not model class name for #{name}. Should be #{model_class_name} or you can use another class to initialize form object or just initialize form with object.")
157
- raise error.message
158
- end
159
- end
160
- end
94
+ def model_class=(name)
95
+ @@model_class = name
96
+ end
161
97
 
162
- def model_class=(name)
163
- @@model_class = name
98
+ def validates(attribute, **options)
99
+ if !defined?(@@model_class) || @@model_class.nil?
100
+ Tramway::Error.raise_error(:tramway, :core, :application_form, :validates, :you_need_to_set_model_class)
164
101
  end
102
+ @@model_class.validates attribute, **options
103
+ end
104
+ end
165
105
 
166
- def validation_group_class
167
- ActiveModel
168
- end
106
+ private
169
107
 
170
- def validates(attribute, **options)
171
- if !defined?(@@model_class) || @@model_class.nil?
172
- error = Tramway::Error.new(plugin: :core, method: :validates, message: 'You need to set `model_class` name while using validations. Just write `self.model_class = YOUR_MODEL_NAME` in the class area.')
173
- raise error.message
174
- end
175
- @@model_class.validates attribute, **options
176
- end
108
+ def collecting_associations_errors
109
+ @@associations.each do |association|
110
+ model.send("#{association}=", send(association)) if errors.details[association] == [{ error: :blank }]
177
111
  end
178
112
  end
113
+
114
+ def save
115
+ super
116
+ rescue StandardError => e
117
+ Tramway::Error.raise_error :tramway, :core, :application_form, :submit, :looks_like_you_have_method,
118
+ method_name: e.name.to_s.gsub('=', ''), model_class: @@model_class, class_name: self.class
119
+ end
179
120
  end