Class: CategoriesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- CategoriesController
- Defined in:
- app/controllers/categories_controller.rb
Instance Method Summary collapse
- #category_post_types ⇒ Object
- #create ⇒ Object
- #delete_cat_post_type ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #homepage ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #post_types ⇒ Object
- #rss_feed ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
- #update_cat_post_type ⇒ Object
Methods inherited from ApplicationController
#dashboard, #keyboard_tools, #upload
Instance Method Details
#category_post_types ⇒ Object
76 77 78 |
# File 'app/controllers/categories_controller.rb', line 76 def category_post_types @category_post_types = CategoryPostType.where(category: @category).includes(:category, :post_type) end |
#create ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/controllers/categories_controller.rb', line 33 def create @category = Category.new category_params if @category.save if @category.is_homepage Category.where.not(id: @category.id).update_all(is_homepage: false) end before = @category.attributes_print AuditLog.admin_audit(event_type: 'category_create', related: @category, user: current_user, comment: "<<Category #{before}>>") flash[:success] = 'Your category was created.' Rails.cache.delete "#{RequestContext.community_id}/header_categories" Rails.cache.delete 'categories/by_lowercase_name' Rails.cache.delete 'categories/by_id' redirect_to category_path(@category) else flash[:danger] = 'There were some errors while trying to save your category.' render :new, status: 400 end end |
#delete_cat_post_type ⇒ Object
110 111 112 113 |
# File 'app/controllers/categories_controller.rb', line 110 def delete_cat_post_type CategoryPostType.where(category: @category, post_type_id: params[:post_type]).delete_all render json: { status: 'success' } end |
#destroy ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'app/controllers/categories_controller.rb', line 115 def destroy before = @category.attributes_print unless @category.destroy flash[:danger] = "Couldn't delete that category." end AuditLog.admin_audit(event_type: 'category_destroy', user: current_user, comment: "<<Category #{before}>>") redirect_back fallback_location: categories_path end |
#edit ⇒ Object
54 |
# File 'app/controllers/categories_controller.rb', line 54 def edit; end |
#homepage ⇒ Object
22 23 24 25 26 27 |
# File 'app/controllers/categories_controller.rb', line 22 def homepage @category = Category.where(is_homepage: true).first update_last_visit(@category) set_list_posts render :show end |
#index ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'app/controllers/categories_controller.rb', line 7 def index @categories = Category.all.order(sequence: :asc, id: :asc) respond_to do |format| format.html format.json do render json: @categories end end end |
#new ⇒ Object
29 30 31 |
# File 'app/controllers/categories_controller.rb', line 29 def new @category = Category.new end |
#post_types ⇒ Object
129 130 131 132 133 134 |
# File 'app/controllers/categories_controller.rb', line 129 def post_types @post_types = @category.post_types.where(is_top_level: true) if @post_types.count == 1 redirect_to new_category_post_path(post_type: @post_types.first, category: @category) end end |
#rss_feed ⇒ Object
125 126 127 |
# File 'app/controllers/categories_controller.rb', line 125 def rss_feed set_list_posts end |
#show ⇒ Object
17 18 19 20 |
# File 'app/controllers/categories_controller.rb', line 17 def show update_last_visit(@category) set_list_posts end |
#update ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/controllers/categories_controller.rb', line 56 def update before = @category.attributes_print if @category.update category_params if @category.is_homepage Category.where.not(id: @category.id).update_all(is_homepage: false) end after = @category.attributes_print AuditLog.admin_audit(event_type: 'category_update', related: @category, user: current_user, comment: "from <<Category #{before}>>\nto <<Category #{after}>>") flash[:success] = 'Your category was updated.' Rails.cache.delete "#{RequestContext.community_id}/header_categories" Rails.cache.delete 'categories/by_lowercase_name' Rails.cache.delete 'categories/by_id' redirect_to category_path(@category) else flash[:danger] = 'There were some errors while trying to save your category.' render :new end end |
#update_cat_post_type ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/controllers/categories_controller.rb', line 80 def update_cat_post_type @post_type = PostType.find_by(id: params[:post_type]) if @post_type.nil? render json: { status: 'failed', message: 'Post type not found.' }, status: :not_found return end @category_post_type = CategoryPostType.find_by(category: @category, post_type: @post_type) if @category_post_type.nil? @category_post_type = @category.category_post_types.create(post_type: @post_type, upvote_rep: params[:upvote_rep], downvote_rep: params[:downvote_rep]) status = :created else @category_post_type.update(upvote_rep: params[:upvote_rep], downvote_rep: params[:downvote_rep]) status = :ok end # Break rep awards cache either way and regenerate. cache_key = 'network/category_post_types/rep_changes' Rails.cache.delete cache_key, include_community: false Rails.cache.write(cache_key, CategoryPostType.all.to_h do |cpt| [[cpt.category_id, cpt.post_type_id], { 1 => cpt.upvote_rep, -1 => cpt.downvote_rep }] end, include_community: false) view_name = 'categories/_category_post_type_edit' render json: { status: 'success', cpt: @category_post_type, html: render_to_string(view_name, layout: false, locals: { cpt: @category_post_type }) }, status: status end |