Class: BaseForm::Form

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/base_form/form.rb

Overview

This class is the main core functionality, by being an inheritable class, which controls the form attributes assignments, validations and persisting.

Basically you should create your own Form Object Class, and inherit this class (BaseForm::Form). After that you should put all records in the ‘@form_records` variable, through `use_form_records` class method. In your `persist` method implementation, just create the record objects associating it to each variable in `form_records.`

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.form_recordsObject (readonly)

Returns the value of attribute form_records.



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

def form_records
  @form_records
end

Class Method Details

.save(*params) ⇒ Object



27
28
29
# File 'lib/base_form/form.rb', line 27

def self.save(*params)
  new(*params).save
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @persisted
end

#saveObject

This method will make the things happen. It’ll try run validations set in your form class, and if it passes, it’ll run your persist instructions in a block of ActiveRecord transaction. If some record fails it’s persistence/validation, then a rollback will be raised, the form will return those errors grouped in it. Otherwise everything is commited and ‘persisted?` method will return true.



37
38
39
40
41
# File 'lib/base_form/form.rb', line 37

def save
  perform_in_transaction { persist } if valid?

  self
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/base_form/form.rb', line 43

def valid?
  errors.empty? && super
end