Commit 869ef774ed359a1eff63f6f1f09571a9e721845e

Authored by Rafael Manzo
1 parent 23e1963b

Code duplication on RepositoriesController extracted into a private method

Showing 1 changed file with 10 additions and 11 deletions   Show diff stats
app/controllers/repositories_controller.rb
@@ -43,17 +43,12 @@ class RepositoriesController < ApplicationController @@ -43,17 +43,12 @@ class RepositoriesController < ApplicationController
43 @repository.project_id = params[:project_id] #TODO: refactor this 43 @repository.project_id = params[:project_id] #TODO: refactor this
44 # project_id should be part of repository params on the form 44 # project_id should be part of repository params on the form
45 45
46 -  
47 respond_to do |format| 46 respond_to do |format|
48 if @repository.save 47 if @repository.save
49 format.html { redirect_to project_path(params[:project_id]), notice: 'Repository was successfully created.' } 48 format.html { redirect_to project_path(params[:project_id]), notice: 'Repository was successfully created.' }
50 format.json { render action: 'show', status: :created, location: @repository } 49 format.json { render action: 'show', status: :created, location: @repository }
51 else 50 else
52 - @project_id = params[:project_id]  
53 - @repository_types = Repository.repository_types  
54 -  
55 - format.html { render action: 'new' }  
56 - format.json { render json: @repository.errors, status: :unprocessable_entity } 51 + failed_action(format, 'new')
57 end 52 end
58 end 53 end
59 end 54 end
@@ -67,11 +62,7 @@ class RepositoriesController < ApplicationController @@ -67,11 +62,7 @@ class RepositoriesController < ApplicationController
67 format.html { redirect_to(project_repository_path(params[:project_id], @repository.id), notice: 'Repository was successfully updated.') } 62 format.html { redirect_to(project_repository_path(params[:project_id], @repository.id), notice: 'Repository was successfully updated.') }
68 format.json { head :no_content } 63 format.json { head :no_content }
69 else 64 else
70 - @project_id = params[:project_id]  
71 - @repository_types = Repository.repository_types  
72 -  
73 - format.html { render action: 'edit' }  
74 - format.json { render json: @repository.errors, status: :unprocessable_entity } 65 + failed_action(format, 'edit')
75 end 66 end
76 end 67 end
77 end 68 end
@@ -87,6 +78,14 @@ class RepositoriesController < ApplicationController @@ -87,6 +78,14 @@ class RepositoriesController < ApplicationController
87 end 78 end
88 79
89 private 80 private
  81 + def failed_action(format, destiny_action)
  82 + @project_id = params[:project_id]
  83 + @repository_types = Repository.repository_types
  84 +
  85 + format.html { render action: destiny_action }
  86 + format.json { render json: @repository.errors, status: :unprocessable_entity }
  87 + end
  88 +
90 # Use callbacks to share common setup or constraints between actions. 89 # Use callbacks to share common setup or constraints between actions.
91 def set_repository 90 def set_repository
92 @repository = Repository.find(params[:id].to_i) 91 @repository = Repository.find(params[:id].to_i)