11module BetterTogether
2- class CategoriesController < ApplicationController
3- before_action :set_category , only : %i[ show edit update destroy ]
2+ class CategoriesController < FriendlyResourceController
3+ before_action :set_model_instance , only : %i[ show edit update destroy ]
4+ before_action :authorize_category , only : %i[ show edit update destroy ]
5+ after_action :verify_authorized , except : :index
46
57 # GET /categories
68 def index
7- @categories = Category . all
9+ authorize resource_class
10+ @categories = resource_class . all
811 end
912
1013 # GET /categories/1
@@ -13,7 +16,8 @@ def show
1316
1417 # GET /categories/new
1518 def new
16- @category = Category . new
19+ @category = resource_class . new
20+ authorize_category
1721 end
1822
1923 # GET /categories/1/edit
@@ -22,7 +26,8 @@ def edit
2226
2327 # POST /categories
2428 def create
25- @category = Category . new ( category_params )
29+ @category = resource_class . new ( category_params )
30+ authorize_category
2631
2732 if @category . save
2833 redirect_to @category , notice : "Category was successfully created."
@@ -46,15 +51,32 @@ def destroy
4651 redirect_to categories_url , notice : "Category was successfully destroyed." , status : :see_other
4752 end
4853
49- private
50- # Use callbacks to share common setup or constraints between actions.
51- def set_category
52- @category = Category . find ( params [ :id ] )
53- end
54+ protected
5455
55- # Only allow a list of trusted parameters through.
56- def category_params
57- params . fetch ( :category , { } )
58- end
56+ # Adds a policy check for the category
57+ def authorize_category
58+ authorize @category
59+ end
60+
61+ def set_model_instance
62+ @category = set_resource_instance
63+ end
64+
65+ # Only allow a list of trusted parameters through.
66+ def category_params
67+ permitted = [
68+ *resource_class . extra_permitted_attributes
69+ ]
70+
71+ params . require ( resource_class . name . demodulize . underscore . to_sym ) . permit ( permitted )
72+ end
73+
74+ def resource_class
75+ ::BetterTogether ::Category
76+ end
77+
78+ def resource_collection
79+ resource_class . with_translations
80+ end
5981 end
6082end
0 commit comments