Class: Staticpress::Content::Category

Inherits:
Base
  • Object
show all
Includes:
CollectionContent, ResourceContent
Defined in:
lib/staticpress/content/category.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#params, #template_types

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceContent

#find_supported_extensions, #gather_resources_from, #load_resource

Methods included from CollectionContent

#template_path, #template_types

Methods inherited from Base

#==, #content, #content_type, #exist?, find_by_url_path, #full_title, #layout, #markup_template?, #meta, #output_path, #published?, #raw, #render, #render_partial, #save, #save!, #template_context, #template_engine_options, #template_extension, #template_path_content, #theme, theme, #title, #to_s, type, #url_path

Methods included from Helpers

#config, #extensionless_basename, #extensionless_path, #hash_from_array, #hash_from_match_data, #paginate, #settings, #spider_map, #titleize

Constructor Details

#initialize(params) ⇒ Category

Returns a new instance of Category.



8
9
10
11
# File 'lib/staticpress/content/category.rb', line 8

def initialize(params)
  @name = params[:name]
  super
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/staticpress/content/category.rb', line 6

def name
  @name
end

Class Method Details

.allObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/staticpress/content/category.rb', line 43

def self.all
  reply = []

  content_by_category.each do |category, posts|
    1.upto paginate(posts).count do |number|
      reply << new(:name => category, :number => number)
    end
  end

  reply
end

.categoriesObject



59
60
61
# File 'lib/staticpress/content/category.rb', line 59

def self.categories
  content_by_category.keys
end

.content_by_categoryObject



63
64
65
66
67
68
69
70
71
# File 'lib/staticpress/content/category.rb', line 63

def self.content_by_category
  reply = Hash.new { |hash, key| hash[key] = [] }
  Staticpress::Content::Post.published.each do |post|
    (post.meta.categories || []).each do |category|
      (reply[category] ||= []) << post
    end
  end
  reply
end

.publishedObject



55
56
57
# File 'lib/staticpress/content/category.rb', line 55

def self.published
  all
end

Instance Method Details

#optional_param_defaultsObject



13
14
15
# File 'lib/staticpress/content/category.rb', line 13

def optional_param_defaults
  { :number => pages_count }
end

#pages_countObject



17
18
19
# File 'lib/staticpress/content/category.rb', line 17

def pages_count
  (self.class.content_by_category[name].count / config.posts_per_page.to_f).ceil
end

#preferred_layout_namesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/staticpress/content/category.rb', line 25

def preferred_layout_names
  reply = []

  if params[:name].nil?
    reply << :category
  else
    if params[:number].nil?
      reply << :category_index
      reply << :post_index
    end

    reply << :category_paged
    reply << :post_paged
  end

  reply
end

#sub_contentObject



21
22
23
# File 'lib/staticpress/content/category.rb', line 21

def sub_content
  paginate(self.class.content_by_category[params[:name]].sort)[params[:number] - 1]
end