aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb3
-rw-r--r--app/models/favorite.rb8
-rw-r--r--app/models/retweet.rb8
-rw-r--r--app/models/tweet.rb38
-rw-r--r--app/models/user.rb12
5 files changed, 68 insertions, 1 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 50229f9..31fe26d 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -1,2 +1,5 @@
class Account < ActiveRecord::Base
+ def user
+ User.cached(user_id)
+ end
end
diff --git a/app/models/favorite.rb b/app/models/favorite.rb
index c764d08..6e15dc4 100644
--- a/app/models/favorite.rb
+++ b/app/models/favorite.rb
@@ -1,4 +1,12 @@
class Favorite < ActiveRecord::Base
belongs_to :tweet, :counter_cache => true
belongs_to :user
+
+ def user
+ User.cached(user_id)
+ end
+
+ def tweet
+ Tweet.cached(tweet_id)
+ end
end
diff --git a/app/models/retweet.rb b/app/models/retweet.rb
index 34da286..296b877 100644
--- a/app/models/retweet.rb
+++ b/app/models/retweet.rb
@@ -1,4 +1,12 @@
class Retweet < ActiveRecord::Base
belongs_to :tweet, :counter_cache => true
belongs_to :user
+
+ def user
+ User.cached(user_id)
+ end
+
+ def tweet
+ Tweet.cached(tweet_id)
+ end
end
diff --git a/app/models/tweet.rb b/app/models/tweet.rb
index a1124f9..6b6311d 100644
--- a/app/models/tweet.rb
+++ b/app/models/tweet.rb
@@ -2,4 +2,42 @@ class Tweet < ActiveRecord::Base
belongs_to :user
has_many :favorites, :dependent => :delete_all
has_many :retweets, :dependent => :delete_all
+
+ scope :recent, -> do
+ where("tweeted_at > ?", Time.zone.now - 3.days)
+ end
+
+ scope :reacted, -> do
+ where("favorites_count > 0 OR retweets_count > 0")
+ end
+
+ scope :order_by_id, -> do
+ order("id DESC")
+ end
+
+ scope :order_by_reactions, -> do
+ order("COALESCE(favorites_count, 0) + COALESCE(retweets_count, 0) DESC")
+ end
+
+ scope :discovered, -> user do
+ where("id IN (" +
+ "SELECT tweet_id FROM favorites WHERE user_id = ?" +
+ " UNION ALL " +
+ "SELECT tweet_id FROM retweets WHERE user_id = ?" +
+ ")", user.id, user.id)
+ end
+
+ scope :of, -> user do
+ where("user_id = ?", user.id)
+ end
+
+ def self.cached(id)
+ Rails.cache.fetch("tweet/#{id}", :expires_in => 3.hour) do
+ where(:id => id).first
+ end
+ end
+
+ def user
+ User.cached(user_id)
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index d268fcb..b93eac9 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -5,11 +5,21 @@ class User < ActiveRecord::Base
attrs = {:id => u}
end
attrs[:profile_image_url] ||= ActionController::Base.helpers.asset_path("missing_profile_image.png")
- attrs[:name] ||= "Missing: id=#{u}"
+ attrs[:name] ||= "Missing name: #{u}"
super(attrs)
end
has_many :tweets, :dependent => :delete_all
has_many :favorites, :dependent => :delete_all
has_many :retweets, :dependent => :delete_all
+
+ def self.cached(uid)
+ Rails.cache.fetch("user/#{uid}", :expires_in => 1.hour) do
+ where(:id => uid).first
+ end
+ end
+
+ def registered?
+ Account.exists?(:user_id => id)
+ end
end