diff --git a/app/models/comment.rb b/app/models/comment.rb index 3a94c1f..ff30ca7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -116,10 +116,15 @@ class Comment < ActiveRecord::Base self.upvotes - self.downvotes end - def linkified_comment + def generated_markeddown_comment RDiscount.new(self.comment, :smart, :autolink, :safelink, :filter_html).to_html end + + def comment=(com) + self[:comment] = com.to_s.strip + self.markeddown_comment = self.generated_markeddown_comment + end def plaintext_comment # TODO: linkify then strip tags and convert entities back @@ -143,7 +148,8 @@ class Comment < ActiveRecord::Base cs = [ "story_id = ?", story_id ] end - Comment.find(:all, :conditions => cs, :order => "confidence").each do |c| + Comment.find(:all, :conditions => cs, :order => "confidence", + :include => :user).each do |c| (parents[c.parent_comment_id.to_i] ||= []).push c end diff --git a/app/models/story.rb b/app/models/story.rb index 3d45cc1..e7da941 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -156,11 +156,16 @@ class Story < ActiveRecord::Base return -(order + (sign * (seconds.to_f / 45000))).round(7) end - def linkified_text + def generated_markeddown_description RDiscount.new(self.description.to_s, :smart, :autolink, :safelink, :filter_html).to_html end + def description=(desc) + self[:description] = desc.to_s.strip + self.markeddown_description = self.generated_markeddown_description + end + def tags_a tags.map{|t| t.tag } end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 8b45eb0..5f834ab 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -59,7 +59,7 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ? <% end %>
- <%= raw comment.linkified_comment %> + <%= raw comment.markeddown_comment %>
diff --git a/app/views/home/rss.erb b/app/views/home/rss.erb index 32a3e8e..09a26b9 100644 --- a/app/views/home/rss.erb +++ b/app/views/home/rss.erb @@ -14,9 +14,9 @@ <%= story.user.username %> <%= story.created_at.rfc2822 %> <%= story.comments_url %> - <% if story.description.present? %> - <%= raw coder.encode(story.linkified_text, :decimal) - %> + <% if story.markeddown_description.present? %> + <%= raw coder.encode(story.markeddown_description, + :decimal) %> <% end %> <% story.taggings.each do |tagging| %> <%= tagging.tag.tag %> diff --git a/app/views/stories/show.html.erb b/app/views/stories/show.html.erb index 49dc6dc..60332e0 100644 --- a/app/views/stories/show.html.erb +++ b/app/views/stories/show.html.erb @@ -4,9 +4,9 @@
- <% if @story.description.present? %> + <% if @story.markeddown_description.present? %>
- <%= raw @story.linkified_text %> + <%= raw @story.markeddown_description %>
<% end %> diff --git a/db/migrate/20120705145520_move_markdown_into_sql.rb b/db/migrate/20120705145520_move_markdown_into_sql.rb new file mode 100644 index 0000000..6cbd3e7 --- /dev/null +++ b/db/migrate/20120705145520_move_markdown_into_sql.rb @@ -0,0 +1,23 @@ +class MoveMarkdownIntoSql < ActiveRecord::Migration + def up + add_column :comments, :markeddown_comment, :text + add_column :stories, :markeddown_description, :text + + Comment.all.each do |c| + c.markeddown_comment = c.generated_markeddown_comment + Comment.record_timestamps = false + c.save(:validate => false) + end + + Story.all.each do |s| + if s.description.present? + s.markeddown_description = s.generated_markeddown_description + Story.record_timestamps = false + s.save(:validate => false) + end + end + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index 0166a7a..970429d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,20 +11,21 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120704025956) do +ActiveRecord::Schema.define(:version => 20120705145520) do create_table "comments", :force => true do |t| t.datetime "created_at", :null => false t.datetime "updated_at" - t.string "short_id", :limit => 10, :default => "", :null => false + t.string "short_id", :limit => 10, :default => "", :null => false t.integer "story_id", :null => false t.integer "user_id", :null => false t.integer "parent_comment_id" t.integer "thread_id" t.text "comment", :null => false - t.integer "upvotes", :default => 0, :null => false - t.integer "downvotes", :default => 0, :null => false - t.decimal "confidence", :precision => 20, :scale => 19, :default => 0.0, :null => false + t.integer "upvotes", :default => 0, :null => false + t.integer "downvotes", :default => 0, :null => false + t.decimal "confidence", :precision => 20, :scale => 17 + t.text "markeddown_comment" end add_index "comments", ["confidence"], :name => "confidence_idx" @@ -65,15 +66,16 @@ ActiveRecord::Schema.define(:version => 20120704025956) do create_table "stories", :force => true do |t| t.datetime "created_at" t.integer "user_id" - t.string "url", :limit => 250, :default => "" - t.string "title", :limit => 150, :default => "", :null => false + t.string "url", :limit => 250, :default => "" + t.string "title", :limit => 150, :default => "", :null => false t.text "description" - t.string "short_id", :limit => 6, :default => "", :null => false - t.integer "is_expired", :limit => 1, :default => 0, :null => false - t.integer "upvotes", :default => 0, :null => false - t.integer "downvotes", :default => 0, :null => false - t.integer "is_moderated", :limit => 1, :default => 0, :null => false - t.decimal "hotness", :precision => 20, :scale => 10, :default => 0.0, :null => false + t.string "short_id", :limit => 6, :default => "", :null => false + t.integer "is_expired", :limit => 1, :default => 0, :null => false + t.integer "upvotes", :default => 0, :null => false + t.integer "downvotes", :default => 0, :null => false + t.integer "is_moderated", :limit => 1, :default => 0, :null => false + t.decimal "hotness", :precision => 20, :scale => 10 + t.text "markeddown_description" end add_index "stories", ["hotness"], :name => "hotness_idx"