Code blog model to use year based directories

This commit is contained in:
Torsten Rüger 2019-12-07 11:31:19 +02:00
parent 285c6531e4
commit 7e391f0369
2 changed files with 29 additions and 24 deletions

View File

@ -1,17 +1,17 @@
class Post
@@posts = nil
attr_reader :year , :month , :day , :dir , :ext
attr_reader :year , :month , :day , :ext
def initialize(path)
@dir = File.dirname(path)
base , @ext = File.basename(path).split(".")
year , rest = *path.split("/")
@year = parse_int(year , 2100 , "year")
base , @ext = File.basename(rest).split(".")
raise "must be partial, start with _ not:#{base}" unless base[0] == "_"
@words = base.split("-")
@year = parse_int(@words.shift[1 .. -1] , 2100)
@month = parse_int(@words.shift , 12)
@day = parse_int(@words.shift , 32)
raise "Invalid path #{path}" unless @words
@month = parse_int(@words.shift[1 .. -1] , 12 , "month")
@day = parse_int(@words.shift , 32 , "week")
end
def slug
@ -21,18 +21,21 @@ class Post
@words.join(" ")
end
def template_name
"#{year}-#{month.to_s.rjust(2, '0')}-#{day.to_s.rjust(2, '0')}-#{@words.join("-")}"
"#{year}/#{month.to_s.rjust(2, '0')}-#{day.to_s.rjust(2, '0')}-#{@words.join("-")}"
end
def template_file
"#{year}/_#{month.to_s.rjust(2, '0')}-#{day.to_s.rjust(2, '0')}-#{@words.join("-")}"
end
def date
Date.new(year,month,day)
end
def parse_int( value , max)
def parse_int( value , max , name)
ret = value.to_i
raise "invalid value #{value} > #{max}" if ret > max or ret < 1
raise "invalid #{name} #{value} > #{max}" if ret > max or ret < 1
ret
end
def content
File.open("#{@dir}/_#{template_name}.#{ext}" ).read
File.open("#{self.class.blog_path}/#{template_file}.#{ext}" ).read
end
def summary
ret = content.split("%h2").first.gsub("%p", "<br/>").html_safe
@ -41,15 +44,17 @@ class Post
def self.posts
return @@posts if @@posts
posts ={}
Dir["#{self.blog_path}/_2*.haml"].reverse.each do |file|
post = Post.new(file)
Dir["#{self.blog_path}/2*/_*.haml"].reverse.each do |file|
parts = file.split("/")
post_name = parts[-2] + "/" + parts[-1]
post = Post.new(post_name)
posts[post.slug] = post
end
@@posts = posts.sort_by { |slug, post| post.sort_key }.reverse.to_h
end
def sort_key
year*10000 + month*1000 + day
year*1000 + month*10 + day
end
def self.blog_path

View File

@ -4,23 +4,23 @@ RSpec.describe Post, type: :model do
describe "creation" do
it "ok with valid slug" do
post = Post.new("_1993-2-4-title")
post = Post.new("1993/_2-4-title")
expect(post).not_to eq nil
end
it "raises with invalid slug" do
expect{Post.new("_1993-4-title")}.to raise_error RuntimeError
expect{Post.new("1993/_4-title")}.to raise_error RuntimeError
end
it "must start with a year" do
expect{Post.new("_no-num-4-title")}.to raise_error RuntimeError
expect{Post.new("no-num/_4-title")}.to raise_error RuntimeError
end
it "must start with a underscore" do
expect{Post.new("_no-num-4-title")}.to raise_error RuntimeError
expect{Post.new("1999/4-4-title")}.to raise_error RuntimeError
end
end
describe "basic api" do
before :each do
@post = Post.new("_1993-2-4-title")
@post = Post.new("1993/_2-4-title.haml")
end
it "returns title" do
expect(@post.title).to eq "title"
@ -34,25 +34,25 @@ RSpec.describe Post, type: :model do
expect(@post.date).to eq Date.new(1993,2,4)
end
it "returns file_name" do
expect(@post.template_name).to eq "1993-02-04-title"
expect(@post.template_name).to eq "1993/02-04-title"
end
end
describe "precise api definition" do
it "returns whole title" do
post = Post.new("_1993-2-4-Multi-word-title")
post = Post.new("1993/_2-4-Multi-word-title")
expect(post.title).to eq "Multi word title"
end
it "returns slug" do
post = Post.new("_1993-2-4-Multi-word-title")
post = Post.new("1993/_2-4-Multi-word-title")
expect(post.slug).to eq "multi-word-title"
end
it "returns title without extension if given file name" do
post = Post.new("_1993-2-4-title.rb")
post = Post.new("1993/_2-4-title.rb")
expect(post.title).to eq "title"
end
it 'slugs are downcase' do
post = Post.new("_1993-2-4-Multi-word-title")
post = Post.new("1993/_2-4-Multi-word-title")
slug = post.slug
expect(slug.downcase).to eq slug
end
@ -75,7 +75,7 @@ RSpec.describe Post, type: :model do
expect(@posts.length).to be > 0
end
it "post template exists" do
file = Post.blog_path + "/_" + @first.template_name + ".haml"
file = Post.blog_path + "/" + @first.template_file + ".haml"
expect(File.exists?(file)).to eq true
end
end