mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-16 15:20:13 +01:00
store rendered notes of todos in database to avoid costly rendering when you load a page
This commit is contained in:
parent
f32e5b256b
commit
bb8b5a4c72
6 changed files with 81 additions and 27 deletions
|
|
@ -143,14 +143,6 @@ class ApplicationController < ActionController::Base
|
|||
return json_elems
|
||||
end
|
||||
|
||||
# Uses RedCloth to transform text using either Textile or Markdown Need to
|
||||
# require redcloth above RedCloth 3.0 or greater is needed to use Markdown,
|
||||
# otherwise it only handles Textile
|
||||
#
|
||||
def markdown(text)
|
||||
RedCloth.new(text).to_html
|
||||
end
|
||||
|
||||
# Here's the concept behind this "mobile content negotiation" hack: In
|
||||
# addition to the main, AJAXy Web UI, Tracks has a lightweight low-feature
|
||||
# 'mobile' version designed to be suitablef or use from a phone or PDA. It
|
||||
|
|
|
|||
|
|
@ -189,24 +189,6 @@ module ApplicationHelper
|
|||
end
|
||||
end
|
||||
|
||||
AUTO_LINK_MESSAGE_RE = %r{message://<[^>]+>} unless const_defined?(:AUTO_LINK_MESSAGE_RE)
|
||||
|
||||
# Converts message:// links to href. This URL scheme is used on Mac OS X
|
||||
# to link to a mail message in Mail.app.
|
||||
def auto_link_message(text)
|
||||
text.gsub(AUTO_LINK_MESSAGE_RE) do
|
||||
href = $&
|
||||
left, right = $`, $'
|
||||
# detect already linked URLs and URLs in the middle of a tag
|
||||
if left =~ /<[^>]+$/ && right =~ /^[^>]*>/
|
||||
# do not change string; URL is alreay linked
|
||||
href
|
||||
else
|
||||
content = content_tag(:a, h(href), :href => h(href))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def format_note(note)
|
||||
note = auto_link_message(note)
|
||||
note = markdown(note)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ module TodosHelper
|
|||
|
||||
def collapsed_notes_image(todo)
|
||||
link = link_to(image_tag( 'blank.png', :width=>'16', :height=>'16', :border=>'0' ), "#", {:class => 'show_notes', :title => 'Show notes'})
|
||||
notes = content_tag(:div, {:class => "todo_notes", :id => dom_id(todo, 'notes'), :style => "display:none"}) { format_note(todo.notes) }
|
||||
notes = content_tag(:div, {:class => "todo_notes", :id => dom_id(todo, 'notes'), :style => "display:none"}) { todo.rendered_notes }
|
||||
return link+notes
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
class Todo < ActiveRecord::Base
|
||||
|
||||
before_save :render_note
|
||||
after_save :save_predecessors
|
||||
|
||||
# associations
|
||||
|
|
@ -396,4 +397,12 @@ class Todo < ActiveRecord::Base
|
|||
return todo
|
||||
end
|
||||
|
||||
def render_note
|
||||
unless notes.nil?
|
||||
rendered_notes = Tracks::Utils.render_text(notes)
|
||||
else
|
||||
rendered_notes = nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
26
db/migrate/20120412072508_add_rendered_notes.rb
Normal file
26
db/migrate/20120412072508_add_rendered_notes.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
class AddRenderedNotes < ActiveRecord::Migration
|
||||
def self.up
|
||||
add_column :todos, 'rendered_notes', :text
|
||||
|
||||
puts "-- Clearing show_from dates from completed todos"
|
||||
# clear up completed todos that have show_from set. These could have been left over from before the AASM migration
|
||||
Todo.completed.find(:all, :conditions =>[ "NOT(show_from IS NULL)"]).each {|t| t.show_from=nil; t.save!}
|
||||
puts "-- Generating new column values from notes. This may take a while."
|
||||
# Call save! on each todo to force generation of rendered_todos
|
||||
i=0; max = Todo.all.count; start = Time.now
|
||||
Todo.all.each do |todo|
|
||||
todo.save(false)
|
||||
i = i + 1
|
||||
if i%250==0
|
||||
elapsed_sec = (Time.now-start)
|
||||
remaining = (elapsed_sec / i)*(max-i)
|
||||
puts "Progress: #{i} / #{max} (#{(i.to_f/max.to_f*100.0).floor}%) ETA=#{remaining.floor}s"
|
||||
end
|
||||
end
|
||||
puts "Done: #{i} / #{max}"
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_column :todos, 'rendered_notes'
|
||||
end
|
||||
end
|
||||
45
lib/tracks/utils.rb
Normal file
45
lib/tracks/utils.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
module Tracks
|
||||
|
||||
class Utils
|
||||
AUTO_LINK_MESSAGE_RE = %r{message://<[^>]+>} unless const_defined?(:AUTO_LINK_MESSAGE_RE)
|
||||
|
||||
# Converts message:// links to href. This URL scheme is used on Mac OS X
|
||||
# to link to a mail message in Mail.app.
|
||||
def self.auto_link_message(text)
|
||||
text.gsub(AUTO_LINK_MESSAGE_RE) do
|
||||
href = $&
|
||||
left, right = $`, $'
|
||||
# detect already linked URLs and URLs in the middle of a tag
|
||||
if left =~ /<[^>]+$/ && right =~ /^[^>]*>/
|
||||
# do not change string; URL is alreay linked
|
||||
href
|
||||
else
|
||||
content = content_tag(:a, h(href), :href => h(href))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.render_text(text)
|
||||
rendered = Tracks::Utils.auto_link_message(text)
|
||||
rendered = markdown(rendered)
|
||||
rendered = ActionController::Base.helpers.auto_link(rendered, :link => :urls)
|
||||
|
||||
# add onenote and message protocols
|
||||
Sanitize::Config::RELAXED[:protocols]['a']['href'] << 'onenote'
|
||||
Sanitize::Config::RELAXED[:protocols]['a']['href'] << 'message'
|
||||
|
||||
rendered = Sanitize.clean(rendered, Sanitize::Config::RELAXED)
|
||||
return rendered
|
||||
end
|
||||
|
||||
# Uses RedCloth to transform text using either Textile or Markdown Need to
|
||||
# require redcloth above RedCloth 3.0 or greater is needed to use Markdown,
|
||||
# otherwise it only handles Textile
|
||||
#
|
||||
def self.markdown(text)
|
||||
RedCloth.new(text).to_html
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue