Class: Tramway::Core::ApplicationForm

Inherits:
Reform::Form
  • Object
show all
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

ExtendedApplicationForm

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_messageObject

Returns the value of attribute submit_message.



8
9
10
# File 'app/forms/tramway/core/application_form.rb', line 8

def submit_message
  @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_attributesObject



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_associationsObject



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|
    options = @@model_class.reflect_on_all_associations(:belongs_to).select do |a|
      a.name == association.to_sym
    end.first&.options

    if options&.dig(:polymorphic)
      hash.merge! association => @@model_class.send("#{association}_type").values
    elsif options
      hash.merge!(association => (options[:class_name] || association.to_s.camelize).constantize)
    end
    hash
  end
end

.model_classObject



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, **options)
  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, **options
end

Instance Method Details

#associationsObject



37
38
39
# File 'app/forms/tramway/core/application_form.rb', line 37

def associations
  @@associations
end

#model_nameObject



33
34
35
# File 'app/forms/tramway/core/application_form.rb', line 33

def model_name
  @@model_class.model_name
end

#persisted?Boolean

Returns:

  • (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_modelObject



41
42
43
# File 'app/forms/tramway/core/application_form.rb', line 41

def to_model
  self
end