Class: Tramway::Core::ApplicationForm

Inherits:
Reform::Form
  • Object
show all
Defined in:
app/forms/tramway/core/application_form.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ApplicationForm

Returns a new instance of ApplicationForm.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/forms/tramway/core/application_form.rb', line 3

def initialize(object)
  super(object).tap do
    @@model_class = object.class
    @@enumerized_attributes = object.class.enumerized_attributes

    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")
          super association_class.constantize.find value
        end
      else
        self.class.send(:define_method, "#{association}=") do |value|
          super class_name.find value
        end
      end
    end
  end
end

Class Method Details

.association(property) ⇒ Object



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

def association(property)
  properties property
  @@associations ||= []
  @@associations << property
end

.associations(*properties) ⇒ Object



45
46
47
48
49
# File 'app/forms/tramway/core/application_form.rb', line 45

def associations(*properties)
  properties.each do |property|
    association property
  end
end

.enumerized_attributesObject



72
73
74
# File 'app/forms/tramway/core/application_form.rb', line 72

def enumerized_attributes
  @@enumerized_attributes
end

.full_class_name_association(association_name) ⇒ Object



68
69
70
# File 'app/forms/tramway/core/application_form.rb', line 68

def full_class_name_association(association_name)
  full_class_name_associations[association_name]
end

.full_class_name_associationsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/forms/tramway/core/application_form.rb', line 51

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
      if options[:polymorphic]
        hash.merge! association => @@model_class.send("#{association}_type").values
      else
        hash.merge!(association => options[:class_name].constantize)
      end
    end
    hash
  end
end

.model_classObject



80
81
82
# File 'app/forms/tramway/core/application_form.rb', line 80

def model_class
  @@model_class ||= self.name.to_s.sub(/Form$/, '')
end

.reflect_on_association(*args) ⇒ Object



76
77
78
# File 'app/forms/tramway/core/application_form.rb', line 76

def reflect_on_association(*args)
  @@model_class.reflect_on_association(*args)
end

Instance Method Details

#form_properties(**args) ⇒ Object



28
29
30
# File 'app/forms/tramway/core/application_form.rb', line 28

def form_properties(**args)
  @form_properties = args
end

#propertiesObject



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

def properties
  @form_properties || []
end

#submit(params) ⇒ Object



23
24
25
26
# File 'app/forms/tramway/core/application_form.rb', line 23

def submit(params)
  raise 'ApplicationForm::Params should not be nil'.inspect unless params
  save if validate params
end