2012-06-17 03:15:46 +02:00
|
|
|
class Tag < ActiveRecord::Base
|
2012-09-19 19:25:11 +02:00
|
|
|
attr_accessor :filtered_count
|
|
|
|
|
2012-09-19 22:44:57 +02:00
|
|
|
scope :accessible_to, ->(user) do
|
2012-09-20 17:53:11 +02:00
|
|
|
user.is_admin?? all : where(:privileged => false)
|
2012-09-19 22:44:57 +02:00
|
|
|
end
|
|
|
|
|
2012-09-20 02:28:09 +02:00
|
|
|
def self.all_with_filtered_counts_for(user)
|
2012-09-19 19:25:11 +02:00
|
|
|
counts = TagFilter.count(:group => "tag_id")
|
|
|
|
|
2012-09-19 22:44:57 +02:00
|
|
|
Tag.order(:tag).accessible_to(user).map{|t|
|
2012-09-19 19:25:11 +02:00
|
|
|
t.filtered_count = counts[t.id].to_i
|
|
|
|
t
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-09-20 02:57:45 +02:00
|
|
|
def valid_for?(user)
|
|
|
|
if self.privileged?
|
|
|
|
user.is_admin?
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-19 19:25:11 +02:00
|
|
|
def filtered_count
|
|
|
|
@filtered_count ||= TagFilter.where(:tag_id => self.id).count
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|