Class: FormObj::Form

Inherits:
TreeStruct
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/form_obj/form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForm

Returns a new instance of Form.



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

def initialize()
  @errors = ActiveModel::Errors.new(self)
  @persisted = false
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



35
36
37
# File 'lib/form_obj/form.rb', line 35

def errors
  @errors
end

#persistedObject

Returns the value of attribute persisted.



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

def persisted
  @persisted
end

Class Method Details

.array_classObject



22
23
24
# File 'lib/form_obj/form.rb', line 22

def self.array_class
  Array
end

.attribute_classObject



30
31
32
# File 'lib/form_obj/form.rb', line 30

def self.attribute_class
  Attribute
end

.model_nameObject



44
45
46
# File 'lib/form_obj/form.rb', line 44

def self.model_name
  @model_name || super
end

.nested_classObject



26
27
28
# File 'lib/form_obj/form.rb', line 26

def self.nested_class
  ::TreeStruct
end

Instance Method Details

#_set_attribute_value(attribute, value) ⇒ Object



57
58
59
60
# File 'lib/form_obj/form.rb', line 57

def _set_attribute_value(attribute, value)
  @persisted = false
  super
end

#persisted?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/form_obj/form.rb', line 53

def persisted?
  @persisted
end

#primary_keyObject



62
63
64
# File 'lib/form_obj/form.rb', line 62

def primary_key
  send(self.class.primary_key)
end

#primary_key=(val) ⇒ Object



66
67
68
# File 'lib/form_obj/form.rb', line 66

def primary_key=(val)
  send("#{self.class.primary_key}=", val)
end

#savedObject



87
88
89
90
# File 'lib/form_obj/form.rb', line 87

def saved
  @persisted = true
  self
end

#update_attributes(new_attrs, raise_if_not_found: true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/form_obj/form.rb', line 70

def update_attributes(new_attrs, raise_if_not_found: true)
  new_attrs.each_pair do |new_attr, new_val|
    attr = self.class._attributes.find(new_attr)
    if attr.nil?
      raise UnknownAttributeError.new(new_attr) if raise_if_not_found
    else
      if attr.subform?
        self.send(new_attr).update_attributes(new_val)
      else
        self.send("#{new_attr}=", new_val)
      end
    end
  end

  self
end