Skip to content

Commit

Permalink
added 'format' attribute to Post. by default, we still apply RedCloth…
Browse files Browse the repository at this point in the history
… formatting. but if format is HTML, serve raw content.

note that at this point, no code sets format (yet).
  • Loading branch information
flavorjones committed Oct 29, 2008
1 parent db842bb commit 3a058a3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/models/post.rb
Expand Up @@ -47,8 +47,10 @@ def link(root='')
end

def to_html
text = content
text = RedCloth.new(text, [:filter_styles, :no_span_caps]).to_html
text = case format
when 'HTML' then content
else RedCloth.new(content, [:filter_styles, :no_span_caps]).to_html
end
end

end
8 changes: 8 additions & 0 deletions db/migrate/007_add_post_format.rb
@@ -0,0 +1,8 @@
class AddPostFormat < ActiveRecord::Migration
def self.up
add_column :posts, :format, :string
end
def self.down
remove_column :posts, :format
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 6) do
ActiveRecord::Schema.define(:version => 7) do

create_table "comments", :force => true do |t|
t.integer "commentable_id"
Expand Down Expand Up @@ -51,6 +51,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "deleted_at"
t.string "format"
end

create_table "users", :force => true do |t|
Expand Down
7 changes: 6 additions & 1 deletion test/unit/post_test.rb
Expand Up @@ -47,7 +47,7 @@ def test_format_html
that I found</a> or is it
really a problem?
EOF
assert post = create_post(:content => content)
assert post = create_post(:content => content, :format => 'HTML')
assert h = Hpricot.parse(post.to_html)
assert_equal 0, h.search('br').size # when we try to redclothify html, this fails.
end
Expand All @@ -64,6 +64,11 @@ def test_format_redcloth
assert h = Hpricot.parse(post.to_html)
assert_equal 1, h.search('h1').size
assert_equal 2, h.search('p').size

assert post = create_post(:content => content, :format => 'RedCloth')
assert h = Hpricot.parse(post.to_html)
assert_equal 1, h.search('h1').size
assert_equal 2, h.search('p').size
end

protected
Expand Down

0 comments on commit 3a058a3

Please sign in to comment.