Class: Kadmin::Form

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Includes:
ActiveModel::Validations, ActiveRecord::AttributeAssignment
Defined in:
lib/kadmin/form.rb

Overview

Parsing is done by using attribute setters. If you have an attribute called name, then add a reader/writer for it, name and name=, and perform the parsing in name=. If there is no parsing to be done, you can simply delegate the method to the underlying model.

If the attribute is a nested form, in the writer, simply instantiate that form, and pass the attributes on to it, then update the model's association (if any) to reflect the changes.

Validation is performed like on a normal model or ActiveRecord object. If you have no extra validation to perform than that of the model, simply delegate the validate and valid? methods to the model.

To use nested forms, you need to add a reader and a writer. For example, for a form called Person, with potentially X nested Person forms as children, you would have:

Examples:

class PersonForm < Form
  def children
    [@child1, @child2]
  end

  def children_attributes=(attributes)
    ...instantiate subforms and pass attributes...
  end
end

Instance Attribute Summary collapse

Attributes assignment/manipulation collapse

Persistence collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Form



50
51
52
53
54
# File 'lib/kadmin/form.rb', line 50

def initialize(model)
  @errors = ActiveModel::Errors.new(self)
  @model = model
  @form_input = {}
end

Instance Attribute Details

#modelActiveModel::Model (readonly)



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

def model
  @model
end

Class Method Details

.delegate_association(association, to:) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/kadmin/form.rb', line 101

def delegate_association(association, to:)
  self.associations[association] = to

  # add a reader attribute

  class_eval "    def \#{association}\n      return self.associated_forms['\#{association}']\n    end\n  METHOD\nend\n", __FILE__, __LINE__ + 1

.delegate_attributes(*attributes) ⇒ Object

Delegates the list of attributes to the model, both readers and writers. If the attribute value passed is a hash and not a symbol, assumes it is a hash of one key, whose value is an array contained :reader, :writer, or both.

Examples:

delegate_attributes :first_name, { last_name: [:reader] }


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

def delegate_attributes(*attributes)
  delegates = attributes.each_with_object([]) do |attribute, acc|
    case attribute
    when Hash
      key, value = attribute.first
      acc << key if value.include?(:reader)
      acc << "#{key}=" if value.include?(:writer)
    when Symbol, String
      acc.push(attribute, "#{attribute}=")
    else
      raise(ArgumentError, 'Attribute must be one of: Hash, Symbol, String')
    end
  end

  delegate(*delegates, to: :model)
end

Instance Method Details

#associated_formsObject



113
114
115
116
117
118
119
120
# File 'lib/kadmin/form.rb', line 113

def associated_forms
  return @associated_forms ||= begin
    self.class.associations.map do |name, form_class_name|
      form_class = form_class_name.constantize
      form_class.new(@model.public_send(name))
    end
  end
end

#sanitize_for_mass_assignment(attributes) ⇒ Object

For now, we overload the method to accept all attributes. This is removed in Rails 5, so once we upgrade we can remove the overload.



69
70
71
# File 'lib/kadmin/form.rb', line 69

def sanitize_for_mass_assignment(attributes)
  return attributes
end

#saveObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/kadmin/form.rb', line 151

def save
  saved = false
  @model.class.transaction do
    saved = @model.save
    self.associated_forms.each do |_name, form|
      saved &&= form.save
    end

    raise ActiveRecord::Rollback unless saved
  end

  return saved
end

#save!Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kadmin/form.rb', line 165

def save!
  saved = false
  @model.class.transaction do
    saved = @model.save!
    self.associated_forms.each do |_name, form|
      saved &&= form.save! # no need to raise anything, save! will do so

    end
  end

  return saved
end

#to_modelObject



56
57
58
# File 'lib/kadmin/form.rb', line 56

def to_model
  return @model
end