add simple blog controller

This commit is contained in:
Torsten Ruger
2018-04-10 19:11:33 +03:00
parent 5950fac3ce
commit 4b927c4f29
5 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,19 @@
class BlogController < ApplicationController
def index
@pages = Page.pages
end
def page
title = params[:title]
return redirect_to(root_path) unless title
@page = get_page(title)
return redirect_to(root_path) unless @page
end
def get_page(title)
page = Page.pages[title]
#puts "No #{title} in #{Page.pages.keys.join(':')}"
page
end
end

54
app/models/post.rb Normal file
View File

@ -0,0 +1,54 @@
class Post
@@posts = nil
attr_reader :year , :month , :day , :dir , :ext
def initialize(path)
@dir = File.dirname(path)
base , @ext = File.basename(path).split(".")
raise "must be partial, statr with _ not:#{base}" unless base[0] == "_"
@words = base.split("-")
@year = parse_int(@words.shift[1 .. -1] , 2100)
@day = parse_int(@words.shift , 32)
@month = parse_int(@words.shift , 12)
raise "Invalid path #{path}" unless @words
end
def slug
@words.join("-").downcase
end
def title
@words.join(" ")
end
def template_name
"#{date}-#{@words.join("-")}"
end
def date
"#{year}-#{day}-#{month}"
end
def parse_int( value , max)
ret = value.to_i
raise "invalid value #{value} > #{max}" if ret > max or ret < 1
ret
end
def content
File.open("#{@dir}/_#{template_name}.#{ext}" ).read
end
def summary
ret = content.split("%h2").first.gsub("%p", "<br/>").html_safe
ret[0 .. 400]
end
def self.posts
return @@posts if @@posts
@@posts ={}
Dir["#{self.blog_path}/_2*.haml"].reverse.each do |file|
post = Post.new(file)
@@posts[post.slug] = post
end
@@posts
end
def self.blog_path
Rails.configuration.blog_path
end
end