Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding support for cached html in article

  • Loading branch information...
commit 040d263e46ddfa45f6be8a779a36ce76bf354e4e 1 parent 53e6299
@akitaonrails authored
View
1  app/controllers/articles_controller.rb
@@ -1,6 +1,5 @@
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
- @body_html = RDiscount.new(@article.body || "").to_html.html_safe
end
end
View
12 app/models/article.rb
@@ -1,13 +1,15 @@
class Article < ActiveRecord::Base
- attr_accessible :body, :slug, :title, :locale, :translations_attributes
+ attr_accessible :body, :body_html, :slug, :title, :locale, :translations_attributes
extend FriendlyId
friendly_id :title, use: :slugged
- translates :title, :body
+ translates :title, :body, :body_html
accepts_nested_attributes_for :translations
+ before_save :generate_html
+
class Translation
- attr_accessible :locale, :title, :body
+ attr_accessible :locale, :title, :body, :body_html
end
private
@@ -15,4 +17,8 @@ class Translation
def should_generate_new_friendly_id?
new_record?
end
+
+ def generate_html
+ self.body_html = RDiscount.new(self.body).to_html
+ end
end
View
2  app/views/articles/show.html.erb
@@ -1,5 +1,5 @@
<h1><%= @article.title %></h1>
-<%= @body_html %>
+<%= raw @article.body_html %>
<%= language_links %>
View
5 db/migrate/20120714161124_add_html_cache_fields_to_article.rb
@@ -0,0 +1,5 @@
+class AddHtmlCacheFieldsToArticle < ActiveRecord::Migration
+ def change
+ add_column :article_translations, :body_html, :text
+ end
+end
View
3  db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120713204524) do
+ActiveRecord::Schema.define(:version => 20120714161124) do
create_table "active_admin_comments", :force => true do |t|
t.string "resource_id", :null => false
@@ -53,6 +53,7 @@
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.text "body_html"
end
add_index "article_translations", ["article_id"], :name => "index_article_translations_on_article_id"
Please sign in to comment.
Something went wrong with that request. Please try again.