Skip to content

Commit

Permalink
add statuses destroy logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerei committed Jun 17, 2011
1 parent 3b66d32 commit 4e95873
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
5 changes: 3 additions & 2 deletions app/models/reply.rb
Expand Up @@ -3,6 +3,7 @@ class Reply
include Mongoid::Timestamps

field :content
has_many :statuses, :validate => false, :class_name => 'Status::Base', :dependent => :delete

has_and_belongs_to_many :mention_users, :class_name => 'User', :validate => false
belongs_to :topic
Expand All @@ -13,7 +14,7 @@ class Reply
attr_accessible :content

after_create :increment_topic_reply_cache, :add_replier_ids
after_destroy :decrement_topic_reply_cache
before_destroy :decrement_topic_reply_cache
before_save :extract_mentions
after_create :send_menetion_notifications, :create_status

Expand All @@ -26,7 +27,7 @@ def increment_topic_reply_cache

def decrement_topic_reply_cache
# ignore user and time cache
topic.inc :replies_count, -1
topic.inc :replies_count, -1 if topic && !topic.frozen?
end

def add_replier_ids
Expand Down
5 changes: 5 additions & 0 deletions app/models/stream.rb
Expand Up @@ -21,6 +21,11 @@ def push_status(status)
$redis.ltrim store_key, 0, Stream.status_limit - 1
end

# slow than fetch_statuses, but complete than fetch_statuses
def statuses
Status::Base.where(:_id.in => status_ids).desc(:created_at)
end

def fetch_statuses(options = {})
page = (options[:page] || 1).to_i
per_page = (options[:per_page] || 20).to_i
Expand Down
3 changes: 2 additions & 1 deletion app/models/topic.rb
Expand Up @@ -13,7 +13,8 @@ class Topic
scope :marked_by, lambda { |user| where(:marker_ids => user.id) }
scope :replied_by, lambda { |user| where(:replier_ids => user.id) }

has_many :replies, :validate => false, :dependent => :delete
has_many :replies, :validate => false, :dependent => :destroy
has_many :statuses, :validate => false, :class_name => 'Status::Base', :dependent => :delete
belongs_to :user
belongs_to :last_replied_by, :class_name => 'User'
field :actived_at, :type => Time
Expand Down
7 changes: 3 additions & 4 deletions app/models/user.rb
Expand Up @@ -118,10 +118,9 @@ def unban!
end

def clean!
self.replies.delete_all
topic_ids = self.topics.only(:id).to_a.map{|topic| topic.id}
Reply.delete_all :conditions => {:topic_id => {"$in" => topic_ids}}
self.topics.delete_all
self.replies.destroy
self.topics.destroy
self.statuses.delete_all
end

def self.authenticate(login, password)
Expand Down
22 changes: 7 additions & 15 deletions test/unit/user_test.rb
Expand Up @@ -108,6 +108,7 @@ def test_clean

def test_destroy
make_content

assert_difference "Topic.count", -1 do
assert_difference "Reply.count", -3 do
@user.destroy
Expand All @@ -116,21 +117,12 @@ def test_destroy
end

def make_content
t = @user.topics.create :title => 'title', :content => 'content', :tags => 'tag1 tag2'
r = t.replies.new :content => 'content'
r.user = @admin
r.save
r = t.replies.new :content => 'content'
r.user = @user
r.save

t = @admin.topics.create :title => 'title', :content => 'content', :tags => 'tag1 tag2'
r = t.replies.new :content => 'content'
r.user = @admin
r.save
r = t.replies.new :content => 'content'
r.user = @user
r.save
topic = Factory :topic, :user => @user
Factory :reply, :topic => topic
Factory :reply, :topic => topic, :user => @user
topic2 = Factory :topic
Factory :reply, :topic => topic2
Factory :reply, :topic => topic2, :user => @user
end

def test_remember_token
Expand Down

0 comments on commit 4e95873

Please sign in to comment.