Class: NiceForm::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Attributes, ActiveModel::Callbacks, ActiveModel::Conversion, ActiveModel::Validations, ActsLike, Instantiation, PrimaryKey
Defined in:
lib/nice_form/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PrimaryKey

included

Methods included from Instantiation

included

Methods included from ActsLike

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



33
34
35
36
# File 'lib/nice_form/base.rb', line 33

def initialize(attributes = {})
  assign_attributes(attributes) if attributes
  super()
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



26
27
28
# File 'lib/nice_form/base.rb', line 26

def context
  @context
end

Class Method Details

.inherited(klass) ⇒ Object



28
29
30
31
# File 'lib/nice_form/base.rb', line 28

def self.inherited(klass)
  super
  klass.primary_key(*NiceForm.config.primary_key)
end

.inspectObject



38
39
40
41
# File 'lib/nice_form/base.rb', line 38

def self.inspect
  attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ", "
  "#{self.name}(#{attr_list})"
end

Instance Method Details

#attributesObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nice_form/base.rb', line 55

def attributes
  attrs = [self.class.form_primary_key]

  attrs << if ::NiceForm.config.primary_key.is_a?(Array)
             ::NiceForm.config.primary_key[0].to_s
           else
             ::NiceForm::Config.primary_key.to_s
           end

  super.except(*attrs)
end

#inspectObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nice_form/base.rb', line 43

def inspect
  inspection = if defined?(@attributes) && @attributes
                 self.class.attribute_names.filter_map do |name|
                   "#{name}: #{attribute_for_inspect(name)}" if _has_attribute?(name)
                 end.join(", ")
               else
                 "not initialized"
               end

  "#<#{self.class} #{inspection}>"
end

#persisted?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/nice_form/base.rb', line 82

def persisted?
  to_param.present?
end

#to_keyObject



75
76
77
78
79
80
# File 'lib/nice_form/base.rb', line 75

def to_key
  key = attribute(self.class.form_primary_key)
  return nil if key.nil?

  [key]
end

#to_modelObject



67
68
69
# File 'lib/nice_form/base.rb', line 67

def to_model
  self
end

#to_paramObject



71
72
73
# File 'lib/nice_form/base.rb', line 71

def to_param
  attribute(self.class.form_primary_key)
end

#with_context(attrs = {}) ⇒ Object



86
87
88
# File 'lib/nice_form/base.rb', line 86

def with_context(attrs = {})
  @context = attrs
end