Class: Tramway::Core::ApplicationForm
- Inherits:
-
Reform::Form
- Object
- Reform::Form
- Tramway::Core::ApplicationForm
- Defined in:
- app/forms/tramway/core/application_form.rb
Direct Known Subclasses
Class Method Summary collapse
- .association(property) ⇒ Object
- .associations(*properties) ⇒ Object
- .enumerized_attributes ⇒ Object
- .full_class_name_association(association_name) ⇒ Object
- .full_class_name_associations ⇒ Object
- .model_class ⇒ Object
- .model_class=(name) ⇒ Object
- .reflect_on_association(*args) ⇒ Object
- .validates(attribute, **options) ⇒ Object
- .validation_group_class ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #build_errors ⇒ Object
- #delegating(object) ⇒ Object
- #form_properties(**args) ⇒ Object
- #form_properties_additional(**args) ⇒ Object
-
#initialize(object = nil) ⇒ ApplicationForm
constructor
A new instance of ApplicationForm.
- #model_name ⇒ Object
- #properties ⇒ Object
- #submit(params) ⇒ Object
Constructor Details
#initialize(object = nil) ⇒ ApplicationForm
Returns a new instance of ApplicationForm.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/forms/tramway/core/application_form.rb', line 5 def initialize(object = nil) object ||= self.class.model_class.new super(object).tap do @@model_class = object.class @@enumerized_attributes = object.class.try :enumerized_attributes @@associations ||= [] self.class.full_class_name_associations.each do |association, class_name| if class_name.is_a? Array self.class.send(:define_method, "#{association}=") do |value| association_class = send("#{association}_type") if association_class.nil? 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`') else super association_class.constantize.find value end end else self.class.send(:define_method, "#{association}=") do |value| super class_name.find value end end end delegating object end end |
Class Method Details
.association(property) ⇒ Object
109 110 111 112 113 |
# File 'app/forms/tramway/core/application_form.rb', line 109 def association(property) properties property @@associations ||= [] @@associations << property end |
.associations(*properties) ⇒ Object
115 116 117 118 119 |
# File 'app/forms/tramway/core/application_form.rb', line 115 def associations(*properties) properties.each do |property| association property end end |
.enumerized_attributes ⇒ Object
143 144 145 |
# File 'app/forms/tramway/core/application_form.rb', line 143 def enumerized_attributes @@enumerized_attributes end |
.full_class_name_association(association_name) ⇒ Object
139 140 141 |
# File 'app/forms/tramway/core/application_form.rb', line 139 def full_class_name_association(association_name) full_class_name_associations[association_name] end |
.full_class_name_associations ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/forms/tramway/core/application_form.rb', line 121 def full_class_name_associations @@associations&.reduce({}) do |hash, association| = @@model_class.reflect_on_all_associations(:belongs_to).select do |a| a.name == association.to_sym end.first&. if if [:polymorphic] hash.merge! association => @@model_class.send("#{association}_type").values else class_name = [:class_name] || association.to_s.camelize hash.merge!(association => class_name.constantize) end end hash end end |
.model_class ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/forms/tramway/core/application_form.rb', line 151 def model_class if defined?(@@model_class) && @@model_class @@model_class else model_class_name ||= name.to_s.sub(/Form$/, '') begin @@model_class = model_class_name.constantize rescue StandardError 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.") raise error. end end end |
.model_class=(name) ⇒ Object
165 166 167 |
# File 'app/forms/tramway/core/application_form.rb', line 165 def model_class=(name) @@model_class = name end |
.reflect_on_association(*args) ⇒ Object
147 148 149 |
# File 'app/forms/tramway/core/application_form.rb', line 147 def reflect_on_association(*args) @@model_class.reflect_on_association(*args) end |
.validates(attribute, **options) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'app/forms/tramway/core/application_form.rb', line 173 def validates(attribute, **) if !defined?(@@model_class) || @@model_class.nil? 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.') raise error. end @@model_class.validates attribute, ** # @@validations ||= {} # @@validations.deep_merge! attribute => options end |
.validation_group_class ⇒ Object
169 170 171 |
# File 'app/forms/tramway/core/application_form.rb', line 169 def validation_group_class ActiveModel end |
Instance Method Details
#attributes ⇒ Object
100 101 102 103 104 |
# File 'app/forms/tramway/core/application_form.rb', line 100 def attributes properties.reduce({}) do |hash, property| hash.merge! property => send(property.first) end end |
#build_errors ⇒ Object
89 |
# File 'app/forms/tramway/core/application_form.rb', line 89 def build_errors; end |
#delegating(object) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'app/forms/tramway/core/application_form.rb', line 91 def delegating(object) methods = %i[to_key errors] methods.each do |method| self.class.send(:define_method, method) do object.send method end end end |
#form_properties(**args) ⇒ Object
68 69 70 |
# File 'app/forms/tramway/core/application_form.rb', line 68 def form_properties(**args) @form_properties = args end |
#form_properties_additional(**args) ⇒ Object
72 73 74 |
# File 'app/forms/tramway/core/application_form.rb', line 72 def form_properties_additional(**args) @form_properties_additional = args end |
#model_name ⇒ Object
64 65 66 |
# File 'app/forms/tramway/core/application_form.rb', line 64 def model_name @@model_class.model_name end |
#properties ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/forms/tramway/core/application_form.rb', line 76 def properties return @form_properties if @form_properties yaml_config_file_path = Rails.root.join('app', 'forms', "#{self.class.name.underscore}.yml") if File.exist? yaml_config_file_path @form_properties = YAML.load_file(yaml_config_file_path).deep_symbolize_keys @form_properties.deep_merge! @form_properties_additional if @form_properties_additional @form_properties else [] end end |
#submit(params) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/forms/tramway/core/application_form.rb', line 33 def submit(params) if params if validate params begin save.tap do # self.class.remove_validations_from_model! end rescue StandardError => e # self.class.remove_validations_from_model! 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}") raise error. end else association_error = false @@associations.each do |association| if errors.details[association] == [{ error: :blank }] model.send("#{association}=", send(association)) association_error = true end end (association_error && save).tap do # self.class.remove_validations_from_model! end end else # self.class.remove_validations_from_model! error = Tramway::Error.new(plugin: :core, method: :submit, message: 'ApplicationForm::Params should not be nil') raise error. end end |