Module: Granite::Form::Model::Attributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/granite/form/model/attributes.rb,
lib/granite/form/model/attributes/base.rb,
lib/granite/form/model/attributes/attribute.rb,
lib/granite/form/model/attributes/represents.rb,
lib/granite/form/model/attributes/reference_one.rb,
lib/granite/form/model/attributes/reference_many.rb,
lib/granite/form/model/attributes/reflections/base.rb,
lib/granite/form/model/attributes/reflections/attribute.rb,
lib/granite/form/model/attributes/reflections/collection.rb,
lib/granite/form/model/attributes/reflections/dictionary.rb,
lib/granite/form/model/attributes/reflections/represents.rb,
lib/granite/form/model/attributes/reflections/reference_one.rb,
lib/granite/form/model/attributes/reflections/reference_many.rb,
lib/granite/form/model/attributes/reflections/base/build_type_definition.rb,
lib/granite/form/model/attributes/reflections/collection/build_type_definition.rb,
lib/granite/form/model/attributes/reflections/dictionary/build_type_definition.rb,
lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb

Defined Under Namespace

Modules: ClassMethods, Reflections Classes: Attribute, Base, ReferenceMany, ReferenceOne, Represents

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



125
126
127
# File 'lib/granite/form/model/attributes.rb', line 125

def ==(other)
  super || (other.instance_of?(self.class) && other.attributes(false) == attributes(false))
end

#assign_attributes(attrs) ⇒ Object Also known as: attributes=



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/granite/form/model/attributes.rb', line 174

def assign_attributes(attrs)
  attrs.each do |name, value|
    name = name.to_s
    sanitize_value = self.class._sanitize && name == self.class.primary_name

    if respond_to?("#{name}=") && !sanitize_value
      public_send("#{name}=", value)
    else
      attribute_type = sanitize_value ? 'primary' : 'undefined'
      logger.debug("Ignoring #{attribute_type} `#{name}` attribute value for #{self} during mass-assignment")
    end
  end
end

#attribute(name) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/granite/form/model/attributes.rb', line 131

def attribute(name)
  reflection = self.class.reflect_on_attribute(name)
  return unless reflection

  initial_value = @initial_attributes.to_h.fetch(reflection.name, Granite::Form::UNDEFINED)
  @_attributes ||= {}
  @_attributes[reflection.name] ||= reflection.build_attribute(self, initial_value)
end

#attribute_came_from_user?(name) ⇒ Boolean

Returns:



156
157
158
# File 'lib/granite/form/model/attributes.rb', line 156

def attribute_came_from_user?(name)
  attribute(name).came_from_user?
end

#attribute_present?(name) ⇒ Boolean

Returns:



160
161
162
# File 'lib/granite/form/model/attributes.rb', line 160

def attribute_present?(name)
  attribute(name).value_present?
end

#attributes(include_associations = true) ⇒ Object



164
165
166
# File 'lib/granite/form/model/attributes.rb', line 164

def attributes(include_associations = true)
  Hash[attribute_names(include_associations).map { |name| [name, read_attribute(name)] }]
end

#initialize(attrs = {}) ⇒ Object



121
122
123
# File 'lib/granite/form/model/attributes.rb', line 121

def initialize(attrs = {})
  assign_attributes attrs
end

#initialize_copy(_) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/granite/form/model/attributes.rb', line 201

def initialize_copy(_)
  @initial_attributes = Hash[attribute_names.map do |name|
    [name, read_attribute_before_type_cast(name)]
  end]
  @_attributes = nil
  super
end

#inspectObject



197
198
199
# File 'lib/granite/form/model/attributes.rb', line 197

def inspect
  "#<#{self.class.send(:original_inspect)} #{attributes_for_inspect.presence || '(no attributes)'}>"
end

#read_attribute(name) ⇒ Object Also known as: []



146
147
148
# File 'lib/granite/form/model/attributes.rb', line 146

def read_attribute(name)
  attribute(name).read
end

#read_attribute_before_type_cast(name) ⇒ Object



152
153
154
# File 'lib/granite/form/model/attributes.rb', line 152

def read_attribute_before_type_cast(name)
  attribute(name).read_before_type_cast
end

#sync_attributesObject



190
191
192
193
194
195
# File 'lib/granite/form/model/attributes.rb', line 190

def sync_attributes
  attribute_names.each do |name|
    attr = attribute(name)
    attr.try(:sync) if attr.try(:changed?)
  end
end

#update(attrs) ⇒ Object Also known as: update_attributes



168
169
170
# File 'lib/granite/form/model/attributes.rb', line 168

def update(attrs)
  assign_attributes(attrs)
end

#write_attribute(name, value) ⇒ Object Also known as: []=



140
141
142
# File 'lib/granite/form/model/attributes.rb', line 140

def write_attribute(name, value)
  attribute(name).write(value)
end