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.



18
19
20
# File 'lib/base_form/form.rb', line 18

def form_records
  @form_records
end

Class Method Details

.save(*params) ⇒ Object



29
30
31
# File 'lib/base_form/form.rb', line 29

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

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


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

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.



39
40
41
42
43
# File 'lib/base_form/form.rb', line 39

def save
  perform_in_transaction { persist } if valid?

  self
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/base_form/form.rb', line 45

def valid?
  errors.empty? && super
end