Skip to content

Commit

Permalink
Better handle content type errors. Closes #70.
Browse files Browse the repository at this point in the history
  • Loading branch information
bamnet committed Apr 30, 2012
1 parent 7f6e694 commit 083e69a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 11 additions & 6 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ def show
# If the object isn't valid (FooBar) or isn't a
# child of Content (Feed) a 400 error is thrown.
def new
#The default content type is defined in the Configuration model as default_upload_type
if params[:type].nil? && ConcertoConfig[:default_upload_type] != false
@content_const = ConcertoConfig[:default_upload_type].camelize.constantize
# We might already have a content type,
if @content_const.nil? || @content_const.superclass != Content
default_upload_type = ConcertoConfig[:default_upload_type]
if !default_upload_type
raise "Missing Default Content Type"
else
@content_const = default_upload_type.camelize.constantize
end
end

#We don't recognize the content type, or
#its not a child of Content.
# We don't recognize the requested content type, or
# its not a child of Content so we'll return a 400.
if @content_const.nil? || @content_const.superclass != Content
render :nothing => true, :status => 400
render :text => "Unrecognized content type.", :status => 400
else

@content = @content_const.new()
Expand Down
17 changes: 17 additions & 0 deletions test/functional/contents_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ def setup
assert_select("textarea")
end

test "should fallback to generic" do
sign_in users(:katie)
get(:new, {:type => "bananas"})
assert_response :success
assert_select(HTML::Selector.new "input[type=file]")
end

test "broken default type raises exception" do
sign_in users(:katie)
default = ConcertoConfig.find_by_key("default_upload_type")
default.delete

assert_raise RuntimeError do
get :new
end
end

test "should get index" do
get :index
assert_response :success
Expand Down

0 comments on commit 083e69a

Please sign in to comment.