Add a 'private' attribute to tags to allow for admin / mod specific tags

Private tags would allow admins to create tags that only admins could
use, and that regular users could not filter. The best example use case
for this is an 'announements' tag for site announements that all users
should see.
This commit is contained in:
Robert J Samson 2012-09-19 16:44:57 -04:00
parent 64c6949edf
commit 5ada55f10d
5 changed files with 30 additions and 5 deletions

View file

@ -1,10 +1,19 @@
class Tag < ActiveRecord::Base
attr_accessor :filtered_count
def self.all_with_filtered_counts
# Scope to determine what tags a user can see
scope :accessible_to, ->(user) do
if user.is_admin?
all
else
where(:private => false)
end
end
def self.all_with_filtered_counts(user)
counts = TagFilter.count(:group => "tag_id")
Tag.order(:tag).all.map{|t|
Tag.order(:tag).accessible_to(user).map{|t|
t.filtered_count = counts[t.id].to_i
t
}

View file

@ -15,7 +15,7 @@
<th width="15%">Tag</th>
<th width="78%">Description</th>
</tr>
<% Tag.all(:order => :tag).each do |tag| %>
<% Tag.order(:tag).accessible_to(@user).each do |tag| %>
<tr>
<td><input type="checkbox" name="tag_<%= tag.tag %>" value=1
<%= @filtered_tags.map{|ft| ft.tag_id }.include?(tag.id) ?

View file

@ -24,7 +24,7 @@
<%= f.label :tags_a, "Tags:", :class => "required",
:style => "line-height: 2.3em;" %>
<%= f.select "tags_a", options_for_select(
Tag.all_with_filtered_counts.map{|t|
Tag.all_with_filtered_counts(@user).map{|t|
[ "#{t.tag} - #{t.description}", t.tag, { "data-html" => raw("<strong>") +
t.tag + raw("</strong> - ") + t.description.to_s +
(t.filtered_count == 0 ? "" :

View file

@ -0,0 +1,15 @@
class PrivateTags < ActiveRecord::Migration
def up
add_column :tags, :private, :boolean, :default => false
# All existing tags should be public by default
Tag.all.each do |t|
t.private = false
t.save
end
end
def down
remove_column :tags, :private
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120918152116) do
ActiveRecord::Schema.define(:version => 20120919195401) do
create_table "comments", :force => true do |t|
t.datetime "created_at", :null => false
@ -115,6 +115,7 @@ ActiveRecord::Schema.define(:version => 20120918152116) do
t.string "tag", :limit => 25, :default => "", :null => false
t.string "description", :limit => 100
t.boolean "filtered_by_default", :default => false
t.boolean "private", :default => false
end
add_index "tags", ["tag"], :name => "tag", :unique => true