Class: TagsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- TagsController
- Defined in:
- app/controllers/tags_controller.rb
Instance Method Summary collapse
- #category ⇒ Object
- #children ⇒ Object
- #create ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #merge ⇒ Object
- #new ⇒ Object
- #nuke ⇒ Object
- #nuke_warning ⇒ Object
- #rename ⇒ Object
- #select_merge ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Methods inherited from ApplicationController
#dashboard, #keyboard_tools, #upload
Instance Method Details
#category ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/controllers/tags_controller.rb', line 25 def category @tag_set = @category.tag_set @tags = if params[:q].present? @tag_set..search(params[:q]) elsif params[:hierarchical].present? @tag_set.with_paths(params[:no_excerpt]) elsif params[:no_excerpt].present? @tag_set..where(excerpt: ['', nil]) else @tag_set&. end table = params[:hierarchical].present? ? 'tags_paths' : 'tags' @tags = @tags&.left_joins(:posts) &.group(Arel.sql("#{table}.id")) &.select(Arel.sql("#{table}.*, COUNT(DISTINCT IF(posts.deleted = 0, posts.id, NULL)) AS post_count")) &.paginate(per_page: 96, page: params[:page]) @tags = if params[:hierarchical].present? @tags&.order(:path) else @tags&.order(Arel.sql('COUNT(posts.id) DESC')) end @count = @tags&.length || 0 end |
#children ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/controllers/tags_controller.rb', line 105 def children @tags = if params[:q].present? @tag.children.search(params[:q]) elsif params[:hierarchical].present? @tag.children_with_paths.order(:path) else @tag.children.order(Arel.sql('COUNT(posts.id) DESC')) end @count = @tags.count table = params[:hierarchical].present? ? 'tags_paths' : 'tags' @tags = @tags.left_joins(:posts).group(Arel.sql("#{table}.id")) .select(Arel.sql("#{table}.*, COUNT(posts.id) AS post_count")) .paginate(per_page: 96, page: params[:page]) end |
#create ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'app/controllers/tags_controller.rb', line 78 def create @tag = Tag.new(tag_params.merge(tag_set_id: @category.tag_set.id)) if @tag.save flash[:danger] = nil redirect_to tag_path(id: @category.id, tag_id: @tag.id) else flash[:danger] = @tag.errors..join(', ') render :new, status: :bad_request end end |
#edit ⇒ Object
89 90 91 92 |
# File 'app/controllers/tags_controller.rb', line 89 def edit check_your_privilege('edit_tags', nil, true) @tag.tag_synonyms.build end |
#index ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/controllers/tags_controller.rb', line 9 def index @tag_set = if params[:tag_set].present? TagSet.find(params[:tag_set]) end @tags = if params[:term].present? (@tag_set&. || Tag).search(params[:term]) else (@tag_set&. || Tag.all).order(:name) end.includes(:tag_synonyms).paginate(page: params[:page], per_page: 50) respond_to do |format| format.json do render json: @tags.to_json(include: { tag_synonyms: { only: :name } }) end end end |
#merge ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/controllers/tags_controller.rb', line 127 def merge @primary = @tag # No merge to self. if params[:merge_with_id] == @primary.id.to_s flash[:danger] = 'Cannot merge a tag with itself.' redirect_back fallback_location: categories_path return end @subordinate = Tag.find params[:merge_with_id] Post.transaction do AuditLog.moderator_audit event_type: 'tag_merge', related: @primary, user: current_user, comment: "#{@subordinate.name} (#{@subordinate.id}) into #{@primary.name} (#{@primary.id})" # Replace subordinate with primary, except when a post already has primary (to avoid giving them a duplicate tag) posts_sql = 'UPDATE posts INNER JOIN posts_tags ON posts.id = posts_tags.post_id ' \ 'SET posts.tags_cache = REPLACE(posts.tags_cache, ?, ?) ' \ 'WHERE posts_tags.tag_id = ? ' \ 'AND posts_tags.post_id NOT IN (SELECT post_id FROM posts_tags WHERE tag_id = ?)' exec_sql([posts_sql, "\n- #{@subordinate.name}\n", "\n- #{@primary.name}\n", @subordinate.id, @primary.id]) # Remove the subordinate tag from posts that still have it (the ones that were excluded from our previous query) posts2_sql = 'UPDATE posts INNER JOIN posts_tags ON posts.id = posts_tags.post_id ' \ 'SET posts.tags_cache = REPLACE(posts.tags_cache, ?, ?) ' \ 'WHERE posts_tags.tag_id = ?' exec_sql([posts2_sql, "\n- #{@subordinate.name}\n", "\n", @subordinate.id]) # Break hierarchies = 'UPDATE tags SET parent_id = NULL WHERE parent_id = ?' exec_sql([, @subordinate.id]) # Remove references to the tag sql = 'UPDATE IGNORE $TABLENAME SET tag_id = ? WHERE tag_id = ?' exec_sql([sql.gsub('$TABLENAME', 'posts_tags'), @primary.id, @subordinate.id]) exec_sql([sql.gsub('$TABLENAME', 'categories_moderator_tags'), @primary.id, @subordinate.id]) exec_sql([sql.gsub('$TABLENAME', 'categories_required_tags'), @primary.id, @subordinate.id]) exec_sql([sql.gsub('$TABLENAME', 'categories_topic_tags'), @primary.id, @subordinate.id]) exec_sql([sql.gsub('$TABLENAME', 'post_history_tags'), @primary.id, @subordinate.id]) exec_sql([sql.gsub('$TABLENAME', 'suggested_edits_tags'), @primary.id, @subordinate.id]) exec_sql([sql.gsub('$TABLENAME', 'suggested_edits_before_tags'), @primary.id, @subordinate.id]) # Nuke it from orbit @subordinate.destroy end flash[:success] = "Merged #{@subordinate.name} into #{@primary.name}." redirect_to tag_path(id: @category.id, tag_id: @primary.id) end |
#new ⇒ Object
73 74 75 76 |
# File 'app/controllers/tags_controller.rb', line 73 def new @tag = Tag.new @tag.tag_synonyms.build end |
#nuke ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/controllers/tags_controller.rb', line 178 def nuke Post.transaction do AuditLog.admin_audit event_type: 'tag_nuke', related: @tag, user: current_user, comment: "#{@tag.name} (#{@tag.id})" tables = ['posts_tags', 'categories_moderator_tags', 'categories_required_tags', 'categories_topic_tags', 'post_history_tags', 'suggested_edits_tags', 'suggested_edits_before_tags'] # Remove tag from caches caches_sql = 'UPDATE posts INNER JOIN posts_tags ON posts.id = posts_tags.post_id ' \ 'SET posts.tags_cache = REPLACE(posts.tags_cache, ?, ?) ' \ 'WHERE posts_tags.tag_id = ?' exec_sql([caches_sql, "\n- #{@tag.name}\n", "\n", @tag.id]) # Delete all references to the tag tables.each do |tbl| sql = "DELETE FROM #{tbl} WHERE tag_id = ?" exec_sql([sql, @tag.id]) end # Nuke it @tag.destroy end flash[:success] = "Deleted #{@tag.name}" redirect_to (@category) end |
#nuke_warning ⇒ Object
206 |
# File 'app/controllers/tags_controller.rb', line 206 def nuke_warning; end |
#rename ⇒ Object
120 121 122 123 |
# File 'app/controllers/tags_controller.rb', line 120 def rename status = @tag.update(name: params[:name]) render json: { success: status, tag: @tag } end |
#select_merge ⇒ Object
125 |
# File 'app/controllers/tags_controller.rb', line 125 def select_merge; end |
#show ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/controllers/tags_controller.rb', line 53 def show sort_params = { activity: { last_activity: :desc }, age: { created_at: :desc }, score: { score: :desc }, native: Arel.sql('att_source IS NULL DESC, last_activity DESC') } sort_param = sort_params[params[:sort]&.to_sym] || { last_activity: :desc } tag_ids = if params[:self].present? [@tag.id] else @tag.all_children + [@tag.id] end displayed_post_types = @tag.tag_set.categories.map(&:display_post_types).flatten @posts = Post.joins(:tags).where(id: PostsTag.select(:post_id).distinct.where(tag_id: tag_ids)) .undeleted.where(post_type_id: displayed_post_types) .includes(:post_type, :tags).list_includes.paginate(page: params[:page], per_page: 50) .order(sort_param) respond_to do |format| format.html format.rss end end |
#update ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'app/controllers/tags_controller.rb', line 94 def update return unless check_your_privilege('edit_tags', nil, true) wiki_md = params[:tag][:wiki_markdown] if @tag.update(tag_params.merge(wiki: wiki_md.present? ? helpers.render_markdown(wiki_md) : nil).except(:name)) redirect_to tag_path(id: @category.id, tag_id: @tag.id) else render :edit, status: :bad_request end end |