<commit_before>module V1
  class CategoriesController < ApplicationController
    include ApprovableState

    before_action :authenticate_user!, except: :index

    def index
      categories = policy_scope Category.insensitive_order(:name)

      render json: categories, status: :ok
    end

    def create
      category = Category.new(category_params)
      category = initial_approvable_state(category)

      if category.save
        render json: category, status: :created
      else
        render json: { errors: category.errors }, status: :unprocessable_entity
      end
    end

    def update
      authorize category = Category.find(params[:id])

      if approved?(category)
        render json: category
      else
        render json: { errors: category.errors }, status: :unprocessable_entity
      end
    end

    private

    def category_params
      params.require(:category).permit(:name)
    end
  end
end
<commit_msg>Invalidate cache after create or update a category<commit_after>module V1
  class CategoriesController < ApplicationController
    include ApprovableState

    before_action :authenticate_user!, except: :index

    def index
      categories = policy_scope Category.insensitive_order(:name)

      render json: categories, status: :ok
    end

    def create
      category = Category.new(category_params)
      category = initial_approvable_state(category)

      if category.save
        invalidate_cache
        render json: category, status: :created
      else
        render json: { errors: category.errors }, status: :unprocessable_entity
      end
    end

    def update
      authorize category = Category.find(params[:id])

      if approved?(category)
        invalidate_cache
        render json: category
      else
        render json: { errors: category.errors }, status: :unprocessable_entity
      end
    end

    private

    def invalidate_cache
      Rails.cache.delete('categories/admin')
      Rails.cache.delete('categories/public')
    end

    def category_params
      params.require(:category).permit(:name)
    end
  end
end

{'fraction_non_alphanumeric': 0.06863679694947569, 'fraction_numerical': 0.0009532888465204957, 'mean_word_length': 2.788808664259928, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5703561', 'n_tokens_mistral': 566, 'n_tokens_neox': 540, 'n_words': 154}