tramway-core 1.17.2.1 → 1.17.3

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/app/controllers/tramway/core/application_controller.rb +12 -12
  4. data/app/decorators/tramway/core/application_decorator.rb +12 -26
  5. data/app/decorators/tramway/core/associations/class_helper.rb +7 -13
  6. data/app/decorators/tramway/core/associations/object_helper.rb +28 -2
  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 +96 -145
  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 +20 -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/application_helper.rb +2 -0
  24. data/app/helpers/tramway/core/copy_to_clipboard_helper.rb +6 -10
  25. data/app/helpers/tramway/core/inputs_helper.rb +93 -0
  26. data/app/helpers/tramway/core/title_helper.rb +17 -22
  27. data/app/models/tramway/core/application_record.rb +38 -40
  28. data/app/uploaders/image_defaults.rb +2 -1
  29. data/app/uploaders/photo_uploader.rb +8 -8
  30. data/app/views/tramway/core/shared/_input.html.haml +26 -0
  31. data/config/initializers/plurals.rb +2 -2
  32. data/lib/tramway/collection.rb +4 -6
  33. data/lib/tramway/collections.rb +4 -0
  34. data/lib/tramway/collections/helper.rb +15 -14
  35. data/lib/tramway/core.rb +22 -22
  36. data/lib/tramway/core/engine.rb +4 -8
  37. data/lib/tramway/core/generators.rb +6 -0
  38. data/lib/tramway/core/generators/install_generator.rb +17 -17
  39. data/lib/tramway/core/version.rb +1 -1
  40. data/lib/tramway/error.rb +12 -1
  41. data/lib/yaml/errors.yml +35 -0
  42. metadata +22 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63c3cd63ade145dc9d56b1ed79122789322b6956273157b8d81a65ee8b9107af
4
- data.tar.gz: 36e9210fe35bd7e33d218b786354b71a2ad1a316cb33252345a3ae5b49a5b6b3
3
+ metadata.gz: bcffbf3198b80ae53e95b4368e1759f1fb7371c5b1b45d8f9e8d946878c29ad3
4
+ data.tar.gz: cfa47aa8309e0546e6771f940cea2039be641cb05e564b9cd6c3bb8120b8f6b0
5
5
  SHA512:
6
- metadata.gz: 6f95be747fa6d661b575968a76053a0cee7092460d1ae919aa055f4db7a18b1fa9810a1bd76d5c2af4f667b379a836c95461e7c7060ef9f1aeb16a8f5bfe9515
7
- data.tar.gz: a2e82ebb362b3d275aa4bec8e16c49ad6946dd36af98fb9dc5b09251a0b54178ce8115a5e1fe2cff8b121b99f06ed4d4f4579ee9317cb4cc64f85e19e91a69ca
6
+ metadata.gz: 2f29524aa40533858f4589e9627363ff84ab9869ac385e88e12ae3edaf3f125bb87ad19c3a9356f4f109c30ce6f63abf19219914c1ced479af3d73a52b23f557
7
+ data.tar.gz: e715d84c64fa02f09fe5ac54e5136a8f0ae199ac1094a6e27f846cd82e25edd1aa2a1843500924cb80ff3c69915836c2ca9a8926977fb2178d2d83dbe3e6323e
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,18 @@
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
10
+
11
+ def load_extensions
12
+ ::Tramway::Extensions.load if defined? ::Tramway::Extensions
13
+ end
12
14
 
13
- def load_extensions
14
- ::Tramway::Extensions.load if defined? ::Tramway::Extensions
15
- end
16
- end
15
+ def model_class
16
+ params[:model].constantize
17
17
  end
18
18
  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
 
@@ -16,7 +16,7 @@ module Tramway::Core::Associations::ClassHelper
16
16
  end
17
17
 
18
18
  define_method "add_#{association_name}_form" do
19
- "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
20
20
  end
21
21
  end
22
22
 
@@ -25,22 +25,16 @@ module Tramway::Core::Associations::ClassHelper
25
25
  decorate_association association_name
26
26
  end
27
27
  end
28
+
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
@@ -6,10 +6,36 @@ module Tramway::Core::Associations::ObjectHelper
6
6
  object.send(association.name).class
7
7
  else
8
8
  unless association.options[:class_name]
9
- 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}'`")
10
- 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
+ )
11
14
  end
12
15
  association.options[:class_name]
13
16
  end
14
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
15
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,130 @@
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
+ attr_accessor :submit_message
9
+
10
+ def initialize(object = nil)
11
+ object ||= self.class.model_class.new
12
+ super(object).tap do
13
+ @@model_class = object.class
14
+ @@enumerized_attributes = object.class.try :enumerized_attributes
15
+ @@associations ||= []
16
+
17
+ self.class.full_class_name_associations&.each do |association, class_name|
18
+ define_association_method association, class_name
32
19
  end
33
- end
34
20
 
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
21
+ delegating object
55
22
  end
23
+ end
56
24
 
57
- def model_name
58
- @@model_class.model_name
25
+ def submit(params)
26
+ if params
27
+ validate(params) ? save : collecting_associations_errors
28
+ else
29
+ Tramway::Error.raise_error(:tramway, :core, :application_form, :submit, :params_should_not_be_nil)
59
30
  end
31
+ end
60
32
 
61
- def associations
62
- @@associations
63
- end
33
+ def model_name
34
+ @@model_class.model_name
35
+ end
64
36
 
65
- def form_properties(**args)
66
- @form_properties = args
67
- end
37
+ def associations
38
+ @@associations
39
+ end
68
40
 
69
- def form_properties_additional(**args)
70
- @form_properties_additional = args
71
- end
41
+ def to_model
42
+ self
43
+ end
72
44
 
73
- def properties
74
- return @form_properties if @form_properties
45
+ def persisted?
46
+ model.id.nil?
47
+ end
75
48
 
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
84
- end
49
+ class << self
50
+ include Tramway::Core::ApplicationForms::ConstantClassActions
85
51
 
86
- def build_errors; end
52
+ delegate :defined_enums, to: :model_class
87
53
 
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
93
- end
94
- end
54
+ def association(property)
55
+ properties property
56
+ @@associations = ((defined?(@@associations) && @@associations) || []) + [property]
95
57
  end
96
58
 
97
- def attributes
98
- properties.reduce({}) do |hash, property|
99
- hash.merge! property.first => model.values[property.first.to_s]
100
- end
59
+ def associations(*properties)
60
+ properties.each { |property| association property }
101
61
  end
102
62
 
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
111
-
112
- def associations(*properties)
113
- properties.each do |property|
114
- association property
115
- end
116
- end
63
+ def full_class_name_associations
64
+ @@associations&.reduce({}) do |hash, association|
65
+ options = @@model_class.reflect_on_all_associations(:belongs_to).select do |a|
66
+ a.name == association.to_sym
67
+ end.first&.options
117
68
 
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
69
+ if options&.dig(:polymorphic)
70
+ hash.merge! association => @@model_class.send("#{association}_type").values
71
+ elsif options
72
+ hash.merge!(association => (options[:class_name] || association.to_s.camelize).constantize)
133
73
  end
74
+ hash
134
75
  end
76
+ end
135
77
 
136
- def full_class_name_association(association_name)
137
- full_class_name_associations[association_name]
138
- end
78
+ def full_class_name_association(association_name)
79
+ full_class_name_associations[association_name]
80
+ end
139
81
 
140
- def enumerized_attributes
141
- @@enumerized_attributes
142
- end
82
+ def reflect_on_association(*args)
83
+ @@model_class.reflect_on_association(*args)
84
+ end
143
85
 
144
- def reflect_on_association(*args)
145
- @@model_class.reflect_on_association(*args)
146
- end
86
+ def enumerized_attributes
87
+ @@enumerized_attributes
88
+ end
147
89
 
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
90
+ def model_class
91
+ if defined?(@@model_class) && @@model_class
92
+ @@model_class
93
+ else
94
+ model_class_name ||= name.to_s.sub(/Form$/, '')
95
+ begin
96
+ @@model_class = model_class_name.constantize
97
+ rescue StandardError
98
+ Tramway::Error.raise_error :tramway, :core, :application_form, :model_class, :there_is_not_model_class,
99
+ name: name, model_class_name: model_class_name
159
100
  end
160
101
  end
102
+ end
161
103
 
162
- def model_class=(name)
163
- @@model_class = name
164
- end
104
+ def model_class=(name)
105
+ @@model_class = name
106
+ end
165
107
 
166
- def validation_group_class
167
- ActiveModel
108
+ def validates(attribute, **options)
109
+ if !defined?(@@model_class) || @@model_class.nil?
110
+ Tramway::Error.raise_error(:tramway, :core, :application_form, :validates, :you_need_to_set_model_class)
168
111
  end
112
+ @@model_class.validates attribute, **options
113
+ end
114
+ end
169
115
 
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
116
+ private
117
+
118
+ def collecting_associations_errors
119
+ @@associations.each do |association|
120
+ model.send("#{association}=", send(association)) if errors.details[association] == [{ error: :blank }]
177
121
  end
178
122
  end
123
+
124
+ def save
125
+ super
126
+ rescue StandardError => e
127
+ Tramway::Error.raise_error :tramway, :core, :application_form, :submit, :looks_like_you_have_method,
128
+ method_name: e.name.to_s.gsub('=', ''), model_class: @@model_class, class_name: self.class
129
+ end
179
130
  end