Skip to content

Commit 371a821

Browse files
committed
Consume Frontmatter
1 parent 68dd401 commit 371a821

3 files changed

Lines changed: 99 additions & 3 deletions

File tree

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
class ProgrammingLanguagesController < ApplicationController
22
def index
3+
render locals: {
4+
programming_languages: ProgrammingLanguage.all
5+
}
36
end
47

58
def show
6-
render params[:language]
9+
programming_language = ProgrammingLanguage.find!(params[:language])
10+
11+
render programming_language.slug
712
end
813
end

‎app/models/programming_language.rb‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class ProgrammingLanguage
2+
class << self
3+
def all
4+
# Find all the programming languages and sort them, newest first
5+
Dir.children(dirname)
6+
.filter { programming_language_template?(_1) }
7+
.map { dirname.join(_1) }
8+
.map { ProgrammingLanguage.new(_1) }
9+
.sort
10+
.reverse
11+
end
12+
13+
def find(slug)
14+
all.find do |programming_language|
15+
programming_language.slug == slug
16+
end
17+
end
18+
19+
def find!(slug)
20+
find(slug) || raise_record_not_found
21+
end
22+
23+
private
24+
25+
def dirname
26+
# Where all the templates are placed
27+
Rails.root.join('app/views/programming_languages')
28+
end
29+
30+
def programming_language_template?(filename)
31+
# Do not treat templates for regular actions as programming languages
32+
[
33+
'index.',
34+
'show.',
35+
'edit.',
36+
'new.'
37+
].none? do |reserved|
38+
filename.starts_with?(reserved)
39+
end
40+
end
41+
42+
def raise_record_not_found
43+
raise ActiveRecord::RecordNotFound
44+
end
45+
end
46+
47+
attr_accessor :pathname
48+
delegate :content, :data, to: :frontmatter, prefix: true
49+
50+
def initialize(pathname)
51+
@pathname = pathname
52+
end
53+
54+
def slug
55+
pathname.basename.to_s.split('.').first
56+
end
57+
58+
def path
59+
"/#{slug}"
60+
end
61+
62+
def name
63+
frontmatter_data['name']
64+
end
65+
66+
def first_introduced
67+
frontmatter_data['first_introduced']
68+
end
69+
70+
def <=>(other)
71+
# Can only be compare with other ProgrammingLanguage instances
72+
return nil unless other.is_a?(self.class)
73+
74+
# Compare name if both languages was introduced the same year
75+
return name <=> other.name if first_introduced == other.first_introduced
76+
77+
# Age before beauty
78+
first_introduced <=> other.first_introduced
79+
end
80+
81+
private
82+
83+
def frontmatter
84+
# Parse templates with RubyMatter
85+
@frontmatter ||= RubyMatter.read(pathname)
86+
end
87+
end
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
h1 ProgrammingLanguages#index
2-
p Find me in app/views/programming_languages/index.html.slim
1+
h1 Programming languages:
2+
3+
ol
4+
- programming_languages.each do |programming_language|
5+
li
6+
= link_to(programming_language.name, programming_language.path)

0 commit comments

Comments
 (0)