Merge pull request #23 from rjsamson/private-tags

Modify tags to allow for admin / mod specific tags (like 'announcements')
This commit is contained in:
joshua stein 2012-09-20 08:49:59 -07:00
commit 52836803f4
6 changed files with 48 additions and 7 deletions

View file

@ -19,7 +19,7 @@ class Story < ActiveRecord::Base
attr_accessible :title, :description, :tags_a, :moderation_reason
before_create :assign_short_id
before_save :log_moderation
before_save :log_moderation, :check_tags
after_create :mark_submitter
after_save :deal_with_tags
@ -163,6 +163,14 @@ class Story < ActiveRecord::Base
Keystore.increment_value_for("user:#{self.user_id}:stories_submitted")
end
def check_tags
(self.tags_to_add || []).each do |t|
if !t.valid_for?(self.user)
raise "#{self.user.username} does not have permissions to use privileged tags"
end
end
end
def deal_with_tags
(self.tags_to_delete || []).each do |t|
if t.is_a?(Tagging)
@ -173,7 +181,7 @@ class Story < ActiveRecord::Base
end
(self.tags_to_add || []).each do |t|
if t.is_a?(Tag)
if t.is_a?(Tag) && t.valid_for?(self.user)
tg = Tagging.new
tg.tag_id = t.id
tg.story_id = self.id

View file

@ -1,15 +1,32 @@
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(:privileged => false)
end
end
def self.all_with_filtered_counts_for(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
}
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

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_for(@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, :privileged, :boolean, :default => false
# All existing tags should be public by default
Tag.all.each do |t|
t.privileged = false
t.save
end
end
def down
remove_column :tags, :privileged
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 "privileged", :default => false
end
add_index "tags", ["tag"], :name => "tag", :unique => true