Skip to content

Commit

Permalink
Expire (without test)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorah committed May 24, 2014
1 parent d07d289 commit 85b780b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/controllers/stuffs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class StuffsController < ApplicationController
# GET /stuffs
# GET /stuffs.json
def index
@stuffs = Stuff.all
@stuffs = Stuff.available
@stuff = Stuff.new
end

Expand Down Expand Up @@ -57,6 +57,7 @@ def download
# Use callbacks to share common setup or constraints between actions.
def set_stuff
@stuff = Stuff.find(params[:id])
raise ActiveRecord::RecordNotFound if @stuff.expired?
end

# Never trust parameters from the scary internet, only allow the white list through.
Expand Down
11 changes: 11 additions & 0 deletions app/models/stuff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ class Stuff < ActiveRecord::Base

has_secure_password validations: false

scope :available, -> { where('? < expires_at', Time.now) }

def expired?
self.expires_at ? (Time.now >= self.expires_at) : false
end

before_validation :assign_expires_at
def assign_expires_at
self.expires_at ||= Time.now + 1.day
end

def file
end

Expand Down
19 changes: 18 additions & 1 deletion spec/models/stuff_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'spec_helper'

describe Stuff, :type => :model do
subject(:stuff) { Stuff.new(title: 'foo') }
let(:attrs) { {title: 'foo'} }
subject(:stuff) { Stuff.new(attrs) }

describe "#file=" do
let(:uploaded_file) { double('uploaded_file') }
Expand Down Expand Up @@ -34,4 +35,20 @@
expect( stuff.filepath ).to eq '/tmp/a.jpg'
end
end

describe "#expired?" do
subject { stuff.expired? }
before { stuff.save }

it { should eq false }

context "when expired" do
let(:attrs) { {title: 'foo', expires_at: 1.day.ago} }
it { should eq true }
end
end

it "assigns expires_at" do
expect(stuff.tap(&:save).expires_at).to be_a_kind_of(Time)
end
end

0 comments on commit 85b780b

Please sign in to comment.