Class: Faq
- Inherits:
-
Object
- Object
- Faq
- 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
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
- .all ⇒ Object
- .categories_all ⇒ Object
- .directory ⇒ Object
- .feed ⇒ Object
- .feed_last_modified ⇒ Object
- .find(id) ⇒ Object
- .find_by_category(category = nil) ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .reset! ⇒ Object
- .where(conditions = {}) ⇒ Object
Instance Method Summary collapse
- #action ⇒ Object
- #author ⇒ Object
- #categories ⇒ Object
- #categories_to_url ⇒ Object
- #content ⇒ Object
- #date ⇒ Object
- #email ⇒ Object
-
#initialize(path) ⇒ Faq
constructor
A new instance of Faq.
- #metadata ⇒ Object
- #permalink_format ⇒ Object
- #summary ⇒ Object
- #timestamp ⇒ Object (also: #last_modified)
- #title ⇒ Object
- #to_key ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
- #visible? ⇒ Boolean
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
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
11 12 13 |
# File 'app/models/faq.rb', line 11 def slug @slug end |
Class Method Details
.all ⇒ Object
102 103 104 105 106 107 |
# File 'app/models/faq.rb', line 102 def all file_extensions = Faqmarkdown::Config.[: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_all ⇒ Object
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 |
.directory ⇒ Object
124 125 126 |
# File 'app/models/faq.rb', line 124 def directory Rails.root.join('app', 'faqs') end |
.feed ⇒ Object
151 152 153 |
# File 'app/models/faq.rb', line 151 def feed all.first(10) end |
.feed_last_modified ⇒ Object
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 |
.first ⇒ Object
143 144 145 |
# File 'app/models/faq.rb', line 143 def first all.first end |
.last ⇒ Object
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
#action ⇒ Object
72 73 74 |
# File 'app/models/faq.rb', line 72 def action [:action] end |
#author ⇒ Object
60 61 62 |
# File 'app/models/faq.rb', line 60 def [:author] end |
#categories ⇒ Object
68 69 70 |
# File 'app/models/faq.rb', line 68 def categories [:categories] end |
#categories_to_url ⇒ Object
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 |
#content ⇒ Object
47 48 49 50 |
# File 'app/models/faq.rb', line 47 def content load_content @content end |
#date ⇒ Object
82 83 84 |
# File 'app/models/faq.rb', line 82 def date @date ||= Time.zone.parse([:date] || @date_str).to_date end |
#email ⇒ Object
64 65 66 |
# File 'app/models/faq.rb', line 64 def email [:email] end |
#metadata ⇒ Object
42 43 44 45 |
# File 'app/models/faq.rb', line 42 def load_content @metadata end |
#permalink_format ⇒ Object
34 35 36 |
# File 'app/models/faq.rb', line 34 def permalink_format Faqmarkdown::Config.[:permalink_format] end |
#summary ⇒ Object
56 57 58 |
# File 'app/models/faq.rb', line 56 def summary [:summary] end |
#timestamp ⇒ Object Also known as: last_modified
88 89 90 |
# File 'app/models/faq.rb', line 88 def date.to_time_in_current_zone end |
#title ⇒ Object
52 53 54 |
# File 'app/models/faq.rb', line 52 def title [:title] || slug.titleize end |
#to_key ⇒ Object
38 39 40 |
# File 'app/models/faq.rb', line 38 def to_key [slug] end |
#to_param ⇒ Object
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_s ⇒ Object
97 98 99 |
# File 'app/models/faq.rb', line 97 def to_s "#{title.inspect} (#{slug})" end |
#visible? ⇒ Boolean
93 94 95 |
# File 'app/models/faq.rb', line 93 def visible? <= Time.zone.now end |