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 43 @repository.project_id = params[:project_id] #TODO: refactor this
44 44 # project_id should be part of repository params on the form
45 45  
46   -
47 46 respond_to do |format|
48 47 if @repository.save
49 48 format.html { redirect_to project_path(params[:project_id]), notice: 'Repository was successfully created.' }
50 49 format.json { render action: 'show', status: :created, location: @repository }
51 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 52 end
58 53 end
59 54 end
... ... @@ -67,11 +62,7 @@ class RepositoriesController < ApplicationController
67 62 format.html { redirect_to(project_repository_path(params[:project_id], @repository.id), notice: 'Repository was successfully updated.') }
68 63 format.json { head :no_content }
69 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 66 end
76 67 end
77 68 end
... ... @@ -87,6 +78,14 @@ class RepositoriesController < ApplicationController
87 78 end
88 79  
89 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 89 # Use callbacks to share common setup or constraints between actions.
91 90 def set_repository
92 91 @repository = Repository.find(params[:id].to_i)
... ...