journalduhacker/app/models/tag.rb
joshua stein 249dd85ec3 allow non-logged-in users to define tag filters
when not logged in, store the filters in a long-lasting cookie and
do not cache the home page

for that one guy on hacker news that complained about lobste.rs not
having this
2013-08-05 02:19:55 -05:00

36 lines
721 B
Ruby

class Tag < ActiveRecord::Base
has_many :taggings,
:dependent => :delete_all
attr_accessor :filtered_count
scope :accessible_to, ->(user) do
user && user.is_admin?? all : where(:privileged => false)
end
def self.all_with_filtered_counts_for(user)
counts = TagFilter.count(:group => "tag_id")
Tag.order(:tag).accessible_to(user).map{|t|
t.filtered_count = counts[t.id].to_i
t
}
end
def css_class
"tag tag_#{self.tag}" << (self.is_media?? " tag_is_media" : "")
end
def valid_for?(user)
if self.privileged?
user.is_admin?
else
true
end
end
def filtered_count
@filtered_count ||= TagFilter.where(:tag_id => self.id).count
end
end