From 7e391f0369785fb1a8af19a3a601ea0b1a0c38f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Sat, 7 Dec 2019 11:31:19 +0200 Subject: [PATCH] Code blog model to use year based directories --- app/models/post.rb | 31 ++++++++++++++++++------------- spec/models/blog_spec.rb | 22 +++++++++++----------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index e1cd1bd..2cd469f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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", "
").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 diff --git a/spec/models/blog_spec.rb b/spec/models/blog_spec.rb index 2f8caf9..0d3487b 100644 --- a/spec/models/blog_spec.rb +++ b/spec/models/blog_spec.rb @@ -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