Class: Tramway::Core::ApplicationForm
- Inherits:
-
Reform::Form
- Object
- Reform::Form
- Tramway::Core::ApplicationForm
- Extended by:
- Tramway::Core::ApplicationForms::ConstantClassActions
- Includes:
- Tramway::Core::ApplicationForms::AssociationObjectHelpers, Tramway::Core::ApplicationForms::ConstantObjectActions, Tramway::Core::ApplicationForms::PropertiesObjectHelper
- Defined in:
- app/forms/tramway/core/application_form.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#submit_message ⇒ Object
Returns the value of attribute submit_message.
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
Instance Method Summary collapse
- #associations ⇒ Object
-
#initialize(object = nil) ⇒ ApplicationForm
constructor
A new instance of ApplicationForm.
- #model_name ⇒ Object
- #persisted? ⇒ Boolean
- #submit(params) ⇒ Object
- #to_model ⇒ Object
Constructor Details
#initialize(object = nil) ⇒ ApplicationForm
Returns a new instance of ApplicationForm.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/forms/tramway/core/application_form.rb', line 10 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| define_association_method association, class_name end delegating object end end |
Instance Attribute Details
#submit_message ⇒ Object
Returns the value of attribute submit_message.
8 9 10 |
# File 'app/forms/tramway/core/application_form.rb', line 8 def @submit_message end |
Class Method Details
.association(property) ⇒ Object
54 55 56 57 |
# File 'app/forms/tramway/core/application_form.rb', line 54 def association(property) properties property @@associations = ((defined?(@@associations) && @@associations) || []) + [property] end |
.associations(*properties) ⇒ Object
59 60 61 |
# File 'app/forms/tramway/core/application_form.rb', line 59 def associations(*properties) properties.each { |property| association property } end |
.enumerized_attributes ⇒ Object
86 87 88 |
# File 'app/forms/tramway/core/application_form.rb', line 86 def enumerized_attributes @@enumerized_attributes end |
.full_class_name_association(association_name) ⇒ Object
78 79 80 |
# File 'app/forms/tramway/core/application_form.rb', line 78 def full_class_name_association(association_name) full_class_name_associations[association_name] end |
.full_class_name_associations ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/forms/tramway/core/application_form.rb', line 63 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 &.dig(:polymorphic) hash.merge! association => @@model_class.send("#{association}_type").values elsif hash.merge!(association => ([:class_name] || association.to_s.camelize).constantize) end hash end end |
.model_class ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/forms/tramway/core/application_form.rb', line 90 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 Tramway::Error.raise_error :tramway, :core, :application_form, :model_class, :there_is_not_model_class, name: name, model_class_name: model_class_name end end end |
.model_class=(name) ⇒ Object
104 105 106 |
# File 'app/forms/tramway/core/application_form.rb', line 104 def model_class=(name) @@model_class = name end |
.reflect_on_association(*args) ⇒ Object
82 83 84 |
# File 'app/forms/tramway/core/application_form.rb', line 82 def reflect_on_association(*args) @@model_class.reflect_on_association(*args) end |
.validates(attribute, **options) ⇒ Object
108 109 110 111 112 113 |
# File 'app/forms/tramway/core/application_form.rb', line 108 def validates(attribute, **) if !defined?(@@model_class) || @@model_class.nil? Tramway::Error.raise_error(:tramway, :core, :application_form, :validates, :you_need_to_set_model_class) end @@model_class.validates attribute, ** end |
Instance Method Details
#associations ⇒ Object
37 38 39 |
# File 'app/forms/tramway/core/application_form.rb', line 37 def associations @@associations end |
#model_name ⇒ Object
33 34 35 |
# File 'app/forms/tramway/core/application_form.rb', line 33 def model_name @@model_class.model_name end |
#persisted? ⇒ Boolean
45 46 47 |
# File 'app/forms/tramway/core/application_form.rb', line 45 def persisted? model.id.nil? end |
#submit(params) ⇒ Object
25 26 27 28 29 30 31 |
# File 'app/forms/tramway/core/application_form.rb', line 25 def submit(params) if params validate(params) ? save : collecting_associations_errors else Tramway::Error.raise_error(:tramway, :core, :application_form, :submit, :params_should_not_be_nil) end end |
#to_model ⇒ Object
41 42 43 |
# File 'app/forms/tramway/core/application_form.rb', line 41 def to_model self end |