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?



128
129
130
# File 'lib/granite/form/model/attributes.rb', line 128

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

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



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/granite/form/model/attributes.rb', line 177

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'
      report_unknown_attribute(attribute_type, name)
    end
  end
end

#attribute(name) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/granite/form/model/attributes.rb', line 134

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:



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

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

#attribute_present?(name) ⇒ Boolean

Returns:



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

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

#attributes(include_associations = true) ⇒ Object



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

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

#initialize(attrs = {}) ⇒ Object



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

def initialize(attrs = {})
  assign_attributes attrs
end

#initialize_copy(_) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/granite/form/model/attributes.rb', line 204

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

#inspectObject



200
201
202
# File 'lib/granite/form/model/attributes.rb', line 200

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

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



149
150
151
# File 'lib/granite/form/model/attributes.rb', line 149

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

#read_attribute_before_type_cast(name) ⇒ Object



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

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

#sync_attributesObject



193
194
195
196
197
198
# File 'lib/granite/form/model/attributes.rb', line 193

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



171
172
173
# File 'lib/granite/form/model/attributes.rb', line 171

def update(attrs)
  assign_attributes(attrs)
end

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



143
144
145
# File 'lib/granite/form/model/attributes.rb', line 143

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