start on blog functionality

This commit is contained in:
Torsten Ruger 2017-06-11 16:57:22 +03:00
parent 194deaff43
commit 964db2675d
8 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,13 @@
class BlogController < ApplicationController
def page
slug = params[:slug]
return redirect_to(root_path) unless slug
@page = get_page(slug)
return redirect_to(root_path) unless @page
end
def get_page(slug)
Page.new(slug)
end
end

10
app/models/page.rb Normal file
View File

@ -0,0 +1,10 @@
class Page
attr_reader :year , :month , :day , :title
def initialize(path)
@year , @month , @day , @title = path.split("-")
raise "Invalid path #{path}" unless @title
@title = @title.split(".").first
end
end

3
app/views/blog/page.haml Normal file
View File

@ -0,0 +1,3 @@
%h1 Blog
= @page.title

View File

@ -26,6 +26,8 @@ module WebDevSite
config.assets.paths << Gem.loaded_specs['susy'].full_gem_path+'/sass'
config.blog_path = Rails.root.to_s + "/app/blog"
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

View File

@ -1,6 +1,8 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.blog_path = Rails.root.to_s + "/spec/blog"
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped

View File

@ -26,4 +26,7 @@ Rails.application.routes.draw do
root to: 'high_voltage/pages#show' , id: 'index'
get "/blog" , to: "blog#index" , as: :blog_index
get "/blog/*slug" , to: "blog#page" , as: :blog_page
end

View File

@ -0,0 +1,14 @@
feature 'Page show' do
scenario 'page shows for correct slug' do
visit blog_page_url("2017-2-2-Bitcoin")
expect(page).to have_content("Blog")
end
scenario 'page title shows for correct slug' do
visit blog_page_url("2017-2-2-Bitcoin")
expect(page).to have_content("Bitcoin")
end
end

33
spec/models/page_spec.rb Normal file
View File

@ -0,0 +1,33 @@
require 'rails_helper'
RSpec.describe Page, type: :model do
describe "creation" do
it "ok with valid slug" do
page = Page.new("1993-2-4-title")
expect(page).not_to eq nil
end
it "raises with invalid slug" do
expect{Page.new("1993-4-title")}.to raise_error RuntimeError
end
end
describe "basic api" do
before :each do
@page = Page.new("1993-2-4-title")
end
it "returns title" do
expect(@page.title).to eq "title"
end
it "returns dates" do
expect(@page.year).to eq "1993"
expect(@page.month).to eq "2"
expect(@page.day).to eq "4"
end
end
it "returns title without extension if given file name" do
page = Page.new("1993-2-4-title.rb")
expect(page.title).to eq "title"
end
end