Module: Hickey::DomainDetector::Scopes

Defined in:
lib/hickey/domain_detector/scopes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/hickey/domain_detector/scopes.rb', line 4

def self.included(base)
  base.send(:alias_method_chain, :visit_hash, :scopes)
  base.send(:alias_method_chain, :new_instance, :scopes)
end

Instance Method Details

#new_instance_with_scopes(class_or_instance, record) ⇒ Object



18
19
20
21
22
# File 'lib/hickey/domain_detector/scopes.rb', line 18

def new_instance_with_scopes(class_or_instance, record)
  returning new_instance_without_scopes(class_or_instance, record) do |instance|
    scopes[instance.class.name.foreign_key] = instance
  end
end

#visit_hash_with_scopes(attribute, record) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/hickey/domain_detector/scopes.rb', line 9

def visit_hash_with_scopes(attribute, record)
  scopes.each do |foreign_key, owner|
    record[foreign_key] = owner.id unless record[foreign_key]
  end
  returning visit_hash_without_scopes(attribute, record) do |instance|
    scopes.delete instance.class.name.foreign_key
  end
end