Class: Pbw::ItemContainer

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/pbw/item_container.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.creatable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/models/pbw/item_container.rb', line 19

def self.creatable_by?(user, subject)
    true
end

.deletable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/pbw/item_container.rb', line 27

def self.deletable_by?(user, subject)
    user.admin?
end

.editable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/pbw/item_container.rb', line 23

def self.editable_by?(user, subject)
    user.admin? || subject.user == user || (subject.token && subject.token.user == user)
end

.find_for_area(area, item) ⇒ Object



47
48
49
# File 'app/models/pbw/item_container.rb', line 47

def self.find_for_area(area, item)
    where(area: area, item: item).first
end

.find_for_token(token, item) ⇒ Object



37
38
39
# File 'app/models/pbw/item_container.rb', line 37

def self.find_for_token(token, item)
    where(token: token, item: item).first
end

.find_for_user(user, item) ⇒ Object



57
58
59
# File 'app/models/pbw/item_container.rb', line 57

def self.find_for_user(user, item)
    where(user: user, item: item).first
end

.find_or_create_for_area(area, item, quantity_to_add) ⇒ Object



41
42
43
44
45
# File 'app/models/pbw/item_container.rb', line 41

def self.find_or_create_for_area(area, item, quantity_to_add)
	container = where(area: area, item: item).first
	container = new(area: area, item: item) unless container
	container.add_item(quantity_to_add) && container.save ? container : false
end

.find_or_create_for_token(token, item, quantity_to_add) ⇒ Object



31
32
33
34
35
# File 'app/models/pbw/item_container.rb', line 31

def self.find_or_create_for_token(token, item, quantity_to_add)
	container = where(token: token, item: item).first
	container = new(token: token, item: item) unless container
	container.add_item(quantity_to_add) && container.save ? container : false
end

.find_or_create_for_user(user, item, quantity_to_add) ⇒ Object



51
52
53
54
55
# File 'app/models/pbw/item_container.rb', line 51

def self.find_or_create_for_user(user, item, quantity_to_add)
	container = where(user: user, item: item).first
	container = new(user: user, item: item) unless container
	container.add_item(quantity_to_add) && container.save ? container : false
end

.viewable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/models/pbw/item_container.rb', line 15

def self.viewable_by?(user, subject)
    user.admin? || subject.user == user || (subject.token && subject.token.user == user)
end

Instance Method Details

#add_item(quantity) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/pbw/item_container.rb', line 61

def add_item(quantity)
	return remove_item!(quantity.abs) if quantity < 0
	return false unless self.item.before_add(self,quantity)
	self.quantity = self.quantity + quantity
	self.item.after_add(self,quantity)
	self
end

#belongs_toObject



100
101
102
# File 'app/models/pbw/item_container.rb', line 100

def belongs_to
	self.token || self.area || self.user
end

#empty?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/pbw/item_container.rb', line 104

def empty?
	self.quantity == 0
end

#remove_item(quantity) ⇒ Object



69
70
71
72
73
74
75
# File 'app/models/pbw/item_container.rb', line 69

def remove_item(quantity)
	return add_item!(quantity.abs) if quantity < 0
	return false unless self.item.before_remove(self,quantity)
	self.quantity = self.quantity - quantity
	self.item.after_remove(self,quantity)
	self
end

#transfer_item!(to, quantity) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/pbw/item_container.rb', line 77

def transfer_item!(to, quantity)
	return false unless to && quantity
	return false unless self.item.before_transfer(belongs_to, to, quantity)
	remove_item(quantity)
	return false unless save
	if to.class.ancestors.include?(Area)
		container = find_or_create_for_area(to, self.item, quantity)
	elsif to.class.ancestors.include?(Token)
		container = find_or_create_for_token(to, self.item, quantity)
	elsif to.class.ancestors.include?(Pbw::Engine.user_class)
		container = find_or_create_for_user(to, self.item, quantity)
	else
		return false
	end
	unless container
		add_item(quantity) && save
		false 
	else
		self.item.after_transfer(belongs_to, to, quantity)
		container
	end
end