Skip to content

Commit

Permalink
Refactor controllers to share resource params
Browse files Browse the repository at this point in the history
  • Loading branch information
potomak committed Mar 12, 2017
1 parent 7df1fbc commit fc9d5bb
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 50 deletions.
6 changes: 2 additions & 4 deletions app/controllers/api/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Api
class ProjectsController < BaseController
include ProjectsParams

before_action :authenticate_user!
before_action :find_project, only: [:show, :update, :destroy]

Expand Down Expand Up @@ -49,9 +51,5 @@ def destroy
def find_project
@project = current_user.projects.find(params[:id])
end

def resource_params
params.require(:project).permit(:name, :tag_list, :money_budget, :time_budget)
end
end
end
6 changes: 2 additions & 4 deletions app/controllers/api/tomatoes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Api
class TomatoesController < BaseController
include TomatoesParams

before_action :authenticate_user!
before_action :find_tomato, only: [:show, :update, :destroy]

Expand Down Expand Up @@ -58,9 +60,5 @@ def to
def find_tomato
@tomato = current_user.tomatoes.find(params[:id])
end

def resource_params
params.require(:tomato).try(:permit, :tag_list)
end
end
end
19 changes: 2 additions & 17 deletions app/controllers/api/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Api
class UsersController < BaseController
include UsersParams

before_action :authenticate_user!

# GET /api/user
Expand All @@ -15,22 +17,5 @@ def update
render status: :unprocessable_entity, json: current_user.errors
end
end

private

def resource_params
params.require(:user).permit(
:name,
:email,
:image,
:time_zone,
:color,
:work_hours_per_day,
:average_hourly_rate,
:currency,
:volume,
:ticking
)
end
end
end
11 changes: 11 additions & 0 deletions app/controllers/concerns/projects_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ProjectsParams
def resource_params
params.require(:project).try(
:permit,
:name,
:tag_list,
:money_budget,
:time_budget
)
end
end
5 changes: 5 additions & 0 deletions app/controllers/concerns/tomatoes_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module TomatoesParams
def resource_params
params.require(:tomato).try(:permit, :tag_list)
end
end
17 changes: 17 additions & 0 deletions app/controllers/concerns/users_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module UsersParams
def resource_params
params.require(:user).try(
:permit,
:name,
:email,
:image,
:time_zone,
:color,
:work_hours_per_day,
:average_hourly_rate,
:currency,
:volume,
:ticking
)
end
end
6 changes: 2 additions & 4 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ProjectsController < ApplicationController
include ProjectsParams

before_action :authenticate_user!
before_action :find_project, only: [:show, :edit, :update, :destroy]

Expand Down Expand Up @@ -50,8 +52,4 @@ def destroy
def find_project
@project = current_user.projects.find(params[:id])
end

def resource_params
params.require(:project).permit(:name, :tag_list, :money_budget, :time_budget)
end
end
6 changes: 2 additions & 4 deletions app/controllers/tomatoes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class TomatoesController < ApplicationController
include TomatoesParams

before_action :authenticate_user!, except: [:by_day, :by_hour]
before_action :find_user, only: [:by_day, :by_hour]
before_action :find_tomato, only: [:show, :edit, :update, :destroy]
Expand Down Expand Up @@ -114,8 +116,4 @@ def notice_message
I18n.t('tomato.short_break')
end
end

def resource_params
params.require(:tomato).permit(:tag_list)
end
end
19 changes: 2 additions & 17 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class UsersController < ApplicationController
include UsersParams

before_action :authenticate_user!, except: :show
before_action :same_user!, except: :show

Expand All @@ -24,21 +26,4 @@ def destroy
@user.destroy
redirect_to root_url
end

private

def resource_params
params.require(:user).permit(
:name,
:email,
:image,
:time_zone,
:color,
:work_hours_per_day,
:average_hourly_rate,
:currency,
:volume,
:ticking
)
end
end

0 comments on commit fc9d5bb

Please sign in to comment.