Class: Gamification::Inventory

Inherits:
ApplicationRecord show all
Defined in:
app/models/gamification/inventory.rb

Instance Method Summary collapse

Instance Method Details

#equip_item(user_id) ⇒ Object



32
33
34
35
36
37
# File 'app/models/gamification/inventory.rb', line 32

def equip_item(user_id)
  unnequip_same_type_item(user_id)
  unnequip_same_associated_jump_item(user_id)
  equipe_associated_jump_item(user_id)
  self.update_attributes(:equipped => true)
end

#equipe_associated_jump_item(user_id) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/models/gamification/inventory.rb', line 22

def equipe_associated_jump_item(user_id)
  if self.item.item_group
    inventory = Inventory.where.not(id: self.id).where(user_id: user_id,gamification_item_groups: {id: self.item.item_group.id}).where(gamification_item_types: {key: self.item.item_type.key + "jump"})
                    .joins(item: :item_group).joins(item: :item_type)
    if inventory.any?
      inventory.update_all(:equipped => true)
    end
  end
end

#unnequip_same_associated_jump_item(user_id) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'app/models/gamification/inventory.rb', line 12

def unnequip_same_associated_jump_item(user_id)
  if self.item.item_type
    unequip_inventory = Inventory.where(user_id: user_id,gamification_item_types: {key: self.item.item_type.key + "jump"})
                            .joins(item: :item_group).joins(item: :item_type)
    if unequip_inventory.any?
      unequip_inventory.update_all(equipped: false)
    end
  end
end

#unnequip_same_type_item(user_id) ⇒ Object



8
9
10
# File 'app/models/gamification/inventory.rb', line 8

def unnequip_same_type_item(user_id)
  Inventory.where(user_id: user_id,gamification_items: {gamification_item_type_id: self.item.item_type.id}).joins(:item).update_all(equipped: false)
end