Class: ShopCategory

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attrsObject

Returns attributes attached to the category



78
79
80
# File 'app/models/shop_category.rb', line 78

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

.methdsObject

Returns methods with usefuly information



83
84
85
# File 'app/models/shop_category.rb', line 83

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

.paramsObject

Returns a custom hash of attributes on the category



88
89
90
# File 'app/models/shop_category.rb', line 88

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

.sort(category_ids) ⇒ Object

Sorts a group of categories based on their ID and position in an array



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

def sort(category_ids)
  category_ids.each_with_index do |id,index|
    ShopCategory.find(id).update_attributes!(
      :position  => index
    )
  end
end

Instance Method Details

#available_imagesObject

Returns images not attached to category



58
# File 'app/models/shop_category.rb', line 58

def available_images; Image.all - images; end

#categoriesObject

Returns the categories nested directly beneath this



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

def categories
  pages = page.children.all(
    :conditions => { :class_name => 'ShopCategoryPage' },
    :order      => 'pages.position ASC'
  ).map(&:shop_category)
end

#descriptionObject

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



28
29
30
# File 'app/models/shop_category.rb', line 28

def description
  page.render_part('description')
end

#handleObject

Returns the url of the page formatted as an sku



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

def handle; ShopProduct.to_sku(slug); end

#image_idsObject

Returns an array of image ids



55
# File 'app/models/shop_category.rb', line 55

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

#imagesObject

Return an array of the pages images



52
# File 'app/models/shop_category.rb', line 52

def images; page.images; end

#nameObject

Returns the title of the categories’ page



19
# File 'app/models/shop_category.rb', line 19

def name; page.title; end

#productsObject

Returns products through the pages children



33
34
35
36
37
38
# File 'app/models/shop_category.rb', line 33

def products
  pages = page.children.all(
    :conditions => { :class_name => 'ShopProductPage' },
    :order      => 'pages.position ASC'
  ).map(&:shop_product)
end

#slugObject

Returns the page slug



61
# File 'app/models/shop_category.rb', line 61

def slug; page.slug; end

#to_json(*attrs) ⇒ Object

Overloads the base to_json to return what we want



64
# File 'app/models/shop_category.rb', line 64

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

#urlObject

Returns the url of the page



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

def url; page.url; end