Class: Blog

Inherits:
Page
  • Object
show all
Defined in:
lib/models/blog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



2
3
4
# File 'lib/models/blog.rb', line 2

def month
  @month
end

#number_of_pagesObject (readonly)

Returns the value of attribute number_of_pages.



2
3
4
# File 'lib/models/blog.rb', line 2

def number_of_pages
  @number_of_pages
end

#page_paramsObject (readonly)

Returns the value of attribute page_params.



2
3
4
# File 'lib/models/blog.rb', line 2

def page_params
  @page_params
end

#tagObject (readonly)

Returns the value of attribute tag.



2
3
4
# File 'lib/models/blog.rb', line 2

def tag
  @tag
end

#total_articlesObject (readonly)

Returns the value of attribute total_articles.



2
3
4
# File 'lib/models/blog.rb', line 2

def total_articles
  @total_articles
end

#yearObject (readonly)

Returns the value of attribute year.



2
3
4
# File 'lib/models/blog.rb', line 2

def year
  @year
end

Instance Method Details

#all_article_monthsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/models/blog.rb', line 87

def all_article_months
  counts = Hash.new(0)

  # generate a count of articles for each month
  children.each do |child|
    date = child.published.at_beginning_of_month
    counts[date] += 1
  end

  # collect the months into an array of counted values
  months = counts.each_pair.collect {|date, count| OpenStruct.new(date: date, count: count, path: month_path(date.month, date.year))}
  months.sort_by(&:date).reverse
end

#all_article_tagsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/models/blog.rb', line 101

def 
  counts = Hash.new(0)

  # count the number of articles each tag appears in
  children.each do |child|
    child.tags.each do |tag|
      counts[tag] += 1
    end
  end

  # collect the tags into an array of counted values
  tags = counts.each_pair.collect {|tag, count| OpenStruct.new(tag: tag, count: count, path: tag_path(tag))}
  tags.sort_by(&:count).reverse
end

#articlesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/models/blog.rb', line 20

def articles
  return @articles if @articles
  query = site.articles.where(parent: id).order('published desc')
  
  # FIXME: merge in to search page
  if params['tag']
    @tag = params['tag']
    query = query.where(tags: @tag)
    @page_params = "tag=#{params['tag']}&"
  elsif params['month'] && params['year']
    @month = [[params['month'].to_i, 1].max, 12].min # constrain the month between 1..12
    @year  = params['year'].to_i
    query = query.where(:published.gte => Time.local(@year, @month, 1), :published.lte => Time.local(@year, @month + 1, 1))
    @page_params = "year=#{params['year']}&month=#{params['month']}&"        
  else
    @page_params = ''
  end
  
  @total_articles = query.count
  @number_of_pages = (@total_articles.to_f / articles_per_page).ceil
  query.limit(articles_per_page).skip(page_number * articles_per_page).all
end

#first_page?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/models/blog.rb', line 47

def first_page?
  page_number == 0
end

#last_page?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/models/blog.rb', line 51

def last_page?
  page_number == (@number_of_pages - 1)
end

#latest_articles(limit = 3) ⇒ Object



43
44
45
# File 'lib/models/blog.rb', line 43

def latest_articles(limit = 3)
  site.articles.where(parent: id).order('published desc').limit(limit).all
end

#month_path(month, year) ⇒ Object



83
84
85
# File 'lib/models/blog.rb', line 83

def month_path(month, year)
  "#{path}?month=#{month}&year=#{year}"
end

#next_page_pathObject



75
76
77
78
79
80
81
# File 'lib/models/blog.rb', line 75

def next_page_path
  if last_page?
    path
  else
    "#{path}?page=#{page_number + 1}"
  end
end

#page_numberObject



55
56
57
# File 'lib/models/blog.rb', line 55

def page_number
  @page_number ||= params['page'].to_i
end

#page_path(page_number) ⇒ Object



63
64
65
# File 'lib/models/blog.rb', line 63

def page_path(page_number)
  "#{path}?page=#{page_number}"
end

#previous_page_pathObject



67
68
69
70
71
72
73
# File 'lib/models/blog.rb', line 67

def previous_page_path
  if first_page?
    path
  else
    "#{path}?page=#{page_number - 1}"
  end
end

#tag_path(tag) ⇒ Object



59
60
61
# File 'lib/models/blog.rb', line 59

def tag_path(tag)
  "#{path}?tag=#{CGI::escape(tag || '')}"
end

#xmlObject



16
17
18
# File 'lib/models/blog.rb', line 16

def xml
  @xml
end