Sha256: 107eb5c61e22666e83d0187fbaa217e6df284d5d7798145c63fb1eba8ef1889b

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Gamification
  class Inventory < ApplicationRecord
    belongs_to :user
    belongs_to :item, class_name: 'Gamification::Item', foreign_key: :gamification_item_id

    scope :equipped, -> {where equipped: true}

    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

    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

    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

    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
go_gamification-0.0.20 app/models/gamification/inventory.rb
go_gamification-0.0.19 app/models/gamification/inventory.rb
go_gamification-0.0.18 app/models/gamification/inventory.rb
go_gamification-0.0.17 app/models/gamification/inventory.rb