Module: Asset::Ownership::Owned

Included in:
Plate
Defined in:
app/models/asset/ownership.rb

Overview

rubocop:todo Style/Documentation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Currently only plates can be owned.


34
35
36
37
38
39
40
41
# File 'app/models/asset/ownership.rb', line 34

def self.included(base)
  base.class_eval do
    has_one :plate_owner
    has_one :owner, source: :user, through: :plate_owner

    scope :for_user, ->(user) { user.nil? ? all : joins(:plate_owner).where(plate_owners: { user_id: user }) }
  end
end

Instance Method Details

#change_owner_to(owner, source_event) ⇒ Object


43
44
45
46
47
48
49
# File 'app/models/asset/ownership.rb', line 43

def change_owner_to(owner, source_event)
  if plate_owner.nil?
    update!(plate_owner: PlateOwner.create!(user: owner, eventable: source_event, plate: self))
  else
    plate_owner.update!(user: owner, eventable: source_event)
  end
end