Class: Staticpress::Content::Tag

Inherits:
Base
  • Object
show all
Includes:
CollectionContent, ResourceContent
Defined in:
lib/staticpress/content/tag.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) ⇒ Tag

Returns a new instance of Tag.



8
9
10
11
# File 'lib/staticpress/content/tag.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/tag.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/tag.rb', line 43

def self.all
  reply = []

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

  reply
end

.content_by_tagObject



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

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

.publishedObject



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

def self.published
  all
end

.tagsObject



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

def self.tags
  content_by_tag.keys
end

Instance Method Details

#optional_param_defaultsObject



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

def optional_param_defaults
  { :number => pages_count }
end

#pages_countObject



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

def pages_count
  (self.class.content_by_tag[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/tag.rb', line 25

def preferred_layout_names
  reply = []

  if params[:name].nil?
    reply << :tag
  else
    if params[:number].nil?
      reply << :tag_index
      reply << :post_index
    end

    reply << :tag_paged
    reply << :post_paged
  end

  reply
end

#sub_contentObject



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

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