Class: TagsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- TagsController
- Includes:
- DraftManagement
- Defined in:
- /var/apps/qpixel/app/controllers/tags_controller.rb
Constant Summary
Constants included from DraftManagement
DraftManagement::DRAFTABLE_FIELDS, DraftManagement::DRAFT_MAX_AGE, DraftManagement::NESTED_DRAFTABLE_FIELDS, DraftManagement::TOP_LEVEL_DRAFTABLE_FIELDS
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 included from DraftManagement
#do_delete_draft, #do_save_draft
Methods inherited from ApplicationController
#dashboard, #keyboard_tools, #upload
Instance Method Details
#category ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 32 def category @tag_set = @category.tag_set if @tag_set.nil? not_found! return end @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.list_includes @count_all = @tags.length || 0 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 130 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
98 99 100 101 102 103 104 105 106 107 108 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 98 def create create_params = tag_params.merge(tag_set_id: @category.tag_set.id) @tag = Tag.new(create_params) if @tag.save do_delete_draft(current_user, URI(request.referer || '').path) redirect_to tag_path(id: @category.id, tag_id: @tag.id) else render :new, status: :bad_request end end |
#edit ⇒ Object
110 111 112 113 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 110 def edit check_your_privilege('edit_tags', nil, true) @tag.tag_synonyms.build end |
#index ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 11 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 @tags = @tags.list_includes .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
176 177 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 176 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
93 94 95 96 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 93 def new @tag = Tag.new @tag.tag_synonyms.build end |
#nuke ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 227 def nuke if @tag.children.any? flash[:error] = 'Cannot delete a tag that has children.' else 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}" end redirect_to (@category) end |
#nuke_warning ⇒ Object
259 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 259 def nuke_warning; end |
#rename ⇒ Object
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 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 145 def rename status = false @tag.transaction do old_tag_name = @tag.name status = @tag.update(name: params[:name]) if status AuditLog.moderator_audit(event_type: 'tag_rename', related: @tag, user: current_user, comment: "#{old_tag_name} renamed to #{params[:name]}") else raise ActiveRecord::Rollback end end if status render json: { status: 'success', tag: @tag } else render json: { status: 'failed', message: I18n.t('tags.errors.rename_generic'), errors: @tag.errors., tag: @tag }, status: :bad_request end end |
#select_merge ⇒ Object
174 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 174 def select_merge; end |
#show ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 68 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 post_ids = Post.undeleted .where(post_type_id: displayed_post_types) .joins(:posts_tags) .where(posts_tags: { tag_id: tag_ids }) .select(:id) @posts = Post.where(id: post_ids) .list_includes .order(sort_param) .paginate(page: params[:page], per_page: 50) respond_to do |format| format.html format.rss end end |
#update ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File '/var/apps/qpixel/app/controllers/tags_controller.rb', line 115 def update return unless check_your_privilege('edit_tags', nil, true) wiki_md = params[:tag][:wiki_markdown] update_params = tag_params.merge(wiki: wiki_md.present? ? helpers.render_markdown(wiki_md) : nil) .except(:name) if @tag.update(update_params) do_delete_draft(current_user, URI(request.referer || '').path) redirect_to tag_path(id: @category.id, tag_id: @tag.id) else render :edit, status: :bad_request end end |