Class: ShopProduct
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ShopProduct
- Defined in:
- app/models/shop_product.rb
Class Method Summary collapse
-
.attrs ⇒ Object
Returns attributes attached to the product.
-
.methds ⇒ Object
Returns methods with usefuly information.
-
.params ⇒ Object
Returns a custom hash of attributes on the product.
-
.sort(category_id, product_ids) ⇒ Object
Sorts products within a category.
-
.to_sku(url) ⇒ Object
Converts a url to a pretty sku and removes the shop prefix /shop/page/category/product page-category-product.
Instance Method Summary collapse
-
#apply_variant_template(variant) ⇒ Object
Applies an array of variant names as product_variants.
-
#available_images ⇒ Object
Returns images not attached to product.
-
#category ⇒ Object
Returns category through the pages parent.
-
#category_id ⇒ Object
Returns id of category.
-
#customer_ids ⇒ Object
Returns an array of customer ids.
-
#customers ⇒ Object
Returns the customers of this product.
-
#description ⇒ Object
Returns the content of the product’s page’s description part.
-
#image_ids ⇒ Object
Returns an array of image ids.
-
#images ⇒ Object
Return an array of the pages images.
-
#name ⇒ Object
Returns the title of the product’s page.
-
#sku ⇒ Object
Returns the url of the page formatted as an sku.
-
#slug ⇒ Object
Returns the page slug.
-
#to_json(*attrs) ⇒ Object
Overloads the base to_json to return what we want.
-
#url ⇒ Object
Returns the url of the page.
Class Method Details
.attrs ⇒ Object
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 |
.methds ⇒ Object
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 |
.params ⇒ Object
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..each do |variant| variants.new(:name => variant).save! rescue (result = false) end result end |
#available_images ⇒ Object
Returns images not attached to product
57 |
# File 'app/models/shop_product.rb', line 57 def available_images; Image.all - images; end |
#category ⇒ Object
Returns category through the pages parent
31 |
# File 'app/models/shop_product.rb', line 31 def category; page.parent.shop_category; end |
#category_id ⇒ Object
Returns id of category
34 |
# File 'app/models/shop_product.rb', line 34 def category_id; category.id; end |
#customer_ids ⇒ Object
Returns an array of customer ids
48 |
# File 'app/models/shop_product.rb', line 48 def customer_ids; customers.map(&:id); end |
#customers ⇒ Object
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 |
#description ⇒ Object
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_ids ⇒ Object
Returns an array of image ids
54 |
# File 'app/models/shop_product.rb', line 54 def image_ids; images.map(&:id); end |
#images ⇒ Object
Return an array of the pages images
51 |
# File 'app/models/shop_product.rb', line 51 def images; page.images; end |
#name ⇒ Object
Returns the title of the product’s page
22 |
# File 'app/models/shop_product.rb', line 22 def name; page.title; end |
#sku ⇒ Object
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 |
#slug ⇒ Object
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 |
#url ⇒ Object
Returns the url of the page
28 |
# File 'app/models/shop_product.rb', line 28 def url; page.url; end |