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



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

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

.methdsObject

Returns methods with usefuly information



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

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

.paramsObject

Returns a custom hash of attributes on the category



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

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



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

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

Instance Method Details

#available_imagesObject

Returns images not attached to category



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

def available_images; Image.all - images; end

#categoriesObject

Returns the categories nested directly beneath this



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

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



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

def description
  page.render_part('description')
end

#handleObject

Returns the url of the page formatted as an sku



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

def handle; ShopProduct.to_sku(slug); end

#image_idsObject

Returns an array of image ids



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

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

#imagesObject

Return an array of the pages images



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

def images; page.images; end

#nameObject

Returns the title of the categories’ page



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

def name; page.title; end

#productsObject

Returns products through the pages children



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

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

#slugObject

Returns the page slug



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

def slug; page.slug; end

#to_json(*attrs) ⇒ Object

Overloads the base to_json to return what we want



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

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

#urlObject

Returns the url of the page



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

def url; page.url; end