Skip to content

Commit

Permalink
Delete all posts when the blog expires
Browse files Browse the repository at this point in the history
Closes #4

This is the actual meat of the application.

See https://github.com/collectiveidea/delayed_job/
  • Loading branch information
c-lliope committed Sep 24, 2016
1 parent 5dcf0ba commit 9121aca
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//= require_tree .

function getTimeRemaining(endtime){
var t = Date.parse(endtime) - Date.parse(new Date());
var t = Date.parse(endtime) - (new Date()).getTime();
var seconds = Math.floor( (t/1000) % 60 );
var minutes = Math.floor( (t/1000/60) % 60 );
var hours = Math.floor( (t/(1000*60*60)) % 24 );
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class PostsController < ApplicationController
include ApplicationHelper
before_action :set_post, only: [:show, :edit, :update, :destroy]

http_basic_authenticate_with(
Expand Down Expand Up @@ -30,6 +31,7 @@ def create
@post = Post.new(post_params)

if @post.save
SelfDestruct.new.delay(run_at: expiration_time).attempt
redirect_to @post, notice: "Post was successfully created."
else
render :new
Expand Down
6 changes: 5 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# frozen_string_literal: true

module ApplicationHelper
GRACE_PERIOD = 1.week.freeze

def markdown(source)
Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(source).html_safe
end

def expiration_time
if Post.any?
Post.order(created_at: :desc).first.created_at + 7.days
Post.order(created_at: :desc).first.created_at + GRACE_PERIOD
else
Time.current
end
Expand Down
14 changes: 14 additions & 0 deletions app/jobs/self_destruct.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class SelfDestruct
include ApplicationHelper

def attempt
if Time.current > expiration_time
Rails.logger.fatal "[Self Destruct] Time expired, deleting all posts!"
Post.destroy_all
else
Rails.logger.info "[Self Destruct] Time has not expired, cancelling self-destruct"
end
end
end
22 changes: 22 additions & 0 deletions spec/models/self_destruct_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "rails_helper"

describe SelfDestruct do
describe "#attempt" do
it "deletes all posts if it is after the expiration" do
grace_period = ApplicationHelper::GRACE_PERIOD
create(:post, created_at: (grace_period + 1.minute).ago)

expect { SelfDestruct.new.attempt }.to change(Post, :count).from(1).to(0)
end

it "does not delete anything if it is before the expiration" do
grace_period = ApplicationHelper::GRACE_PERIOD
create(:post, created_at: (grace_period + 1.minute).ago)
create(:post, created_at: (grace_period - 1.minute).ago)

expect { SelfDestruct.new.attempt }.not_to change(Post, :count).from(2)
end
end
end

0 comments on commit 9121aca

Please sign in to comment.