Class: CategoriesController

Inherits:
ApplicationController show all
Defined in:
/var/apps/qpixel/app/controllers/categories_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#category_post_typesObject



86
87
88
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 86

def category_post_types
  @category_post_types = CategoryPostType.where(category: @category).includes(:category, :post_type)
end

#createObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 47

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.'
    clear_categories_cache
    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_typeObject



120
121
122
123
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 120

def delete_cat_post_type
  CategoryPostType.where(category: @category, post_type_id: params[:post_type]).delete_all
  render json: { status: 'success' }
end

#destroyObject



125
126
127
128
129
130
131
132
133
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 125

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

#editObject



66
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 66

def edit; end

#homepageObject



30
31
32
33
34
35
36
37
38
39
40
41
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 30

def homepage
  @category = Category.where(is_homepage: true).first

  unless @category.present?
    redirect_to categories_path
    return
  end

  update_last_visit(@category)
  set_list_posts
  render :show
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 7

def index
  @categories = if params[:term].present?
                  Category.search(params[:term])
                else
                  Category.all
                end

  @categories = @categories.accessible_to(current_user)
                           .order(sequence: :asc, id: :asc)

  respond_to do |format|
    format.html
    format.json do
      render json: @categories
    end
  end
end

#newObject



43
44
45
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 43

def new
  @category = Category.new
end

#post_typesObject



139
140
141
142
143
144
145
146
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 139

def post_types
  @post_types = @category.top_level_post_types
  if @post_types.one?
    redirect_to new_category_post_path(post_type: @post_types.first, category: @category)
  elsif @post_types.empty? && current_user&.admin?
    redirect_to edit_category_post_types_path(@category, no_return: '1')
  end
end

#rss_feedObject



135
136
137
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 135

def rss_feed
  set_list_posts
end

#showObject



25
26
27
28
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 25

def show
  update_last_visit(@category)
  set_list_posts
end

#updateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 68

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.'
    clear_categories_cache
    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_typeObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File '/var/apps/qpixel/app/controllers/categories_controller.rb', line 90

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