Class: ShopPackage

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/shop_package.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attrsObject

Returns attributes attached to the product



51
52
53
# File 'app/models/shop_package.rb', line 51

def attrs
  [ :id, :name, :price, :sku, :description, :created_at, :updated_at ]
end

.incldeObject

Returns the objects to include



61
62
63
# File 'app/models/shop_package.rb', line 61

def inclde
  [ :category, :products ]
end

.methdsObject

Returns methods with usefuly information



56
57
58
# File 'app/models/shop_package.rb', line 56

def methds
  []
end

.paramsObject

Returns a custom hash of attributes on the product



66
67
68
# File 'app/models/shop_package.rb', line 66

def params
  { :only  => self.attrs, :methods => self.methds, :include => self.inclde }
end

Instance Method Details

#available_productsObject

Returns the products not attached to the category



20
# File 'app/models/shop_package.rb', line 20

def available_products; ShopProduct.all - self.products; end

#categoryObject

Not a valid option, but useful incase a tag is called



29
# File 'app/models/shop_package.rb', line 29

def category; nil; end

#slugObject

Returns the slug of the first product



23
# File 'app/models/shop_package.rb', line 23

def slug; packings.first.product.slug; end

#to_json(*attrs) ⇒ Object

Overloads the base to_json to return what we want



32
# File 'app/models/shop_package.rb', line 32

def to_json(*attrs); super self.class.params; end

#urlObject

Returns the url of the first product



26
# File 'app/models/shop_package.rb', line 26

def url; packings.first.product.url; end

#valueObject

Returns the aggregate value of the products in the package



35
36
37
38
39
# File 'app/models/shop_package.rb', line 35

def value
  value = 0
  self.packings.map { |pkg| value += (pkg.product.price.to_f * pkg.quantity) }
  value
end

#weightObject

Returns the aggregate weight of the products in the package



42
43
44
45
46
# File 'app/models/shop_package.rb', line 42

def weight
  weight = 0
  self.packings.map { |pkg| weight += (pkg.product.weight.to_f * pkg.quantity) }
  weight
end