add Tag.active scope to deprecate tags without removing them

This commit is contained in:
joshua stein 2014-02-21 10:51:48 -06:00
parent d75242f646
commit 95202f21b2
6 changed files with 20 additions and 7 deletions

View file

@ -5,7 +5,7 @@ class FiltersController < ApplicationController
@cur_url = "/filters" @cur_url = "/filters"
@title = "Filtered Tags" @title = "Filtered Tags"
@tags = Tag.all_with_story_counts_for(@user) @tags = Tag.active.all_with_story_counts_for(@user)
if @user if @user
@filtered_tags = @user.tag_filter_tags.to_a @filtered_tags = @user.tag_filter_tags.to_a
@ -16,7 +16,8 @@ class FiltersController < ApplicationController
def update def update
tags_param = params[:tags] tags_param = params[:tags]
new_tags = tags_param.blank? ? [] : Tag.where(:tag => tags_param).to_a new_tags = tags_param.blank? ? [] :
Tag.active.where(:tag => tags_param).to_a
new_tags.keep_if {|t| t.valid_for? @user } new_tags.keep_if {|t| t.valid_for? @user }
if @user if @user

View file

@ -149,6 +149,9 @@ class Story < ActiveRecord::Base
if !t.tag.valid_for?(self.user) if !t.tag.valid_for?(self.user)
raise "#{self.user.username} does not have permission to use " << raise "#{self.user.username} does not have permission to use " <<
"privileged tag #{t.tag.tag}" "privileged tag #{t.tag.tag}"
elsif t.tag.inactive? && !t.new_record?
# stories can have inactive tags as long as they existed before
raise "#{self.user.username} cannot add inactive tag #{t.tag.tag}"
end end
end end
@ -336,7 +339,7 @@ class Story < ActiveRecord::Base
new_tag_names_a.each do |tag_name| new_tag_names_a.each do |tag_name|
if tag_name.to_s != "" && !self.tags.exists?(:tag => tag_name) if tag_name.to_s != "" && !self.tags.exists?(:tag => tag_name)
if t = Tag.where(:tag => tag_name).first if t = Tag.active.where(:tag => tag_name).first
# we can't lookup whether the user is allowed to use this tag yet # we can't lookup whether the user is allowed to use this tag yet
# because we aren't assured to have a user_id by now; we'll do it in # because we aren't assured to have a user_id by now; we'll do it in
# the validation with check_tags # the validation with check_tags

View file

@ -10,6 +10,8 @@ class Tag < ActiveRecord::Base
user && user.is_moderator?? all : where(:privileged => false) user && user.is_moderator?? all : where(:privileged => false)
end end
scope :active, -> { where(:inactive => false) }
def to_param def to_param
self.tag self.tag
end end
@ -17,7 +19,7 @@ class Tag < ActiveRecord::Base
def self.all_with_filtered_counts_for(user) def self.all_with_filtered_counts_for(user)
counts = TagFilter.group(:tag_id).count counts = TagFilter.group(:tag_id).count
Tag.order(:tag).accessible_to(user).map{|t| Tag.active.order(:tag).accessible_to(user).map{|t|
t.filtered_count = counts[t.id].to_i t.filtered_count = counts[t.id].to_i
t t
} }
@ -26,7 +28,7 @@ class Tag < ActiveRecord::Base
def self.all_with_story_counts_for(user) def self.all_with_story_counts_for(user)
counts = Tagging.group(:tag_id).count counts = Tagging.group(:tag_id).count
Tag.order(:tag).accessible_to(user).map{|t| Tag.active.order(:tag).accessible_to(user).map{|t|
t.stories_count = counts[t.id].to_i t.stories_count = counts[t.id].to_i
t t
} }

View file

@ -191,7 +191,7 @@ class User < ActiveRecord::Base
end end
def most_common_story_tag def most_common_story_tag
Tag.joins( Tag.active.joins(
:stories :stories
).where( ).where(
:stories => { :user_id => self.id } :stories => { :user_id => self.id }

View file

@ -0,0 +1,5 @@
class InactiveTags < ActiveRecord::Migration
def change
add_column :tags, :inactive, :boolean, :default => false
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140219183804) do ActiveRecord::Schema.define(version: 20140221164400) do
create_table "comments", force: true do |t| create_table "comments", force: true do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
@ -107,6 +107,7 @@ ActiveRecord::Schema.define(version: 20140219183804) do
add_index "stories", ["hotness"], name: "hotness_idx", using: :btree add_index "stories", ["hotness"], name: "hotness_idx", using: :btree
add_index "stories", ["is_expired", "is_moderated"], name: "is_idxes", using: :btree add_index "stories", ["is_expired", "is_moderated"], name: "is_idxes", using: :btree
add_index "stories", ["short_id"], name: "short_id", unique: true, using: :btree
add_index "stories", ["url"], name: "url", length: {"url"=>191}, using: :btree add_index "stories", ["url"], name: "url", length: {"url"=>191}, using: :btree
create_table "tag_filters", force: true do |t| create_table "tag_filters", force: true do |t|
@ -130,6 +131,7 @@ ActiveRecord::Schema.define(version: 20140219183804) do
t.string "description", limit: 100 t.string "description", limit: 100
t.boolean "privileged", default: false t.boolean "privileged", default: false
t.boolean "is_media", default: false t.boolean "is_media", default: false
t.boolean "inactive", default: false
end end
add_index "tags", ["tag"], name: "tag", unique: true, using: :btree add_index "tags", ["tag"], name: "tag", unique: true, using: :btree