Skip to content
Browse files

Use ActiveRecord instead of files

  • Loading branch information...
1 parent e07582f commit 85371bab1b002ff92bd10df36b27142030dbde21 @jferris jferris committed Jan 20, 2010
View
2 app/helpers/announcements_helper.rb
@@ -4,7 +4,7 @@ def announcement_hidden?(announcement)
end
def current_announcement
- @current_announcement ||= Announcement.new
+ @current_announcement ||= Announcement.current
end
end
View
17 app/models/announcement.rb
@@ -1,18 +1,9 @@
-class Announcement
-
- mattr_accessor :file_path
- attr_accessor :created_at, :body
-
- def initialize
- if File.readable?(Announcement.file_path)
- file = File.open(Announcement.file_path)
- @body = file.read
- @created_at = file.mtime.to_i unless body.blank?
- end
+class Announcement < ActiveRecord::Base
+ def self.current
+ first(:order => 'created_at DESC') || new
end
def exists?
- !created_at.nil?
+ !new_record?
end
-
end
View
9 generators/paul_revere/paul_revere_generator.rb
@@ -0,0 +1,9 @@
+class PaulRevereGenerator < Rails::Generator::Base
+ def manifest
+ record do |m|
+ m.migration_template "migration.rb",
+ 'db/migrate',
+ :migration_file_name => "create_announcements"
+ end
+ end
+end
View
12 generators/paul_revere/templates/migration.rb
@@ -0,0 +1,12 @@
+class CreateAnnouncements < ActiveRecord::Migration
+ def self.up
+ create_table :announcements do |t|
+ t.text :body
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :announcements
+ end
+end
View
9 shoulda_macros/announcement.rb
@@ -3,20 +3,15 @@ class ActiveSupport::TestCase
def self.with_current_announcement(&blk)
context "When there is a current announcement" do
setup do
- File.stubs(:readable?).with(Announcement.file_path).returns true
- file = mock
- file.stubs(:read).returns("Announcement Body")
- @mtime = Time.now
- file.stubs(:mtime).returns @mtime
- File.stubs(:open).with(Announcement.file_path).returns file
+ Annoucement.create!(:body => 'therefore,')
end
merge_block(&blk)
end
end
def self.should_display_current_announcement
should "have the current announcement in it" do
- assert_match Announcement.new.body, @email.body
+ assert_match Announcement.current.body, @email.body
end
end
View
78 test/announcement_test.rb
@@ -2,74 +2,20 @@
require File.join(File.dirname(__FILE__), '..', 'app', 'models', 'announcement')
class AnnouncementTest < ActiveSupport::TestCase
-
- context "with a fake file_path" do
- setup { Announcement.file_path = "/something/fake" }
+ should "return the latest announcement when there are several" do
+ old = Announcement.create!(:body => 'no fun', :created_at => 2.days.ago)
+ latest = Announcement.create!(:body => 'fun', :created_at => 1.day.ago)
+ older = Announcement.create!(:body => 'less fun', :created_at => 3.days.ago)
- context "An Announcment when there is an announcement" do
- setup do
- File.expects(:readable?).with(Announcement.file_path).returns true
- file = mock
- file.expects(:read).returns("Announcement Body")
- @mtime = Time.now
- file.expects(:mtime).returns @mtime
- File.expects(:open).with(Announcement.file_path).returns file
- @announcement = Announcement.new
- end
-
- should "return modification time went sent created_at" do
- assert_equal @mtime.to_i, @announcement.created_at
- end
-
- should "return announcement body when sent body" do
- assert_equal "Announcement Body", @announcement.body
- end
-
- should "return true when sent exists?" do
- assert @announcement.exists?
- end
- end
-
- context "An announcement when there is a blank announcement" do
- setup do
- File.expects(:readable?).with(Announcement.file_path).returns true
- file = mock
- file.expects(:read).returns("")
- File.expects(:open).with(Announcement.file_path).returns file
- @announcement = Announcement.new
- end
-
- should "return nil when sent created_at" do
- assert @announcement.created_at.nil?
- end
-
- should "return have a blank body" do
- assert @announcement.body.blank?
- end
-
- should "return false when sent exists?" do
- assert !@announcement.exists?
- end
- end
-
- context "An announcement when there is no announcement file" do
- setup do
- File.expects(:readable?).with(Announcement.file_path).returns false
- @announcement = Announcement.new
- end
-
- should "return nil when sent created_at" do
- assert @announcement.created_at.nil?
- end
-
- should "return have a blank body" do
- assert @announcement.body.blank?
- end
+ assert_equal latest, Announcement.current
+ end
- should "return false when sent exists?" do
- assert !@announcement.exists?
- end
- end
+ should "return an existent announcement where there is no announcement" do
+ Announcement.create!(:body => 'body')
+ assert Announcement.current.exists?
end
+ should "return a non-existent announcement where there is no announcement" do
+ assert ! Announcement.current.exists?
+ end
end
View
11 test/announcements_helper_test.rb
@@ -5,24 +5,21 @@
class AnnouncementsHelperTest < ActionView::TestCase
include AnnouncementsHelper
- should "return a new announcement when sent current_announcement and cache doesn't exist" do
+ should "return the current announcement when sent current_announcement and cache doesn't exist" do
@current_announcement = nil
- Announcement.stubs(:new).returns(:foo)
+ Announcement.stubs(:current).returns(:foo)
assert_equal :foo, current_announcement
end
should "return a cached announcement when sent current_announcement and cache exists" do
@current_announcement = :foo
- Announcement.expects(:new).never
+ Announcement.expects(:current).never
assert_equal :foo, current_announcement
end
context "when there is an announcement" do
setup do
- Announcement.file_path = "/somewhere/fake"
- @announcement = Announcement.new
- @announcement.stubs(:exists?).returns(true)
- @announcement.stubs(:created_at).returns(Time.now.to_i)
+ @announcement = Announcement.create!(:body => 'a body')
end
context "and the user has hidden an announcement" do
View
27 test/test_helper.rb
@@ -1,27 +1,46 @@
ENV['RAILS_ENV'] = 'test'
+PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+RAILS_ROOT = PROJECT_ROOT
+
require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
require 'shoulda'
require 'action_controller'
require 'action_view'
require 'action_view/test_case'
-require 'active_support'
-require 'active_support/test_case'
+require 'active_record'
require 'test_help'
require 'redgreen'
class ActionView::TestCase
class TestController < ActionController::Base
attr_accessor :request, :response, :params
-
+
def initialize
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
-
+
# TestCase doesn't have context of a current url so cheat a bit
@params = {}
send(:initialize_current_url)
end
end
end
+class ActiveSupport::TestCase
+ setup do
+ Announcement.delete_all
+ end
+end
+
+database_file = File.join(File.dirname(__FILE__), 'test.db')
+at_exit { FileUtils.rm(database_file) }
+
+load File.join(PROJECT_ROOT, 'generators', 'paul_revere', 'templates', 'migration.rb')
+ActiveRecord::Base.establish_connection(
+ :adapter => 'sqlite3',
+ :database => database_file
+)
+CreateAnnouncements.suppress_messages { CreateAnnouncements.migrate(:up) }

0 comments on commit 85371ba

Please sign in to comment.
Something went wrong with that request. Please try again.