Skip to content

Commit

Permalink
Patched issue with MySQL database errors during initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
peakpg committed Apr 29, 2011
1 parent c5eae35 commit 35a3d2c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 27 deletions.
9 changes: 9 additions & 0 deletions app/models/page_route.rb
Expand Up @@ -24,6 +24,15 @@ class PageRoute < ActiveRecord::Base

after_save :reload_routes


# Determines if its safe to call any persistent methods on PageRoutes. This can be false if either the database doesn't exist,
# or the page_routes table doesn't yet exist.
#
# @return [Boolean] Whether its safe to call any ActiveRecord persistent method or not.
def self.can_be_loaded?
database_exists? && table_exists?
end

# Force Rails to reload the routes. Allows modules to call this without concern that the Rails classes are going to change again.
def self.reload_routes
Rails.application.reload_routes!
Expand Down
23 changes: 22 additions & 1 deletion lib/cms/extensions/active_record/base.rb
@@ -1,6 +1,7 @@
module Cms
module Extensions
module ActiveRecord

module Base
def updated_on_string(fmt="%b %e, %Y")
if respond_to?(:updated_at) && updated_at
Expand All @@ -10,7 +11,27 @@ def updated_on_string(fmt="%b %e, %Y")
end
end
end

module ClassMethods
# Determines if the database for this Rails App exists yet. Useful for methods which might be called during
# rake tasks or initialize where a database not yet being created is not fatal, but should be ignored.
#
# @return [Boolean] false if it does not exist.
def database_exists?
begin
connection
return true
rescue StandardError # Hopefully this works with MySql, MySql2 and SQLite
logger.warn "Attempted to establish a connection with the database, but could not do so."
return false
end

end
end
end
end
end
ActiveRecord::Base.send(:include, Cms::Extensions::ActiveRecord::Base)
ActiveRecord::Base.send(:include, Cms::Extensions::ActiveRecord::Base)
ActiveRecord::Base.extend(Cms::Extensions::ActiveRecord::ClassMethods)


5 changes: 1 addition & 4 deletions lib/cms/routes.rb
Expand Up @@ -126,16 +126,13 @@ def routes_for_browser_cms
get 'cache', :to=>'cache#show', :as=>'cache'
delete 'cache', :to=>'cache#destroy'

# This is only for testing, and should be moved to the config/routes.rb file eventually.
# content_blocks :sample_blocks

match "/routes", :to => "routes#index", :as=>'routes'

end

# Loads all Cms PageRoutes from the database
# TODO: Needs a integration/functional level test to verify that a page route w/ constraints will be correctly mapped.
if PageRoute.table_exists?
if PageRoute.can_be_loaded?
PageRoute.all(:order => "page_routes.name").each do |r|
match r.pattern, :to=>r.to, :as=>r.route_name, :_page_route_id=>r.page_route_id, :via=>r.via, :constraints=>r.constraints
end
Expand Down
15 changes: 15 additions & 0 deletions test/unit/extensions/active_record/base_test.rb
Expand Up @@ -7,4 +7,19 @@ def test_updated_on_string
base.updated_at = Time.zone.parse("1978-07-06")
assert_equal "Jul 6, 1978", base.updated_on_string
end
end

# Must use vanilla TestCase to avoid ActiveRecord setup conflicts
class TestExtensions < Test::Unit::TestCase

def test_throws_error
#test "If a connection throws an error when established, then we consider the database to not exist." do
ActiveRecord::Base.expects(:connection).raises(StandardError)
assert_equal false, ActiveRecord::Base.database_exists?
end

def test_exists
#test "If we can establish a connection, the database exists" do
assert_equal true, ActiveRecord::Base.database_exists?
end
end
63 changes: 46 additions & 17 deletions test/unit/models/page_route_test.rb
Expand Up @@ -5,28 +5,29 @@ def setup
@route = PageRoute.new(:pattern=>"/:some/:pattern", :name=>"My Name")
end


def test_create
page = Factory(:page, :path => "/things/overview")
route = page.page_routes.build(:pattern => "/things/:year/:month/:day")
route.add_requirement(:year, "\\d{4,}")
route.add_requirement(:month, "\\d{2,}")
route.add_requirement(:day, "\\d{2,}")
route.add_condition(:method, "get")

assert route.save!
assert_equal "/things/:year/:month/:day", route.pattern
assert_equal({
:controller => "cms/content",
:action => "show_page_route",
:_page_route_id => route.id.to_s,
:requirements => {
:year => /\d{4,}/,
:month => /\d{2,}/,
:day => /\d{2,}/
}, :conditions => {
:method => :get
}
}, route.options_map)
assert_equal({
:controller => "cms/content",
:action => "show_page_route",
:_page_route_id => route.id.to_s,
:requirements => {
:year => /\d{4,}/,
:month => /\d{2,}/,
:day => /\d{2,}/
}, :conditions => {
:method => :get
}
}, route.options_map)


end
Expand All @@ -46,24 +47,52 @@ def test_create
end
test "setting method conditions" do
@route.add_condition(:method, "post")
assert_equal( [:post] , @route.via)
assert_equal([:post], @route.via)

end

test "constraints allows for regular expressions to be set for pattern elements in a route" do
@route.add_requirement(:year, '\d{4,}')
assert_equal({ :year => /\d{4,}/ }, @route.constraints)
assert_equal({:year => /\d{4,}/}, @route.constraints)
end

test "add_constraint is replacement method for add_requirement " do
@route.add_requirement(:year, '\d{4,}')
assert_equal({ :year => /\d{4,}/ }, @route.constraints)
assert_equal({:year => /\d{4,}/}, @route.constraints)
end

test "constraints handles more than one pattern" do
@route.add_requirement(:month, '\d{2,}')
@route.add_requirement(:day, '\d{2,}')
@route.add_requirement(:year, '\d{4,}')
assert_equal({:month => /\d{2,}/,:day => /\d{2,}/, :year => /\d{4,}/ }, @route.constraints)
assert_equal({:month => /\d{2,}/, :day => /\d{2,}/, :year => /\d{4,}/}, @route.constraints)
end
end


class LoadingPageRoutesTest < ActiveSupport::TestCase

def setup

end

test "can_be_loaded?" do
PageRoute.expects(:database_exists?).returns(true)
PageRoute.expects(:table_exists?).returns(true)

assert_equal true, PageRoute.can_be_loaded?
end

test "Routes cannot be loaded if the table doesn't exist" do
PageRoute.expects(:database_exists?).returns(true)
PageRoute.expects(:table_exists?).returns(false)

assert_equal false, PageRoute.can_be_loaded?
end

test "Routes cannot be loaded if the database doesn't exist" do
PageRoute.expects(:database_exists?).returns(false)

assert_equal false, PageRoute.can_be_loaded?
end
end
5 changes: 0 additions & 5 deletions todo_list.txt
Expand Up @@ -4,7 +4,6 @@ Start with:
Open Issues
===========
* SQLite3 #<ActiveRecord::StatementInvalid: Could not find table 'file_blocks'> warnings
* Mysql2 throws exceptions during generators (similar reason to previous)
* rake db:install - Get warnings about db already existing (with SQLite3)

Gems to Publish (once its live)
Expand All @@ -21,10 +20,6 @@ Future Things to do
* Add Block.publish and publish! for easier coding.
* Move 'datepicker' initialization into application.js
* Upgrade jquery.selectbox-0.5 to jquery.sb.js (https://github.com/revsystems/jQuery-SelectBox). This will likely improve the usability of the selectbox.
* Reduce need to developers to modify content-like files on server (CSS, JS, Images, Templates)
** Confirm w/ developers on what is commonly touched.
** Step 1: Make templates start on file system, appear in UI and then be 'forked' in UI. Avoids need to 'copy' into CMS and delete.
** Look at WebDAV / HTTPS protocol.
* What is the difference between application template and profiles?
* Clean up all 'old' initialization code (add_cms_routes_method, add to rails path, etc)
* Merge all existing migrations into core as well as seeds.rb. Rework how seeds gets run as part of the project. (Future proof this).
Expand Down

0 comments on commit 35a3d2c

Please sign in to comment.