move markeddown html into sql so it doesn't have to be generated every time

it's unlikely to change and if it does, we can just re-generate
everything in sql at once
This commit is contained in:
joshua stein 2012-07-05 10:08:14 -05:00
parent d170d36fd0
commit 342d6ff911
7 changed files with 58 additions and 22 deletions

View file

@ -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

View file

@ -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

View file

@ -59,7 +59,7 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ?
<% end %>
</div>
<div class="comment_text">
<%= raw comment.linkified_comment %>
<%= raw comment.markeddown_comment %>
</div>
<div class="comment_reply"></div>

View file

@ -14,9 +14,9 @@
<author><%= story.user.username %></author>
<pubDate><%= story.created_at.rfc2822 %></pubDate>
<comments><%= story.comments_url %></comments>
<% if story.description.present? %>
<description><%= raw coder.encode(story.linkified_text, :decimal)
%></description>
<% if story.markeddown_description.present? %>
<description><%= raw coder.encode(story.markeddown_description,
:decimal) %></description>
<% end %>
<% story.taggings.each do |tagging| %>
<category><%= tagging.tag.tag %></category>

View file

@ -4,9 +4,9 @@
</ol>
<div class="story_content">
<% if @story.description.present? %>
<% if @story.markeddown_description.present? %>
<div class="story_text">
<%= raw @story.linkified_text %>
<%= raw @story.markeddown_description %>
</div>
<% end %>

View file

@ -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

View file

@ -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"