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: {}, current_user: nil) ⇒ Form

Returns a new instance of Form.



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

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

  initialize_fields
  initialize_models
  assign_defaults
end

Class Attribute Details

.model_save_orderObject (readonly)

Returns the value of attribute model_save_order.



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

def model_save_order
  @model_save_order
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



49
50
51
# File 'lib/formwandler/form.rb', line 49

def current_user
  @current_user
end

#modelsObject (readonly)

Returns the value of attribute models.



49
50
51
# File 'lib/formwandler/form.rb', line 49

def models
  @models
end

Class Method Details

.attribute_accessor(name, field_definition) ⇒ Object



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

def attribute_accessor(name, field_definition)
  define_method "#{name}=" do |value|
    models[field_definition.model].send("#{field_definition.source}=", value)
  end

  define_method name do
    models[field_definition.model].send(field_definition.source)
  end
end

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

Yields:

  • ()


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

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

  yield(field_definitions[name]) if block_given?

  if field_definitions[name].model.present?
    attribute_accessor(name, field_definitions[name])
  else
    attr_accessor name
  end
end

.field_definitionsObject



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

def field_definitions
  @field_definitions ||= {}
end

.model_nameObject



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

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

.save_order(*model_names) ⇒ Object



42
43
44
# File 'lib/formwandler/form.rb', line 42

def save_order(*model_names)
  @model_save_order = model_names
end

Instance Method Details

#field(name) ⇒ Object



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

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

#fields(*names) ⇒ Object



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

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

#fields_for_model(model) ⇒ Object



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

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

#models_valid?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#submit(params) ⇒ Object



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

def submit(params)
  assign_attributes permitted_params(params)

  if valid? && models_valid?
    save_models!
    load_results
  else
    false
  end
end

#to_paramObject



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

def to_param
  models.values.first.to_param
end