-
Notifications
You must be signed in to change notification settings - Fork 0
/
acts_as_tweetable.rb
65 lines (55 loc) · 1.86 KB
/
acts_as_tweetable.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
require 'active_record'
module Newscloud
module Acts
module Tweetable
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_tweetable
has_one :tweeted_item, :as => :item
include Newscloud::Acts::Tweetable::InstanceMethods
extend Newscloud::Acts::Tweetable::TweetClassMethods
end
end
module TweetClassMethods
def tweetable?
true
end
def hot_items
setting_group = self.tweet_setting_group
min_votes = Metadata::Setting.find_setting("tweet_#{setting_group}_min_votes").try(:value) || Metadata::Setting.find_setting("tweet_default_min_votes").try(:value)
limit = Metadata::Setting.find_setting("tweet_#{setting_group}_limit").try(:value) || Metadata::Setting.find_setting("tweet_default_limit").try(:value)
#
# TODO:: Fix error from this
# ArgumentError: Unknown key(s): at_least
#
begin
hot_options = self.options_for_tally(
{ :at_least => min_votes,
:at_most => 1000,
:start_at => 1.day.ago,
:limit => limit,
:order => "#{self.table_name}.created_at desc"
}).merge({:include => [:tweeted_item], :conditions=>"votes.voteable_type = '#{self.name}' AND tweeted_items.item_id IS NULL"})
self.find(:all, hot_options)
rescue Exception => e
Rails.logger.error("ERROR: Tweet Hot Items error:: #{e}")
return false
end
end
def tweet_setting_group
self.table_name
end
end
module InstanceMethods
def tweetable?
true
end
def tweet
Resque.enqueue(TwitterWorker, self.class.name, self.id)
end
end
end
end
end