11class WritingSessionsController < ApplicationController
2- protect_from_forgery with : :exception , if : Proc . new { |c | c . request . format != 'application/json' }
3- protect_from_forgery with : :null_session , if : Proc . new { |c | c . request . format == 'application/json' }
4- before_action :story
5- before_action :writing_session , only : [ :show , :edit , :update , :destroy , :header_actions ]
6- after_action :touch_story , only : [ :create , :update , :destroy ]
7- layout "home" , only : [ :new , :edit ]
2+ protect_from_forgery with : :exception , if : proc { |c | c . request . format != 'application/json' }
3+ protect_from_forgery with : :null_session , if : proc { |c | c . request . format == 'application/json' }
4+ after_action :touch_story , only : %i[ create update destroy ]
5+ layout 'home' , only : %i[ new edit ]
86
9- def index
10- unless can? :read , @story
11- redirect_to_home
12- end
7+ load_and_authorize_resource :story
8+ load_and_authorize_resource :writing_session , through : :story , shallow : true
139
10+ def index
1411 @writing_sessions = @story . writing_sessions . order ( updated_at : :desc )
1512 end
1613
1714 # GET /writing_sessions/1
1815 # GET /writing_sessions/1.json
19- def show
20- unless can? :read , @session
21- redirect_to_home
22- end
23- end
16+ def show ; end
2417
2518 # GET /writing_sessions/new
2619 def new
2720 @title = 'Compose'
28- @session = @story . writing_sessions . create ( user_id : current_user . id , text : "" )
21+ @writing_session = @story . writing_sessions . create ( user_id : current_user . id , text : '' )
2922
30- redirect_to edit_story_writing_session_path ( @story , @session . id )
23+ redirect_to edit_story_writing_session_path ( @story , @writing_session . id )
3124 end
3225
3326 # GET /writing_sessions/1/edit
34- def edit
35- unless can? :update , @session
36- redirect_to_home
37- end
38- end
27+ def edit ; end
3928
4029 # POST /writing_sessions
4130 # POST /writing_sessions.json
4231 def create
43- unless can? :create , WritingSession
44- redirect_to_home
45- end
46-
47- params = session_params
48- params [ :text ] = "<div>#{ params [ :text ] } </div>"
32+ text = "<div>#{ writing_session_params [ :text ] } </div>"
4933
50- @session = @story . writing_sessions . new params
51- @session . user_id = current_user . id
52- @session . word_count = @session . calculate_word_count
34+ @writing_session = @story . writing_sessions . new ( writing_session_params . merge ( text :, user_id : current_user . id ) )
35+ @writing_session . word_count = @writing_session . calculate_word_count
5336
5437 respond_to do |format |
55- if @session . save
56- format . html { redirect_to edit_story_writing_session_path ( @story , @session . id ) , notice : 'Session was successfully created.' , status : :see_other }
57- format . json { render json : @session , status : :ok }
38+ if @writing_session . save
39+ format . html do
40+ redirect_to edit_story_writing_session_path ( @story , @writing_session . id ) , notice : 'Session was successfully created.' ,
41+ status : :see_other
42+ end
43+ format . json { render json : @writing_session , status : :ok }
5844 else
5945 format . html { render :new }
60- format . json { render json : @session . errors , status : :unprocessable_entity }
46+ format . json { render json : @writing_session . errors , status : :unprocessable_entity }
6147 end
6248 end
6349 end
6450
6551 # PATCH/PUT /writing_sessions/1
6652 # PATCH/PUT /writing_sessions/1.json
6753 def update
68- unless can? :update , @session
69- redirect_to_home
70- end
71-
72- @session . text += "<div>#{ session_params [ :text ] } </div>"
73- @session . word_count = @session . calculate_word_count
54+ @writing_session . text += "<div>#{ writing_session_params [ :text ] } </div>"
55+ @writing_session . word_count = @writing_session . calculate_word_count
7456
7557 respond_to do |format |
76- if @session . save
77- format . html { redirect_to edit_story_writing_session_path ( @story , @session . id ) , status : :see_other }
78- format . json { render json : @session , status : :ok }
58+ if @writing_session . save
59+ format . html { redirect_to edit_story_writing_session_path ( @story , @writing_session . id ) , status : :see_other }
60+ format . json { render json : @writing_session , status : :ok }
7961 else
8062 format . html { render :edit }
81- format . json { render json : @session . errors , status : :unprocessable_entity }
63+ format . json { render json : @writing_session . errors , status : :unprocessable_entity }
8264 end
8365 end
8466 end
8567
8668 # DELETE /writing_sessions/1
8769 # DELETE /writing_sessions/1.json
8870 def destroy
89- unless can? :destroy , @session
90- redirect_to_home
91- end
92-
93- @session . destroy
71+ @writing_session . destroy
9472 respond_to do |format |
9573 format . html { redirect_to story_path ( @story ) , notice : 'Session was successfully destroyed.' , status : :see_other }
9674 format . json { head :no_content }
@@ -99,19 +77,8 @@ def destroy
9977
10078 private
10179
102- # Use callbacks to share common setup or constraints between actions.
103- def writing_session
104- @session = @story . writing_sessions . find ( params [ :id ] )
105- end
106-
107- def story
108- @story = current_user . stories . find ( params [ :story_id ] )
109- rescue ActiveRecord ::RecordNotFound
110- redirect_to_home
111- end
112-
11380 # Only allow a list of trusted parameters through.
114- def session_params
81+ def writing_session_params
11582 params . require ( :writing_session ) . permit ( :text )
11683 end
11784
0 commit comments