Class: ShopProduct

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attrsObject

Returns attributes attached to the product



89
90
91
# File 'app/models/shop_product.rb', line 89

def attrs
  [ :id, :price, :page_id, :created_at, :updated_at ]
end

.methdsObject

Returns methods with usefuly information



94
95
96
# File 'app/models/shop_product.rb', line 94

def methds
  [ :category_id, :name, :description, :handle, :url, :customer_ids, :image_ids, :created_at, :updated_at ]
end

.paramsObject

Returns a custom hash of attributes on the product



99
100
101
# File 'app/models/shop_product.rb', line 99

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

.sort(category_id, product_ids) ⇒ Object

Sorts products within a category



77
78
79
80
81
82
83
84
85
86
# File 'app/models/shop_product.rb', line 77

def sort(category_id, product_ids)
  parent_id = ShopCategory.find(category_id).page_id
  
  product_ids.each_with_index do |id, index|
    ShopProduct.find(id).page.update_attributes!(
      :position  => index+1,
      :parent_id => parent_id
    )
  end
end

.to_sku(url) ⇒ Object

Converts a url to a pretty sku and removes the shop prefix /shop/page/category/product page-category-product



104
105
106
107
108
# File 'app/models/shop_product.rb', line 104

def to_sku(url)
  if url.present?
    url.downcase.strip.gsub(/[^a-zA-Z0-9_]/,"_")
  end
end

Instance Method Details

#apply_variant_template(variant) ⇒ Object

Applies an array of variant names as product_variants



66
67
68
69
70
71
72
# File 'app/models/shop_product.rb', line 66

def apply_variant_template(variant)
  result = true
  variant.options.each do |variant|
    variants.new(:name => variant).save! rescue (result = false)
  end
  result
end

#available_imagesObject

Returns images not attached to product



57
# File 'app/models/shop_product.rb', line 57

def available_images; Image.all - images; end

#categoryObject

Returns category through the pages parent



31
# File 'app/models/shop_product.rb', line 31

def category; page.parent.shop_category; end

#category_idObject

Returns id of category



34
# File 'app/models/shop_product.rb', line 34

def category_id; category.id; end

#customer_idsObject

Returns an array of customer ids



48
# File 'app/models/shop_product.rb', line 48

def customer_ids; customers.map(&:id); end

#customersObject

Returns the customers of this product



45
# File 'app/models/shop_product.rb', line 45

def customers; line_items.map(&:customer).flatten.compact.uniq; end

#descriptionObject

Returns the content of the product’s page’s description part



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

def description
  page.render_part('description')
end

#image_idsObject

Returns an array of image ids



54
# File 'app/models/shop_product.rb', line 54

def image_ids; images.map(&:id); end

#imagesObject

Return an array of the pages images



51
# File 'app/models/shop_product.rb', line 51

def images; page.images; end

#nameObject

Returns the title of the product’s page



22
# File 'app/models/shop_product.rb', line 22

def name; page.title; end

#skuObject

Returns the url of the page formatted as an sku



25
# File 'app/models/shop_product.rb', line 25

def sku; ShopProduct.to_sku(slug); end

#slugObject

Returns the page slug



60
# File 'app/models/shop_product.rb', line 60

def slug; page.slug; end

#to_json(*attrs) ⇒ Object

Overloads the base to_json to return what we want



63
# File 'app/models/shop_product.rb', line 63

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

#urlObject

Returns the url of the page



28
# File 'app/models/shop_product.rb', line 28

def url; page.url; end