Reuse post-analyzer and cooked-document when requesting multiple post stats
This commit is contained in:
parent
ad2b667504
commit
ebd5fa76c5
2 changed files with 8 additions and 10 deletions
app/models
|
@ -83,17 +83,18 @@ class Post < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_analyzer
|
def post_analyzer
|
||||||
@post_analyzer = PostAnalyzer.new(raw, topic_id)
|
@post_analyzers ||= {}
|
||||||
|
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
|
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
|
||||||
define_method(attr) do
|
define_method(attr) do
|
||||||
PostAnalyzer.new(raw, topic_id).send(attr)
|
post_analyzer.send(attr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cook(*args)
|
def cook(*args)
|
||||||
PostAnalyzer.new(raw, topic_id).cook(*args)
|
post_analyzer.cook(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
class PostAnalyzer
|
class PostAnalyzer
|
||||||
|
|
||||||
attr_accessor :cooked, :raw
|
|
||||||
|
|
||||||
def initialize(raw, topic_id)
|
def initialize(raw, topic_id)
|
||||||
@raw = raw
|
@raw = raw
|
||||||
@topic_id = topic_id
|
@topic_id = topic_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def cooked_document
|
|
||||||
@cooked = cook(@raw, topic_id: @topic_id)
|
|
||||||
@cooked_document = Nokogiri::HTML.fragment(@cooked)
|
|
||||||
end
|
|
||||||
|
|
||||||
# What we use to cook posts
|
# What we use to cook posts
|
||||||
def cook(*args)
|
def cook(*args)
|
||||||
cooked = PrettyText.cook(*args)
|
cooked = PrettyText.cook(*args)
|
||||||
|
@ -110,6 +103,10 @@ class PostAnalyzer
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def cooked_document
|
||||||
|
@cooked_document ||= Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
|
||||||
|
end
|
||||||
|
|
||||||
def link_is_a_mention?(l)
|
def link_is_a_mention?(l)
|
||||||
html_class = l.attributes['class']
|
html_class = l.attributes['class']
|
||||||
return false if html_class.nil?
|
return false if html_class.nil?
|
||||||
|
|
Reference in a new issue