forked from robbin/robbin_site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.rb
90 lines (73 loc) · 2.72 KB
/
blog.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Blog < ActiveRecord::Base
acts_as_cached(:version => 1, :expires_in => 1.week)
acts_as_taggable
attr_protected :account_id, :blog_content_id
after_save :clean_cache
before_destroy :clean_cache
belongs_to :blog_content, :dependent => :destroy
belongs_to :account, :counter_cache => true
has_many :comments, :class_name => 'BlogComment', :dependent => :destroy
has_many :attachments, :dependent => :destroy
validates :title, :presence => true
validates :title, :length => {:in => 3..50}
delegate :content, :to => :blog_content, :allow_nil => true
# virtual property for setting tag_list
def user_tags
self.tag_list.join(" , ")
end
def user_tags=(tags)
unless tags.blank?
# filter illegal characters
self.tag_list = tags.split(/\s*,\s*/).uniq.collect {|t| t.downcase}.select {|t| t =~ /^(?!_)(?!.*?_$)[\+#a-zA-Z0-9_\s\u4e00-\u9fa5]+$/}.join(",")
end
end
# virtual property for blog_content's content body
def content=(value) # must prepend self otherwise do not update blog_content
self.blog_content ||= BlogContent.new
self.blog_content.content = value
self.content_updated_at = Time.now
end
def update_blog(param_hash)
self.transaction do
update_attributes!(param_hash)
blog_content.save!
save!
end
rescue
return false
end
def attach!(owner)
self.transaction do
owner.attachments.orphan.each {|attachment| attachment.update_attribute(:blog_id, self.id) }
end
end
# blog viewer hit counter
def increment_view_count
increment(:view_count) # add view_count += 1
write_second_level_cache # update cache per hit, but do not touch db
# update db per 10 hits
self.class.update_all({:view_count => view_count}, :id => id) if view_count % 10 == 0
end
def cached_tags
cached_tag_list ? cached_tag_list.split(/\s*,\s*/) : []
end
def clean_cache
APP_CACHE.delete("#{CACHE_PREFIX}/blog_tags/tag_cloud") # clean tag_cloud
APP_CACHE.delete("#{CACHE_PREFIX}/rss/all") # clean rss cache
APP_CACHE.delete("#{CACHE_PREFIX}/layout/right") # clean layout right column cache in _right.erb
end
def content_cache_key
"#{CACHE_PREFIX}/blog_content/#{self.id}/#{content_updated_at.to_i}"
end
def md_content # cached markdown format blog content
APP_CACHE.fetch(content_cache_key) { GitHub::Markdown.to_html(content, :gfm) }
end
def self.cached_tag_cloud
APP_CACHE.fetch("#{CACHE_PREFIX}/blog_tags/tag_cloud") do
self.tag_counts.sort_by(&:count).reverse
end
end
def self.hot_blogs(count)
self.order('comments_count DESC, view_count DESC').limit(count)
end
end