Class: NiceForm::Base
- Inherits:
-
Object
- Object
- NiceForm::Base
- 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
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #persisted? ⇒ Boolean
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #with_context(attrs = {}) ⇒ Object
Methods included from PrimaryKey
Methods included from Instantiation
Methods included from ActsLike
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
#context ⇒ Object (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 |
.inspect ⇒ Object
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
#attributes ⇒ Object
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 |
#inspect ⇒ Object
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
82 83 84 |
# File 'lib/nice_form/base.rb', line 82 def persisted? to_param.present? end |
#to_key ⇒ Object
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_model ⇒ Object
67 68 69 |
# File 'lib/nice_form/base.rb', line 67 def to_model self end |
#to_param ⇒ Object
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 |