From 5ada55f10dd11d1fd80c174b1752beb99f37d369 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Wed, 19 Sep 2012 16:44:57 -0400 Subject: [PATCH] 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. --- app/models/tag.rb | 13 +++++++++++-- app/views/filters/index.html.erb | 2 +- app/views/stories/_form.html.erb | 2 +- db/migrate/20120919195401_private_tags.rb | 15 +++++++++++++++ db/schema.rb | 3 ++- 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20120919195401_private_tags.rb diff --git a/app/models/tag.rb b/app/models/tag.rb index fe4e5c5..c8f5456 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 } diff --git a/app/views/filters/index.html.erb b/app/views/filters/index.html.erb index 44f2b74..8f9c587 100644 --- a/app/views/filters/index.html.erb +++ b/app/views/filters/index.html.erb @@ -15,7 +15,7 @@ Tag Description - <% Tag.all(:order => :tag).each do |tag| %> + <% Tag.order(:tag).accessible_to(@user).each do |tag| %> "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("") + t.tag + raw(" - ") + t.description.to_s + (t.filtered_count == 0 ? "" : diff --git a/db/migrate/20120919195401_private_tags.rb b/db/migrate/20120919195401_private_tags.rb new file mode 100644 index 0000000..8352cc1 --- /dev/null +++ b/db/migrate/20120919195401_private_tags.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 00a5690..6797a05 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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