Permalink
Browse files

Merge branch 'future'

  • Loading branch information...
yhara committed Nov 17, 2016
2 parents 0dcc730 + 484a248 commit f37bf58aa461323e2999d881e704e7c7687fa232
Showing with 73 additions and 16 deletions.
  1. +28 −13 app.rb
  2. +30 −0 spec/app_spec.rb
  3. +14 −2 views/edit.slim
  4. +1 −1 views/screen.sass
View
41 app.rb
@@ -13,14 +13,19 @@
require 'active_support/core_ext/object/to_query'
class Post < ActiveRecord::Base
validates_presence_of :body
validates_presence_of :datetime
validates_presence_of :published_at
validates_presence_of :body, :title, :datetime, :published_at
scope :published, ->{ where('datetime <= ?', Time.now) }
scope :future, ->{ where('datetime > ?', Time.now) }
def permanent?
permanent
end
def future?
self.datetime > Time.now
end
def url
URI.join(NLog2.config[:blog][:url], path_to_show).to_s
end
@@ -191,13 +196,15 @@ def next_page_path(scope, params={})
#
get '/' do
@posts = Post.where(permanent: false)
@posts = Post.published
.where(permanent: false)
.order(datetime: :desc).page(params[:page]).per(10)
slim :index
end
get '/_list' do
@posts = Post.where(permanent: false)
@posts = Post.published
.where(permanent: false)
.order(datetime: :desc).page(params[:page]).per(100)
slim :list
end
@@ -207,7 +214,7 @@ def next_page_path(scope, params={})
d = Date.new(*date.map(&:to_i))
range = d.in_time_zone...(d+1).in_time_zone
cond = Post.where(permanent: false, slug: slug_or_id)
cond = Post.published.where(permanent: false, slug: slug_or_id)
if (id = Integer(slug_or_id) rescue nil)
cond = cond.or(Post.where(id: id))
end
@@ -227,7 +234,8 @@ def next_page_path(scope, params={})
end
get '/_feed.xml' do
@feed_posts = Post.where(permanent: false)
@feed_posts = Post.published
.where(permanent: false)
.order(datetime: :desc).limit(10)
builder :_feed
end
@@ -239,6 +247,7 @@ def next_page_path(scope, params={})
get '/_edit' do redirect '/_edit/' end
get '/_edit/:id?' do
authenticate!
@flash = {}
if (id = params[:id])
@post = Post.find_by(id: id) or raise Sinatra::NotFound
else
@@ -251,7 +260,7 @@ def next_page_path(scope, params={})
post '/_edit' do
authenticate!
@flash_error = nil
@flash = {}
if (id = params[:id])
@post = Post.find_by(id: id) or raise Sinatra::NotFound
else
@@ -265,16 +274,22 @@ def next_page_path(scope, params={})
if (d = Time.zone.parse(params[:datetime]) rescue nil)
@post.datetime = d
else
@flash_error = "Failed to parse date: #{params[:datetime].inspect}"
@flash[:error] = "Failed to parse date: #{params[:datetime].inspect}"
@post.datetime = Time.now
end
if params[:submit_by] == "Save" && !@flash_error
if params[:submit_by] == "Save" && !@flash[:error]
@post.published_at ||= Time.now
if @post.save
redirect @post.path_to_show
if @post.future?
@flash[:notice] = "Scheduled `#{@post.title}' to be posted at #{@post.author_datetime}"
@post = Post.new; @post.datetime = Time.now
slim :edit
else
redirect @post.path_to_show
end
else
@flash_error = "Failed to save record: #{@post.errors.messages.inspect}"
@flash[:error] = "Failed to save record: #{@post.errors.messages.inspect}"
slim :edit
end
else
@@ -290,7 +305,7 @@ def next_page_path(scope, params={})
# Permanent articles
#
get %r{/(\w+)} do |name|
@post = Post.where(permanent: true, slug: name).first
@post = Post.published.where(permanent: true, slug: name).first
raise Sinatra::NotFound unless @post
@title = @post.title
slim :show
View
@@ -36,6 +36,13 @@ def app
get '/'
expect(last_response.body).to include("TITLE")
end
it 'should not show future post' do
Post.create!(@valid_posted.merge(datetime: Time.now + 3600,
title: "FUTURE POST"))
get '/'
expect(last_response.body).not_to include("FUTURE POST")
end
end
describe '/yyyy/dd/mm/xx' do
@@ -58,6 +65,14 @@ def app
get "/1234/12/12/#{post.id}"
expect(last_response.body).to include("this is body")
end
it 'should not show future post' do
Post.create!(@valid_posted.merge(
slug: "future-post",
datetime: Time.utc(9999, 12, 12)))
get '/9999/12/12/future-post'
expect(last_response).to be_not_found
end
end
describe '/_feed.xml' do
@@ -67,6 +82,13 @@ def app
expect(last_response.body).to start_with("<?xml")
expect(last_response.body).to include(@valid_params[:body])
end
it 'should not include future post' do
Post.create!(@valid_posted.merge(datetime: Time.now + 3600,
title: "FUTURE POST"))
get '/_feed.xml'
expect(last_response.body).not_to include("FUTURE POST")
end
end
describe '/_edit (no trailing slash)' do
@@ -155,6 +177,14 @@ def app
expect(url).to end_with("SLUG")
end
context 'when saving future post' do
it 'should show edit page again' do
authorize 'jhon', 'passw0rd'
post '/_edit', @valid_params.merge(datetime: Time.now + 3600, submit_by: "Save")
expect(last_response).to be_ok # Not redirect
end
end
it 'should not raise error when failed to parse datetime' do
count = Post.count
View
@@ -1,5 +1,17 @@
.flash
.error=@flash_error
- if @flash[:notice]
.flash.notice = @flash[:notice]
- if @flash[:error]
.flash.error = @flash[:error]
- if Post.future.any?
h2 Scheduled posts
.future-posts
ul
- Post.future.each do |post|
li
a href=(post.path_to_edit)
= "#{post.title} (#{post.author_datetime})"
- if params[:submit_by] == "Preview"
.preview
View
@@ -32,7 +32,7 @@ code
border: 1px solid gray
margin: 1rem
padding: 0.5rem
.error
&.error
color: red
.post

0 comments on commit f37bf58

Please sign in to comment.