Permalink
Browse files

Parrot caches user data in comments table.

  • Loading branch information...
1 parent 50da258 commit cc66aecccaea204bdfff8bfb3be6e3d679712a8a @tute committed May 7, 2012
Showing with 14 additions and 1 deletion.
  1. +11 −1 app/models/parrot/comment.rb
  2. +3 −0 db/migrate/20120429124205_create_parrot_comments.rb
@@ -5,6 +5,8 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
belongs_to :author, :class_name => Parrot.author_class
+ before_save :cache_author_data
+
attr_accessible :commentable, :body
validates_presence_of :commentable, :body, :author_id
@@ -14,7 +16,15 @@ def self.following(other_comment)
end
def author
- Parrot.author_class.find(author_id)
+ Parrot.author_class.find_by_id author_id
+ end
+
+ # An author may delete it's account, we cache/store it's sensitive data
+ def cache_author_data
+ return if author.nil?
+ [:name, :email, :phone].each do |method|
+ self.send(:"author_#{method}=", author.send(method)) if author.respond_to?(method)
+ end
end
end
end
@@ -2,6 +2,9 @@ class CreateParrotComments < ActiveRecord::Migration
def change
create_table :parrot_comments do |t|
t.integer :author_id
+ t.string :author_name
+ t.string :author_email
+ t.string :author_phone
t.string :commentable_type
t.integer :commentable_id
t.text :body

0 comments on commit cc66aec

Please sign in to comment.