Code blog model to use year based directories
This commit is contained in:
parent
285c6531e4
commit
7e391f0369
@ -1,17 +1,17 @@
|
|||||||
class Post
|
class Post
|
||||||
@@posts = nil
|
@@posts = nil
|
||||||
|
|
||||||
attr_reader :year , :month , :day , :dir , :ext
|
attr_reader :year , :month , :day , :ext
|
||||||
|
|
||||||
def initialize(path)
|
def initialize(path)
|
||||||
@dir = File.dirname(path)
|
year , rest = *path.split("/")
|
||||||
base , @ext = File.basename(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] == "_"
|
raise "must be partial, start with _ not:#{base}" unless base[0] == "_"
|
||||||
@words = base.split("-")
|
@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
|
raise "Invalid path #{path}" unless @words
|
||||||
|
@month = parse_int(@words.shift[1 .. -1] , 12 , "month")
|
||||||
|
@day = parse_int(@words.shift , 32 , "week")
|
||||||
end
|
end
|
||||||
|
|
||||||
def slug
|
def slug
|
||||||
@ -21,18 +21,21 @@ class Post
|
|||||||
@words.join(" ")
|
@words.join(" ")
|
||||||
end
|
end
|
||||||
def template_name
|
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
|
end
|
||||||
def date
|
def date
|
||||||
Date.new(year,month,day)
|
Date.new(year,month,day)
|
||||||
end
|
end
|
||||||
def parse_int( value , max)
|
def parse_int( value , max , name)
|
||||||
ret = value.to_i
|
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
|
ret
|
||||||
end
|
end
|
||||||
def content
|
def content
|
||||||
File.open("#{@dir}/_#{template_name}.#{ext}" ).read
|
File.open("#{self.class.blog_path}/#{template_file}.#{ext}" ).read
|
||||||
end
|
end
|
||||||
def summary
|
def summary
|
||||||
ret = content.split("%h2").first.gsub("%p", "<br/>").html_safe
|
ret = content.split("%h2").first.gsub("%p", "<br/>").html_safe
|
||||||
@ -41,15 +44,17 @@ class Post
|
|||||||
def self.posts
|
def self.posts
|
||||||
return @@posts if @@posts
|
return @@posts if @@posts
|
||||||
posts ={}
|
posts ={}
|
||||||
Dir["#{self.blog_path}/_2*.haml"].reverse.each do |file|
|
Dir["#{self.blog_path}/2*/_*.haml"].reverse.each do |file|
|
||||||
post = Post.new(file)
|
parts = file.split("/")
|
||||||
|
post_name = parts[-2] + "/" + parts[-1]
|
||||||
|
post = Post.new(post_name)
|
||||||
posts[post.slug] = post
|
posts[post.slug] = post
|
||||||
end
|
end
|
||||||
@@posts = posts.sort_by { |slug, post| post.sort_key }.reverse.to_h
|
@@posts = posts.sort_by { |slug, post| post.sort_key }.reverse.to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_key
|
def sort_key
|
||||||
year*10000 + month*1000 + day
|
year*1000 + month*10 + day
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.blog_path
|
def self.blog_path
|
||||||
|
@ -4,23 +4,23 @@ RSpec.describe Post, type: :model do
|
|||||||
|
|
||||||
describe "creation" do
|
describe "creation" do
|
||||||
it "ok with valid slug" 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
|
expect(post).not_to eq nil
|
||||||
end
|
end
|
||||||
it "raises with invalid slug" do
|
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
|
end
|
||||||
it "must start with a year" do
|
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
|
end
|
||||||
it "must start with a underscore" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "basic api" do
|
describe "basic api" do
|
||||||
before :each do
|
before :each do
|
||||||
@post = Post.new("_1993-2-4-title")
|
@post = Post.new("1993/_2-4-title.haml")
|
||||||
end
|
end
|
||||||
it "returns title" do
|
it "returns title" do
|
||||||
expect(@post.title).to eq "title"
|
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)
|
expect(@post.date).to eq Date.new(1993,2,4)
|
||||||
end
|
end
|
||||||
it "returns file_name" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "precise api definition" do
|
describe "precise api definition" do
|
||||||
it "returns whole title" 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"
|
expect(post.title).to eq "Multi word title"
|
||||||
end
|
end
|
||||||
it "returns slug" do
|
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"
|
expect(post.slug).to eq "multi-word-title"
|
||||||
end
|
end
|
||||||
it "returns title without extension if given file name" do
|
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"
|
expect(post.title).to eq "title"
|
||||||
end
|
end
|
||||||
it 'slugs are downcase' do
|
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
|
slug = post.slug
|
||||||
expect(slug.downcase).to eq slug
|
expect(slug.downcase).to eq slug
|
||||||
end
|
end
|
||||||
@ -75,7 +75,7 @@ RSpec.describe Post, type: :model do
|
|||||||
expect(@posts.length).to be > 0
|
expect(@posts.length).to be > 0
|
||||||
end
|
end
|
||||||
it "post template exists" do
|
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
|
expect(File.exists?(file)).to eq true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user