Class: Tramway::Core::ApplicationForm

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

Direct Known Subclasses

ExtendedApplicationForm

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil) ⇒ 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
22
23
24
25
# File 'app/forms/tramway/core/application_form.rb', line 3

def initialize(object = nil)
  object = self.class.model_class.new unless object
  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")
          super association_class.constantize.find value
        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



62
63
64
65
66
# File 'app/forms/tramway/core/application_form.rb', line 62

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

.associations(*properties) ⇒ Object



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

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

.enumerized_attributesObject



96
97
98
# File 'app/forms/tramway/core/application_form.rb', line 96

def enumerized_attributes
  @@enumerized_attributes
end

.full_class_name_association(association_name) ⇒ Object



92
93
94
# File 'app/forms/tramway/core/application_form.rb', line 92

def full_class_name_association(association_name)
  full_class_name_associations[association_name]
end

.full_class_name_associationsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/forms/tramway/core/application_form.rb', line 74

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
        class_name = options[:class_name] || association.to_s.camelize
        hash.merge!(association => class_name.constantize)
      end
    end
    hash
  end
end

.model_classObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/forms/tramway/core/application_form.rb', line 104

def model_class
  if defined?(@@model_class) && @@model_class
    @@model_class
  else
    model_class_name ||= self.name.to_s.sub(/Form$/, '')
    begin
      @@model_class = model_class_name.constantize
    rescue
      error = Tramway::Error.new(plugin: :core, method: :model_class, message: ("There is not model class name for #{self.name}. Should be #{model_class_name} or you can use another class to initialize form object or just initialize form with object."))
      raise error.message
    end
  end
end

.reflect_on_association(*args) ⇒ Object



100
101
102
# File 'app/forms/tramway/core/application_form.rb', line 100

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

.validation_group_classObject



118
119
120
# File 'app/forms/tramway/core/application_form.rb', line 118

def validation_group_class
  ActiveModel
end

Instance Method Details

#build_errorsObject



48
# File 'app/forms/tramway/core/application_form.rb', line 48

def build_errors; end

#delegating(object) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/forms/tramway/core/application_form.rb', line 50

def delegating(object)
  methods = [:to_key, :errors]
  methods.each do |method|
    self.class.send(:define_method, method) do
      object.send method
    end
  end
end

#form_properties(**args) ⇒ Object



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

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

#model_nameObject



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

def model_name
  @@model_class.model_name
end

#propertiesObject



44
45
46
# File 'app/forms/tramway/core/application_form.rb', line 44

def properties
  @form_properties || []
end

#submit(params) ⇒ Object



27
28
29
30
31
32
33
34
# File 'app/forms/tramway/core/application_form.rb', line 27

def submit(params)
  if params
    save if validate params
  else
    error = Tramway::Error.new(plugin: :core, method: :submit, message: ('ApplicationForm::Params should not be nil'))
    raise error.message
  end
end