Module: Asset::Ownership::Owned

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Currently only plates can be owned.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/asset/ownership.rb', line 33

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

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

Instance Method Details

#change_owner_to(owner, source_event) ⇒ Object


48
49
50
51
52
53
54
# File 'app/models/asset/ownership.rb', line 48

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