Module: Netfira::WebConnect::Model::Record::Relations

Included in:
Netfira::WebConnect::Model::Record
Defined in:
lib/netfira/web_connect/model/record/relations.rb

Overview

These methods help relate and unrelate records

Instance Method Summary collapse

Instance Method Details

#relate(record) ⇒ Object Also known as: <<



7
8
9
10
11
12
13
14
15
# File 'lib/netfira/web_connect/model/record/relations.rb', line 7

def relate(record)
  error_messages = {
      no_relation: 'You cannot add a %s to a %s',
      wrong_shop: 'Tried to add a record from shop %s to shop %s'
  }
  prepare_relation(record, error_messages) do |relation_class, attrs|
    relation_class.with_deleted.find_or_create_by(attrs).tap { |x| x.restore! if x.destroyed? }
  end
end

#unrelate(record) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/netfira/web_connect/model/record/relations.rb', line 19

def unrelate(record)
  error_messages = {
      no_relation: 'You cannot remove a %s from a %s',
      wrong_shop: 'Tried to remove a record of shop %s from shop %s'
  }
  prepare_relation(record, error_messages) do |relation_class, attrs|
    relation_class.with_deleted.find_by(attrs).tap { |x| x.destroy! if x }
  end
end