Class: ActiveModel::Form

Inherits:
Object
  • Object
show all
Extended by:
Naming
Includes:
ActiveModel, Conversion, Validations
Defined in:
lib/active_model/form.rb,
lib/active_model/form/version.rb,
lib/active_model/form/attributes.rb

Defined Under Namespace

Modules: BooleanAttribute, DateAttribute, DateTimeAttribute, FileAttribute, FloatAttribute, IntegerAttribute, StringAttribute

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Form

Returns a new instance of Form.


13
14
15
# File 'lib/active_model/form.rb', line 13

def initialize(attributes=nil)
  self.assign_attributes(attributes)
end

Class Method Details

.attribute(name, type_name) ⇒ Object


27
28
29
30
31
32
# File 'lib/active_model/form.rb', line 27

def self.attribute(name, type_name)
  # TODO: Make inheritance of ActiveModel::Forms do the right thing with attributes
  @_attributes ||= {}
  @_attributes[name.to_s] = self.type_from_type_name(type_name)
  self.send(:attr_accessor, name)
end

.attributesObject


34
35
36
# File 'lib/active_model/form.rb', line 34

def self.attributes
  @_attributes ||= {}
end

.model_name=(name) ⇒ Object

Set the model name to change the field names generated by the Rails form helpers


23
24
25
# File 'lib/active_model/form.rb', line 23

def self.model_name=(name)
  @_model_name = ActiveModel::Name.new(self, nil, name)
end

Instance Method Details

#assign_attributes(new_attributes) ⇒ Object


47
48
49
50
51
52
53
54
55
# File 'lib/active_model/form.rb', line 47

def assign_attributes(new_attributes)
  return if new_attributes.blank?
  new_attributes = self.clean_attributes(new_attributes)
  new_attributes.each do |k, v|
    if attribute = self.class.attributes[k].presence
      send("#{k}=", attribute.parse(v))
    end
  end
end

#column_for_attribute(name) ⇒ Object

Returns the column object for the named attribute.


39
40
41
# File 'lib/active_model/form.rb', line 39

def column_for_attribute(name)
  self.class.attributes[name.to_s]
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)

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

def has_attribute?(name)
  self.class.attributes[name.to_s].present?
end

#persisted?Boolean

For the Rails route helpers

Returns:

  • (Boolean)

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

def persisted?
  false
end