Module: ActiveForm::Form

Defined in:
lib/activeform-rails/unpersistent_model.rb,
lib/activeform-rails/form.rb

Overview

UnpersistentModel allows the ActiveForm::Form object to delegate methods to this object with expected results

  • #model_name is used by Form helpers & Rails frequently to define the form namespace

  • #to_key, #to_param, #id should aways return nil as the Form cannot be persisted unless backed by ActiveModel

  • #persisted? should aways return false as the Form cannot be persisted unless backed by ActiveModel

Defined Under Namespace

Modules: ClassMethods Classes: UnpersistentModel

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/activeform-rails/form.rb', line 2

def self.included(base)
  base.class_eval do
    extend ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Validations
  end
  base.extend ClassMethods
end

Instance Method Details

#fill_attributes(params) ⇒ Object



78
79
80
# File 'lib/activeform-rails/form.rb', line 78

def fill_attributes(params)
  assign_from_hash(params)
end

#initialize(attributes = {}) ⇒ Object



74
75
76
# File 'lib/activeform-rails/form.rb', line 74

def initialize(attributes = {})
  assign_from_hash(attributes)
end

#main_modelObject



96
97
98
99
100
101
102
# File 'lib/activeform-rails/form.rb', line 96

def main_model
  if self.class.main_model.kind_of?(Symbol)
    send(self.class.main_model)
  else
    self.class.main_model
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/activeform-rails/form.rb', line 104

def new_record?
  !persisted?
end

#save(&block) ⇒ Object



82
83
84
85
86
87
# File 'lib/activeform-rails/form.rb', line 82

def save(&block)
  ensure_persistable
  valid?.tap do
    call_action_or_block(:save, &block)
  end
end

#save!(&block) ⇒ Object



89
90
91
92
93
94
# File 'lib/activeform-rails/form.rb', line 89

def save!(&block)
  ensure_persistable
  ActiveRecord::Base.transaction do
    call_action_or_block(:save!, &block)
  end
end