Skip to content

Commit

Permalink
Fixed issues previewing article drafts
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2206 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Sep 18, 2006
1 parent ca0a4b4 commit 940954d
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 70 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
* SVN *

* Fixed issues previewing article drafts

# Added new liquid vars {{ site.latest_articles }} and {{ site.latest_comments }},
as well as filters like {{ site | latest_articles: 5 }} or {{ site | latest_comments: 5 }}

Expand Down
6 changes: 3 additions & 3 deletions app/drops/article_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def article() @source end
def initialize(source, options = {})
@options = options
@source = source
@site = options.delete(:site)
@site = options.delete(:site) || @source.site
@article_liquid = {
'id' => @source.id,
'title' => @source.title,
'permalink' => @source.permalink,
'body' => @source.body_html,
'excerpt' => @source.excerpt_html,
'published_at' => (@site.timezone.utc_to_local(@source.published_at) rescue nil),
'updated_at' => (@site.timezone.utc_to_local(@source.updated_at) rescue nil),
'published_at' => (@source.published_at ? @site.timezone.utc_to_local(@source.published_at) : nil),
'updated_at' => (@source.updated_at ? @site.timezone.utc_to_local(@source.updated_at) : nil),
'comments_count' => @source.comments_count,
'author' => @source.user.to_liquid,
'accept_comments' => @source.accept_comments?,
Expand Down
1 change: 1 addition & 0 deletions app/filters/core_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def textilize(text)
end

def format_date(date, format, ordinalized = false)
return '' if date.nil?
if ordinalized
date ? date.to_time.to_ordinalized_s(format.to_sym) : nil
else
Expand Down
2 changes: 1 addition & 1 deletion app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def accept_comments?
end

def comments_expired_at
published_at + comment_age.days
(published_at || Time.now.utc) + comment_age.days
end

def set_filter_from(filtered_object)
Expand Down
4 changes: 4 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ def permalink_regex(refresh = false)
end

def permalink_for(article)
old_published = article.published_at
article.published_at ||= Time.now.utc
permalink_style.split('/').inject [''] do |s, piece|
s << (piece =~ PERMALINK_VAR && PERMALINK_OPTIONS.keys.include?($1) ? article.send($1).to_s : piece)
end.join('/')
ensure
article.published_at = old_published
end

def search_url(query, page = nil)
Expand Down
76 changes: 76 additions & 0 deletions test/functional/admin/articles_controller_assets_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/articles_controller'

# Re-raise errors caught by the controller.
class Admin::ArticlesController; def rescue_action(e) raise e end; end

class Admin::ArticlesControllerAssetsTest < Test::Unit::TestCase
fixtures :contents, :content_versions, :sections, :assigned_sections, :users, :sites, :tags, :taggings, :memberships

def setup
@controller = Admin::ArticlesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
login_as :quentin
FileUtils.mkdir_p ASSET_PATH
end

def test_should_upload_asset
asset_count = Object.const_defined?(:Magick) ? 3 : 1 # asset + 2 thumbnails

assert_difference Asset, :count, asset_count do
post :upload, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') }
assert_response :success
assert_template 'new'
end
end

def test_should_upload_asset_and_redirect_to_article
asset_count = Object.const_defined?(:Magick) ? 3 : 1 # asset + 2 thumbnails

assert_difference Asset, :count, asset_count do
post :upload, :id => contents(:welcome).id,
:asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') }
assert_response :success
assert_template 'edit'
assert_equal contents(:welcome), assigns(:article)
end
end

def test_should_not_error_on_new_article_asset_upload
assert_no_difference Asset, :count do
post :upload
assert_response :success
assert_template 'new'
end
end

def test_should_not_error_on_article_asset_upload
assert_no_difference Asset, :count do
post :upload, :id => contents(:welcome).id
assert_response :success
assert_template 'edit'
assert_equal contents(:welcome), assigns(:article)
end
end

def test_should_not_create_article_when_uploading_asset
Time.mock! Time.local(2005, 1, 1, 12, 0, 0) do
assert_no_difference Article, :count do
post :upload, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') },
:article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah",
'published_at(1i)' => '2005', 'published_at(2i)' => '1', 'published_at(3i)' => '1', 'published_at(4i)' => '10' }, :submit => :save
assert_response :success
assert_template 'new'
assert_valid assigns(:article)
assert assigns(:article).new_record?
assert_equal Time.local(2005, 1, 1, 9, 0, 0).utc, assigns(:article).published_at
assert_equal users(:quentin), assigns(:article).updater
end
end
end

def teardown
FileUtils.rm_rf ASSET_PATH
end
end
27 changes: 27 additions & 0 deletions test/functional/admin/articles_controller_preview_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/articles_controller'

# Re-raise errors caught by the controller.
class Admin::ArticlesController; def rescue_action(e) raise e end; end

class Admin::ArticlesControllerPreviewTest < Test::Unit::TestCase
fixtures :contents, :sections, :assigned_sections, :users, :sites
def setup
@controller = Admin::ArticlesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
login_as :quentin
prepare_theme_fixtures
end

def test_show_action_previews_article
get :show, :id => contents(:welcome).id
assert_response :success
end

def test_show_action_previews_article_draft
contents(:welcome).update_attribute :published_at, nil
get :show, :id => contents(:welcome).id
assert_response :success
end
end
65 changes: 0 additions & 65 deletions test/functional/admin/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
login_as :quentin
FileUtils.mkdir_p ASSET_PATH
end

def test_should_require_login
Expand Down Expand Up @@ -171,11 +170,6 @@ def test_edit_form_should_have_correct_post_action
assert_tag :tag => 'form', :attributes => { :action => "/admin/articles/update/#{contents(:welcome).id}" }
end

def test_show_action_previews_article
get :show, :id => contents(:welcome).id
assert_response :success
end

def test_should_update_article_with_correct_time
Time.mock! Time.local(2005, 1, 1, 12, 0, 0) do
post :update, :id => contents(:welcome).id, :article => { 'published_at(1i)' => '2005', 'published_at(2i)' => '1', 'published_at(3i)' => '1', 'published_at(4i)' => '10' }
Expand Down Expand Up @@ -291,65 +285,6 @@ def test_should_save_article_without_revision
post :update, :id => contents(:welcome).id, :article => { :title => 'Foo' }, :commit => 'Save without Revision'
end
end

def test_should_upload_asset
asset_count = Object.const_defined?(:Magick) ? 3 : 1 # asset + 2 thumbnails

assert_difference Asset, :count, asset_count do
post :upload, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') }
assert_response :success
assert_template 'new'
end
end

def test_should_upload_asset_and_redirect_to_article
asset_count = Object.const_defined?(:Magick) ? 3 : 1 # asset + 2 thumbnails

assert_difference Asset, :count, asset_count do
post :upload, :id => contents(:welcome).id,
:asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') }
assert_response :success
assert_template 'edit'
assert_equal contents(:welcome), assigns(:article)
end
end

def test_should_not_error_on_new_article_asset_upload
assert_no_difference Asset, :count do
post :upload
assert_response :success
assert_template 'new'
end
end

def test_should_not_error_on_article_asset_upload
assert_no_difference Asset, :count do
post :upload, :id => contents(:welcome).id
assert_response :success
assert_template 'edit'
assert_equal contents(:welcome), assigns(:article)
end
end

def test_should_not_create_article_when_uploading_asset
Time.mock! Time.local(2005, 1, 1, 12, 0, 0) do
assert_no_difference Article, :count do
post :upload, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') },
:article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah",
'published_at(1i)' => '2005', 'published_at(2i)' => '1', 'published_at(3i)' => '1', 'published_at(4i)' => '10' }, :submit => :save
assert_response :success
assert_template 'new'
assert_valid assigns(:article)
assert assigns(:article).new_record?
assert_equal Time.local(2005, 1, 1, 9, 0, 0).utc, assigns(:article).published_at
assert_equal users(:quentin), assigns(:article).updater
end
end
end

def teardown
FileUtils.rm_rf ASSET_PATH
end

protected
def assert_draft_check_box(visibility = true)
Expand Down
5 changes: 5 additions & 0 deletions test/unit/article_drop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def test_empty_body
assert_equal '', a.send(:body_for_mode, :list)
end

def test_should_use_nil_published_date_for_draft
contents(:welcome).published_at = nil
assert_nil contents(:welcome).to_liquid['published_at']
end

def test_body_with_excerpt
assert contents(:welcome).update_attributes(:body => 'body', :excerpt => 'excerpt'), contents(:welcome).errors.full_messages.to_sentence
a = contents(:welcome).to_liquid
Expand Down
6 changes: 6 additions & 0 deletions test/unit/article_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def test_should_not_create_article_version_for_useless_changes
assert !contents(:welcome).save_version?
end

def test_comment_expiration_date_on_draft
a = create_fake_article
a.published_at = nil
assert_equal 10.days.from_now.utc.to_i, a.comments_expired_at.to_i
end

def test_comment_expiration_date
a = create_fake_article
assert_equal 5.days.from_now.utc.to_i, a.comments_expired_at.to_i
Expand Down
8 changes: 7 additions & 1 deletion test/unit/site_permalink_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ def setup
specify "should generate correct permalink format" do
assert_equal "/#{@article.year}/#{@article.month}/#{@article.day}/#{@article.permalink}", @site.permalink_for(@article)
end


specify "should generate correct permalink format for draft" do
@article.published_at = nil
now = Time.now.utc
assert_equal "/#{now.year}/#{now.month}/#{now.day}/#{@article.permalink}", @site.permalink_for(@article)
end

specify "should generate custom id permalink" do
@site.permalink_style = 'posts/:year/:id'
assert_valid @site
Expand Down

0 comments on commit 940954d

Please sign in to comment.