Class: Faq

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, Gravtastic
Defined in:
app/models/faq.rb

Constant Summary collapse

TIME_FORMAT =
/-\d{6}/
DATE_FORMAT =
/\d{4}-\d{2}-\d{2}(#{TIME_FORMAT})?/
SLUG_FORMAT =
/[A-Za-z0-9_\-]+/
EXTENSION_FORMAT =
/\.[^.]+/
FILENAME_FORMAT =
/^(#{DATE_FORMAT})-(#{SLUG_FORMAT})(#{EXTENSION_FORMAT})$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Faq

Returns a new instance of Faq.



20
21
22
23
# File 'app/models/faq.rb', line 20

def initialize(path)
  @path = path
  @date_str, _, @slug = File.basename(path).match(FILENAME_FORMAT).captures
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



11
12
13
# File 'app/models/faq.rb', line 11

def slug
  @slug
end

Class Method Details

.allObject



102
103
104
105
106
107
# File 'app/models/faq.rb', line 102

def all
  file_extensions = Faqmarkdown::Config.options[:markdown_file_extensions].join(',')
  @@faqs ||= Dir.glob("#{directory}/*.{#{file_extensions}}").map do |filename|
    Faq.new filename
  end.select(&:visible?).sort_by(&:date).reverse
end

.categories_allObject



116
117
118
119
120
121
122
# File 'app/models/faq.rb', line 116

def categories_all
  categories = []
  all.select do |faq|
    faq.categories.each {|x| categories << x} unless faq.categories.nil?
  end
  categories.uniq
end

.directoryObject



124
125
126
# File 'app/models/faq.rb', line 124

def directory
  Rails.root.join('app', 'faqs')
end

.feedObject



151
152
153
# File 'app/models/faq.rb', line 151

def feed
  all.first(10)
end

.feed_last_modifiedObject



155
156
157
# File 'app/models/faq.rb', line 155

def feed_last_modified
  feed.first.try(:last_modified) || Time.now.utc
end

.find(id) ⇒ Object



139
140
141
# File 'app/models/faq.rb', line 139

def find(id)
  where(:to_param => id).first or raise ActiveRecord::RecordNotFound, "Could not find faq with ID #{id.inspect}"
end

.find_by_category(category = nil) ⇒ Object



109
110
111
112
113
114
# File 'app/models/faq.rb', line 109

def find_by_category(category=nil)
  faqs = []
  all.select do |faq|
    faqs << faq if !faq.categories.nil? and faq.categories_to_url.include?(category)
  end
end

.firstObject



143
144
145
# File 'app/models/faq.rb', line 143

def first
  all.first
end

.lastObject



147
148
149
# File 'app/models/faq.rb', line 147

def last
  all.last
end

.reset!Object



159
160
161
# File 'app/models/faq.rb', line 159

def reset!
  @@faqs = nil
end

.where(conditions = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'app/models/faq.rb', line 128

def where(conditions = {})
  conditions = conditions.symbolize_keys
  conditions.assert_valid_keys :year, :month, :day, :slug, :to_param
  [:year, :month, :day].each do |key|
    conditions[key] = conditions[key].to_i if conditions[key].present?
  end
  all.select do |faq|
    conditions.all? { |key, value| faq.send(key) == value }
  end
end

Instance Method Details

#actionObject



72
73
74
# File 'app/models/faq.rb', line 72

def action
  [:action]
end

#authorObject



60
61
62
# File 'app/models/faq.rb', line 60

def author
  [:author]
end

#categoriesObject



68
69
70
# File 'app/models/faq.rb', line 68

def categories
  [:categories]
end

#categories_to_urlObject



76
77
78
79
80
# File 'app/models/faq.rb', line 76

def categories_to_url
  categories = []
  self.categories.each {|x| categories << x.to_url}
  categories
end

#contentObject



47
48
49
50
# File 'app/models/faq.rb', line 47

def content
  load_content
  @content
end

#dateObject



82
83
84
# File 'app/models/faq.rb', line 82

def date
  @date ||= Time.zone.parse([:date] || @date_str).to_date
end

#emailObject



64
65
66
# File 'app/models/faq.rb', line 64

def email
  [:email]
end

#metadataObject



42
43
44
45
# File 'app/models/faq.rb', line 42

def 
  load_content
  @metadata
end


34
35
36
# File 'app/models/faq.rb', line 34

def permalink_format
  Faqmarkdown::Config.options[:permalink_format]
end

#summaryObject



56
57
58
# File 'app/models/faq.rb', line 56

def summary
  [:summary]
end

#timestampObject Also known as: last_modified



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

def timestamp
  date.to_time_in_current_zone
end

#titleObject



52
53
54
# File 'app/models/faq.rb', line 52

def title
  [:title] || slug.titleize
end

#to_keyObject



38
39
40
# File 'app/models/faq.rb', line 38

def to_key
  [slug]
end

#to_paramObject



25
26
27
28
29
30
31
32
# File 'app/models/faq.rb', line 25

def to_param
  case permalink_format
  when :day   then "%04d/%02d/%02d/%s" % [year, month, day, slug]
  when :month then "%04d/%02d/%s" % [year, month, slug]
  when :year  then "%04d/%s" % [year, slug]
  when :slug  then slug
  end
end

#to_sObject



97
98
99
# File 'app/models/faq.rb', line 97

def to_s
  "#{title.inspect} (#{slug})"
end

#visible?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/faq.rb', line 93

def visible?
  timestamp <= Time.zone.now
end