From 95202f21b2a3d36d96e96afdd4e27232793f4ea5 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Fri, 21 Feb 2014 10:51:48 -0600 Subject: [PATCH] add Tag.active scope to deprecate tags without removing them --- app/controllers/filters_controller.rb | 5 +++-- app/models/story.rb | 5 ++++- app/models/tag.rb | 6 ++++-- app/models/user.rb | 2 +- db/migrate/20140221164400_inactive_tags.rb | 5 +++++ db/schema.rb | 4 +++- 6 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20140221164400_inactive_tags.rb diff --git a/app/controllers/filters_controller.rb b/app/controllers/filters_controller.rb index 26f738d..45c82c2 100644 --- a/app/controllers/filters_controller.rb +++ b/app/controllers/filters_controller.rb @@ -5,7 +5,7 @@ class FiltersController < ApplicationController @cur_url = "/filters" @title = "Filtered Tags" - @tags = Tag.all_with_story_counts_for(@user) + @tags = Tag.active.all_with_story_counts_for(@user) if @user @filtered_tags = @user.tag_filter_tags.to_a @@ -16,7 +16,8 @@ class FiltersController < ApplicationController def update 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 } if @user diff --git a/app/models/story.rb b/app/models/story.rb index b04fdfa..bcb2c60 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -149,6 +149,9 @@ class Story < ActiveRecord::Base if !t.tag.valid_for?(self.user) raise "#{self.user.username} does not have permission to use " << "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 @@ -336,7 +339,7 @@ class Story < ActiveRecord::Base new_tag_names_a.each do |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 # because we aren't assured to have a user_id by now; we'll do it in # the validation with check_tags diff --git a/app/models/tag.rb b/app/models/tag.rb index 9d8764b..66c923b 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -10,6 +10,8 @@ class Tag < ActiveRecord::Base user && user.is_moderator?? all : where(:privileged => false) end + scope :active, -> { where(:inactive => false) } + def to_param self.tag end @@ -17,7 +19,7 @@ class Tag < ActiveRecord::Base def self.all_with_filtered_counts_for(user) 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 } @@ -26,7 +28,7 @@ class Tag < ActiveRecord::Base def self.all_with_story_counts_for(user) 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 } diff --git a/app/models/user.rb b/app/models/user.rb index 4296915..50b8aa9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -191,7 +191,7 @@ class User < ActiveRecord::Base end def most_common_story_tag - Tag.joins( + Tag.active.joins( :stories ).where( :stories => { :user_id => self.id } diff --git a/db/migrate/20140221164400_inactive_tags.rb b/db/migrate/20140221164400_inactive_tags.rb new file mode 100644 index 0000000..7e61f02 --- /dev/null +++ b/db/migrate/20140221164400_inactive_tags.rb @@ -0,0 +1,5 @@ +class InactiveTags < ActiveRecord::Migration + def change + add_column :tags, :inactive, :boolean, :default => false + end +end diff --git a/db/schema.rb b/db/schema.rb index cc28bb6..9e23ec1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| 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", ["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 create_table "tag_filters", force: true do |t| @@ -130,6 +131,7 @@ ActiveRecord::Schema.define(version: 20140219183804) do t.string "description", limit: 100 t.boolean "privileged", default: false t.boolean "is_media", default: false + t.boolean "inactive", default: false end add_index "tags", ["tag"], name: "tag", unique: true, using: :btree