Class: Formwandler::Form

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations::Callbacks
Defined in:
lib/formwandler/form.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models: {}, controller:) ⇒ Form

Returns a new instance of Form.



49
50
51
52
53
54
55
56
57
# File 'lib/formwandler/form.rb', line 49

def initialize(models: {}, controller:)
  @models = models.symbolize_keys
  @controller = controller

  initialize_fields
  initialize_models
  assign_defaults
  assign_params
end

Class Attribute Details

.model_save_orderObject (readonly)

Returns the value of attribute model_save_order.



44
45
46
# File 'lib/formwandler/form.rb', line 44

def model_save_order
  @model_save_order
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



47
48
49
# File 'lib/formwandler/form.rb', line 47

def controller
  @controller
end

#modelsObject (readonly)

Returns the value of attribute models.



47
48
49
# File 'lib/formwandler/form.rb', line 47

def models
  @models
end

Class Method Details

.attribute_accessor(name) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/formwandler/form.rb', line 30

def attribute_accessor(name)
  define_method "#{name}=" do |value|
    field(name).value = value
  end

  define_method name do
    field(name).value
  end
end

.field(name, opts = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/formwandler/form.rb', line 21

def field(name, opts = {}, &block)
  field_definition = FieldDefinition.new(name, opts)
  field_definitions[name] = field_definition

  field_definition.instance_exec(&block) if block_given?

  attribute_accessor(name)
end

.field_definitionsObject



17
18
19
# File 'lib/formwandler/form.rb', line 17

def field_definitions
  @field_definitions ||= {}
end

.model_nameObject



13
14
15
# File 'lib/formwandler/form.rb', line 13

def model_name
  ActiveModel::Name.new(self, nil, name.chomp('Form').underscore)
end

.save_order(*model_names) ⇒ Object



40
41
42
# File 'lib/formwandler/form.rb', line 40

def save_order(*model_names)
  @model_save_order = model_names
end

Instance Method Details

#field(name) ⇒ Object



67
68
69
# File 'lib/formwandler/form.rb', line 67

def field(name)
  @fields.fetch(name)
end

#fields(*names) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/formwandler/form.rb', line 71

def fields(*names)
  if names.any?
    @fields.fetch_values(*names)
  else
    @fields.values
  end
end

#fields_for_model(model) ⇒ Object



79
80
81
# File 'lib/formwandler/form.rb', line 79

def fields_for_model(model)
  fields.select { |field| field.model == model }
end

#models_valid?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/formwandler/form.rb', line 83

def models_valid?
  all_valid = true
  models.each do |name, model|
    all_valid = false if model.invalid?
    next if model.valid?
    fields_for_model(name).each do |field|
      model.errors[field.source].each do |error_message|
        errors.add(field.name, error_message)
      end
    end
  end
  all_valid
end

#persisted?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/formwandler/form.rb', line 59

def persisted?
  models.values.first&.persisted? || false
end

#submitObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/formwandler/form.rb', line 97

def submit
  if valid? && models_valid?
    ActiveRecord::Base.transaction do
      save_models!
    end
    load_results
  else
    false
  end
end

#to_paramObject



63
64
65
# File 'lib/formwandler/form.rb', line 63

def to_param
  models.values.first.to_param
end