class ArDocStore::Attributes::AssignEmbedsManyAttributes

Attributes

assn_name[R]
class_name[R]
models[RW]
parent[R]
values[RW]

Public Class Methods

new(parent, class_name, assn_name, models, values) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 97
def initialize(parent, class_name, assn_name, models, values)
  @parent, @class_name, @assn_name, @models, @values = parent, class_name, assn_name, models, values
  @models ||= ArDocStore::EmbeddedCollection.new
  values.each { |value|
    value = value.symbolize_keys
    if value.key?(:id)
      process_existing_model(value)
    else
      next if value.values.all? { |value| value.nil? || value == '' }
      add(value)
    end
  }
end

Private Instance Methods

add(value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 127
def add(value)
  models << class_name.new(value)
end
destroy(model, value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 131
def destroy(model, value)
  wants_to_die?(value) && models.delete(model)
end
destroy_or_update(model, value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 123
def destroy_or_update(model, value)
  destroy(model, value) or update_attributes(model, value)
end
find_model_by_value(value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 143
def find_model_by_value(value)
  models.detect { |item| item.id == value[:id] }
end
process_existing_model(value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 116
def process_existing_model(value)
  return false unless value.key?(:id)

  model = find_model_by_value(value)
  model && destroy_or_update(model, value) or add(value)
end
update_attributes(model, value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 135
def update_attributes(model, value)
  model.attributes = value
end
wants_to_die?(value) click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 139
def wants_to_die?(value)
  value.key?(:_destroy) && ArDocStore.convert_boolean(value[:_destroy])
end