Skip to content

Commit

Permalink
vertical partition post body
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed May 15, 2011
1 parent 006ff0b commit ac7d25e
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def new
else
@post = Post.new
end
@post.build_post_body
end

def show
Expand Down
9 changes: 7 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@

class Post < ActiveRecord::Base

include Markdownable
include UserOwnable
include Voteable
include Commentable

acts_as_taggable

validates_presence_of :title, :body
has_one :post_body

validates_presence_of :title
validates_uniqueness_of :title

scope :implemented, where(:implemented => true)
scope :published, where(:published => true)

after_create :notify_admin

accepts_nested_attributes_for :post_body

paginates_per 10

delegate :body, :formatted_html, :to => :post_body

define_index do
indexes :title, :description, :body

Expand Down
7 changes: 7 additions & 0 deletions app/models/post_body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PostBody < ActiveRecord::Base
include Markdownable

belongs_to :post

validates_presence_of :body
end
3 changes: 2 additions & 1 deletion app/views/posts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
= f.input :title, :label => 'Title'
= f.input :description, :label => 'Short Description', :hint => 'Please give a short description about this best practices', :input_html => { :class => 'wmd-ignore', :rows => 4 }
= f.input :tag_list, :hint => 'Split by comma ( , )'
= f.input :body, :label => 'Content'
= f.semantic_fields_for :post_body do |body_fields|
= body_fields.input :body, :label => 'Content'
21 changes: 21 additions & 0 deletions db/migrate/20110511145447_create_post_bodies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class CreatePostBodies < ActiveRecord::Migration
def self.up
create_table :post_bodies do |t|
t.text :body
t.text :formatted_html
t.integer :post_id

t.timestamps
end
ActiveRecord::Base.connection.execute("INSERT INTO post_bodies(body, formatted_html, post_id) SELECT body, formatted_html, id FROM posts")
remove_column :posts, :body
remove_column :posts, :formatted_html
end

def self.down
add_column :posts, :formatted_html, :text
add_column :posts, :body, :text
ActiveRecord::Base.connection.execute("UPDATE posts, post_bodies SET posts.body = post_bodies.body, posts.formatted_html = post_bodies.formatted_html WHERE posts.id = post_bodies.post_id")
drop_table :post_bodies
end
end
12 changes: 9 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110506140156) do
ActiveRecord::Schema.define(:version => 20110511145447) do

create_table "admin_users", :force => true do |t|
t.string "first_name", :default => "", :null => false
Expand Down Expand Up @@ -173,13 +173,19 @@
t.datetime "updated_at"
end

create_table "post_bodies", :force => true do |t|
t.text "body"
t.text "formatted_html"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "posts", :force => true do |t|
t.string "title"
t.text "body", :limit => 16777215
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.text "formatted_html", :limit => 16777215
t.text "description", :limit => 16777215
t.integer "comments_count", :default => 0
t.integer "vote_points", :default => 0
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/search_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Given /^the (\w+) indexes are processed$/ do |model|
model = model.titleize.gsub(/\s/, '').constantize
ThinkingSphinx::Test.index *model.sphinx_index_names
sleep(0.25)
sleep(2.00)
end
8 changes: 4 additions & 4 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ $(function() {
return false;
});

if ($('#post_body').length > 0) {
$('#post_body').before("<div id='wmd-button-bar'></div>");
$('#post_body').after("<div id='wmd-preview' class='wikistyle'></div>");
if ($('#post_post_body_attributes_body').length > 0) {
$('#post_post_body_attributes_body').before("<div id='wmd-button-bar'></div>");
$('#post_post_body_attributes_body').after("<div id='wmd-preview' class='wikistyle'></div>");
setup_wmd({
input: "post_body",
input: "post_post_body_attributes_body",
button_bar: "wmd-button-bar",
preview: "wmd-preview"
});
Expand Down
12 changes: 10 additions & 2 deletions spec/factories/posts.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
Factory.define :post_body do |pb|
pb.body "subject\n=======\ntitle\n-----"
end

Factory.define :code_post_body, :parent => :post_body do |pb|
pb.body "subject\n=======\ntitle\n-----\n def test\n puts 'test'\n end"
end

Factory.define :post do |p|
p.sequence(:title) {|n| "Post #{n}" }
p.body "subject\n=======\ntitle\n-----"
p.association :user
p.published true
p.association :post_body
end

Factory.define :code_post, :parent => :post do |p|
p.sequence(:title) {|n| "Code Post #{n}" }
p.body "subject\n=======\ntitle\n-----\n def test\n puts 'test'\n end"
p.association :post_body, :factory => :code_post_body
end
5 changes: 5 additions & 0 deletions spec/models/post_body_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe PostBody do
pending "add some examples to (or delete) #{__FILE__}"
end
2 changes: 1 addition & 1 deletion spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
end

should_validate_presence_of :body
should_validate_presence_of :title

describe 'when title validation is required' do
before { Factory.create(:post) }
Expand Down
4 changes: 2 additions & 2 deletions spec/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def should_be_markdownable(factory_id = nil)
it "should generate simple markdown html" do
raw = "subject\n=======\ntitle\n-----"
formatted = "<h1>subject</h1>\n\n<h2>title</h2>\n"
Factory(factory_id, :body => raw).formatted_html.should == formatted
Factory(factory_id, :post_body => Factory(:post_body, :body => raw)).formatted_html.should == formatted
end

it "should generate markdown html with <pre><code>" do
raw = "subject\n=======\ntitle\n-----\n def test\n puts 'test'\n end"
formatted = "<h1>subject</h1>\n\n<h2>title</h2>\n\n<pre><code>def test\n puts 'test'\nend\n</code></pre>\n"
Factory(factory_id, :body => raw).formatted_html.should == formatted
Factory(factory_id, :post_body => Factory(:post_body, :body => raw)).formatted_html.should == formatted
end

end
Expand Down

0 comments on commit ac7d25e

Please sign in to comment.