Commit e19c7cef7b539f04ab71f90e223e457348935b87

Authored by Pedro
Committed by Rafael Manzo
1 parent 72b87ff3

Repository routes decoupled from Project

new and create action can be acessed both with or without a project. This
enables to have repositories the two ways.

The behaviour for the back had to change from going back to a specific project
page. To the all projects pages since some repositories may not have a
project_id.

With the routes, many helpers had to get fixed together with acceptance
tests steps (for this a new step was created in order to be able to create
RepositoryAttributes).

Finally the JS for repository state retrieval needed fixes since it builds the
route withot any helper. Same thing for the JS that prevents infinite state
queries.

Signed off by: Rafael Reggiani Manzo <rr.manzo@gmail.com>
app/assets/javascripts/repository/state.js.coffee
1 1 class Repository.State
2   - constructor: (@project_id, @repository_id) ->
  2 + constructor: (@repository_id) ->
3 3  
4 4 poll_state: (last_state) ->
5   - $.post('/projects/' + @project_id + '/repositories/' + @repository_id + '/state',
  5 + $.post('/repositories/' + @repository_id + '/state',
6 6 last_state: last_state)
7 7  
8 8 schedule_poll_state: (last_state) ->
... ...
app/controllers/repositories_controller.rb
... ... @@ -38,7 +38,7 @@ class RepositoriesController &lt; ApplicationController
38 38 def update
39 39 respond_to do |format|
40 40 if @repository.update(repository_params)
41   - format.html { redirect_to(project_repository_path(params[:project_id], @repository.id), notice: t('successfully_updated', :record => t(@repository.class))) }
  41 + format.html { redirect_to(repository_path(@repository.id), notice: t('successfully_updated', :record => t(@repository.class))) }
42 42 format.json { head :no_content }
43 43 else
44 44 failed_action(format, 'edit')
... ... @@ -51,7 +51,7 @@ class RepositoriesController &lt; ApplicationController
51 51 def destroy
52 52 @repository.destroy
53 53 respond_to do |format|
54   - format.html { redirect_to project_path(params[:project_id]) }
  54 + format.html { redirect_to projects_path }
55 55 format.json { head :no_content }
56 56 end
57 57 end
... ... @@ -80,7 +80,7 @@ class RepositoriesController &lt; ApplicationController
80 80 @repository.process
81 81 set_kalibro_configuration
82 82 respond_to do |format|
83   - format.html { redirect_to project_repository_path(@repository.project_id, @repository.id) }
  83 + format.html { redirect_to repository_path(@repository.id) }
84 84 end
85 85 end
86 86  
... ... @@ -135,7 +135,8 @@ private
135 135 # Code extracted from create action
136 136 def create_and_redir(format)
137 137 if @repository.save
138   - format.html { redirect_to project_repository_process_path(@repository.project_id, @repository.id), notice: t('successfully_created', :record => t(@repository.class)) }
  138 + current_user.repository_attributes.create(repository_id: @repository.id)
  139 + format.html { redirect_to repository_process_path(@repository.id), notice: t('successfully_created', :record => t(@repository.class)) }
139 140 else
140 141 failed_action(format, 'new')
141 142 end
... ...
app/views/projects/show.html.erb
... ... @@ -44,11 +44,11 @@
44 44 <td><%= repository.address %></td>
45 45 <td>
46 46 <% if project_owner? @project.id %>
47   - <%= link_to t('edit'), edit_project_repository_path(@project, repository.id), class: 'btn btn-info' %>
  47 + <%= link_to t('edit'), edit_repository_path(repository.id), class: 'btn btn-info' %>
48 48 <% end %>
49 49 </td>
50 50 <td>
51   - <%= link_to t('show'), project_repository_path(@project, repository.id), class: 'btn btn-info' %></td>
  51 + <%= link_to t('show'), repository_path(repository.id), class: 'btn btn-info' %></td>
52 52 </td>
53 53 </tr>
54 54 <% end %>
... ...
app/views/repositories/_form.html.erb
... ... @@ -106,7 +106,7 @@
106 106 </div>
107 107 <div class="row margin-left-none" style="margin-top: 20px">
108 108 <%= f.submit t('save'), class: 'btn btn-primary' %>
109   - <%= link_to t('back'), project_path(@project_id), class: 'btn btn-default' %>
  109 + <%= link_to t('back'), projects_path, class: 'btn btn-default' %>
110 110 </div>
111 111  
112 112 <script type="text/javascript">
... ...
app/views/repositories/edit.html.erb
... ... @@ -2,6 +2,6 @@
2 2 <h1><%= t_action(:edit, Repository) %></h1>
3 3 </div>
4 4  
5   -<%= form_for(@repository, :url => project_repository_update_url(@project_id, @repository.id), method: :put) do |f| %>
  5 +<%= form_for(@repository, :url => repository_update_url(@repository.id), method: :put) do |f| %>
6 6 <%= render partial: 'form', locals: {f: f} %>
7 7 <% end %>
... ...
app/views/repositories/reload_processing.js.erb
1 1 current_path_splited = window.location.pathname.split("/");
2   -if(current_path_splited[1] != "projects"){
  2 +if(current_path_splited[1] != "repositories"){
3 3 current_path_splited.splice(1,1); // Removes the locale
4 4 }
5 5 current_path = current_path_splited.join("/");
6 6  
7 7 // This if prevents it from updating the repository's state after the user leaves its page
8   -if(current_path == '<%= project_repository_path(project_id: @repository.project_id, id: @repository, locale: nil) %>'){
  8 +if(current_path == '<%= repository_path(id: @repository, locale: nil) %>'){
9 9 $('div#processing_information').html('<%= escape_javascript(render partial: "processing_information") %>');
10   - repository = new Repository.State(<%= @repository.project_id %>, <%= @repository.id %>)
  10 + repository = new Repository.State(<%= @repository.id %>)
11 11 repository.schedule_poll_state('<%= @processing.state %>')
12 12 }
... ...
app/views/repositories/show.html.erb
... ... @@ -39,7 +39,7 @@
39 39  
40 40 <p><strong> <%= t('repository.show.date_processing') %>: </strong></p>
41 41  
42   -<%= form_tag(project_repository_state_with_date_path(@repository.project_id, @repository.id), method: "post", remote: true) do %>
  42 +<%= form_tag(repository_state_with_date_path(@repository.id), method: "post", remote: true) do %>
43 43 <p>
44 44 <%= label_tag :day, t("day") %>:
45 45 <%= select_tag(:day, options_for_select(day_options), :style => "width:55px; margin-top:5px") %>
... ... @@ -71,7 +71,7 @@
71 71 </div>
72 72 <script type="text/javascript">
73 73 $(document).ready(function () {
74   - (new Repository.State(<%= @repository.project_id %>, <%= @repository.id %>)).poll_state('')
  74 + (new Repository.State(<%= @repository.id %>)).poll_state('')
75 75 });
76 76  
77 77 //Loads the accordion
... ... @@ -89,6 +89,6 @@
89 89  
90 90 <%= link_to t('back'), project_path(@repository.project_id), class: 'btn btn-default' %>
91 91 <% if project_owner? @repository.project_id %>
92   - <%= link_to t('repository.show.reprocess'), project_repository_process_path(@repository.project_id, @repository.id), class: 'btn btn-info' %>
93   - <%= link_to t('destroy'), project_repository_path(@repository.project_id, @repository.id), method: :delete, data: { confirm: t('sure_destroy', model: Repository.model_name.human) }, class: 'btn btn-danger' %>
  92 + <%= link_to t('repository.show.reprocess'), repository_process_path(@repository.id), class: 'btn btn-info' %>
  93 + <%= link_to t('destroy'), repository_path(@repository.id), method: :delete, data: { confirm: t('sure_destroy', model: Repository.model_name.human) }, class: 'btn btn-danger' %>
94 94 <% end %>
... ...
config/routes.rb
... ... @@ -4,14 +4,16 @@ Rails.application.routes.draw do
4 4 get 'users/:user_id/projects' => 'users#projects', as: :user_projects
5 5  
6 6 resources :projects do
7   - resources :repositories, except: [:update, :index]
8   - get '/repositories/:id/modules/:module_result_id' => 'repositories#show', as: :repository_module
9   - post '/repositories/:id/state' => 'repositories#state', as: :repository_state
10   - post '/repositories/:id/state_with_date' => 'repositories#state_with_date', as: :repository_state_with_date
11   - put '/repositories/:id' => 'repositories#update', as: :repository_update
12   - get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process
  7 + resources :repositories, only: [:new, :create]
13 8 end
14 9  
  10 + resources :repositories, except: [:update, :index]
  11 + get '/repositories/:id/modules/:module_result_id' => 'repositories#show', as: :repository_module
  12 + post '/repositories/:id/state' => 'repositories#state', as: :repository_state
  13 + post '/repositories/:id/state_with_date' => 'repositories#state_with_date', as: :repository_state_with_date
  14 + put '/repositories/:id' => 'repositories#update', as: :repository_update
  15 + get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process
  16 +
15 17 get '/repository_branches' => 'repositories#branches', as: :repository_branches
16 18  
17 19 resources :kalibro_configurations do
... ...
features/repository/delete.feature
... ... @@ -10,8 +10,9 @@ Feature: Project Deletion
10 10 And I own a sample project
11 11 And I have a sample configuration with native metrics
12 12 And I have a sample repository within the sample project named "QtCalculator"
  13 + And I own that repository
13 14 And I start to process that repository
14 15 And I wait up for a ready processing
15 16 When I visit the repository show page
16 17 And I click the Destroy link
17   - Then I should see "There are no Repositories yet!"
18 18 \ No newline at end of file
  19 + Then I should be in the All Projects page
19 20 \ No newline at end of file
... ...
features/repository/edit.feature
... ... @@ -10,6 +10,7 @@ Feature: Repository Edit
10 10 And I own a sample project
11 11 And I have a sample configuration with native metrics
12 12 And I have a sample repository within the sample project named "QtCalculator"
  13 + And I own that repository
13 14 And I am at repository edit page
14 15 Then the field "Name" should be filled with "QtCalculator"
15 16 And the field "Type" should be filled with "GIT"
... ... @@ -32,6 +33,7 @@ Feature: Repository Edit
32 33 And I own a sample project
33 34 And I have a sample configuration with native metrics
34 35 And I have a sample repository within the sample project named "QtCalculator"
  36 + And I own that repository
35 37 And I am at repository edit page
36 38 When I fill the Name field with " "
37 39 And I fill the Address field with " "
... ... @@ -46,7 +48,9 @@ Feature: Repository Edit
46 48 And I own a sample project
47 49 And I have a sample configuration with native metrics
48 50 And I have a sample repository within the sample project named "MedSquare"
  51 + And I own that repository
49 52 And I have a sample repository within the sample project named "QtCalculator"
  53 + And I own that repository
50 54 And I am at repository edit page
51 55 When I fill the Name field with "MedSquare"
52 56 And I press the Save button
... ...
features/step_definitions/repository_steps.rb
... ... @@ -70,7 +70,7 @@ Given(/^I am at the New Repository page$/) do
70 70 end
71 71  
72 72 Given(/^I am at repository edit page$/) do
73   - visit edit_project_repository_path(project_id: @repository.project_id, id: @repository.id)
  73 + visit edit_repository_path(id: @repository.id)
74 74 end
75 75  
76 76 Given(/^I ask for the last ready processing of the given repository$/) do
... ... @@ -89,6 +89,10 @@ Given(/^I see a sample metric&#39;s name$/) do
89 89 expect(page).to have_content(@metric_results.first.metric_configuration.metric.name)
90 90 end
91 91  
  92 +Given(/^I own that repository$/) do
  93 + FactoryGirl.create(:repository_attributes, {repository_id: @repository.id, user_id: @user.id})
  94 +end
  95 +
92 96 When(/^I click on the sample metric's name$/) do
93 97 find_link(@metric_results.first.metric_configuration.metric.name).trigger('click')
94 98 end
... ... @@ -98,7 +102,7 @@ When(/^I set the select field &quot;(.+)&quot; as &quot;(.+)&quot;$/) do |field, text|
98 102 end
99 103  
100 104 When(/^I visit the repository show page$/) do
101   - visit project_repository_path(project_id: @project.id, id: @repository.id)
  105 + visit repository_path(id: @repository.id)
102 106 end
103 107  
104 108 When(/^I click on the sample child's name$/) do
... ...
spec/controllers/repositories_controller_spec.rb
... ... @@ -65,7 +65,7 @@ describe RepositoriesController, :type =&gt; :controller do
65 65 post :create, project_id: project.id, repository: repository_params
66 66 end
67 67  
68   - it { is_expected.to redirect_to(project_repository_process_path(project_id: repository.project_id, id: repository.id)) }
  68 + it { is_expected.to redirect_to(repository_process_path(id: repository.id)) }
69 69 it { is_expected.to respond_with(:redirect) }
70 70 end
71 71  
... ... @@ -136,7 +136,7 @@ describe RepositoriesController, :type =&gt; :controller do
136 136 delete :destroy, id: repository.id, project_id: project.id.to_s
137 137 end
138 138  
139   - it { is_expected.to redirect_to(project_path(repository.project_id)) }
  139 + it { is_expected.to redirect_to(projects_path) }
140 140 it { is_expected.to respond_with(:redirect) }
141 141 end
142 142  
... ... @@ -226,7 +226,7 @@ describe RepositoriesController, :type =&gt; :controller do
226 226 post :update, project_id: project.id.to_s, id: repository.id, repository: repository_params
227 227 end
228 228  
229   - it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) }
  229 + it { is_expected.to redirect_to(repository_path(repository.id)) }
230 230 it { is_expected.to respond_with(:redirect) }
231 231 end
232 232  
... ... @@ -345,7 +345,7 @@ describe RepositoriesController, :type =&gt; :controller do
345 345 KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
346 346 get :process_repository, project_id: project.id.to_s, id: repository.id
347 347 end
348   - it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) }
  348 + it { is_expected.to redirect_to(repository_path(repository.id)) }
349 349 end
350 350  
351 351 describe 'branches' do
... ...
spec/routing/repositories_routing_spec.rb
... ... @@ -2,29 +2,33 @@ require &quot;rails_helper&quot;
2 2  
3 3 describe RepositoriesController, :type => :routing do
4 4 describe "routing" do
5   - it { is_expected.to route(:post, '/projects/1/repositories').
6   - to(controller: :repositories, action: :create, project_id: 1) }
7   - it { is_expected.to route(:get, '/projects/1/repositories/new').
8   - to(controller: :repositories, action: :new, project_id: 1) }
9   - it { is_expected.to route(:get, '/projects/1/repositories/1/edit').
10   - to(controller: :repositories, action: :edit, project_id: 1, id: 1) }
11   - it { is_expected.to route(:get, '/projects/1/repositories/1').
12   - to(controller: :repositories, action: :show, project_id: 1, id: 1) }
13   - it { is_expected.to route(:get, '/projects/1/repositories/1/modules/1').
14   - to(controller: :repositories, action: :show, project_id: 1, module_result_id: 1, id: 1) }
15   - it { is_expected.to route(:delete, '/projects/1/repositories/1').
16   - to(controller: :repositories, action: :destroy, project_id: 1, id: 1) }
17   - it { is_expected.to route(:put, '/projects/1/repositories/1').
18   - to(controller: :repositories, action: :update, project_id: 1, id: 1) }
19   - it { is_expected.not_to route(:get, '/projects/1/repositories').
20   - to(controller: :repositories, action: :index, project_id: 1) }
21   - it { is_expected.to route(:post, '/projects/1/repositories/1/state').
22   - to(controller: :repositories, action: :state, project_id: 1, id: 1) }
23   - it { is_expected.to route(:post, '/projects/1/repositories/1/state_with_date').
24   - to(controller: :repositories, action: :state_with_date, project_id: 1, id: 1) }
25   - it { is_expected.to route(:get, '/projects/1/repositories/1/process').
26   - to(controller: :repositories, action: :process_repository, project_id: 1, id: 1) }
  5 + it { is_expected.to route(:post, '/repositories').
  6 + to(controller: :repositories, action: :create) }
  7 + it { is_expected.to route(:get, '/repositories/new').
  8 + to(controller: :repositories, action: :new) }
  9 + it { is_expected.to route(:get, '/repositories/1/edit').
  10 + to(controller: :repositories, action: :edit, id: 1) }
  11 + it { is_expected.to route(:get, '/repositories/1').
  12 + to(controller: :repositories, action: :show, id: 1) }
  13 + it { is_expected.to route(:get, '/repositories/1/modules/1').
  14 + to(controller: :repositories, action: :show, module_result_id: 1, id: 1) }
  15 + it { is_expected.to route(:delete, '/repositories/1').
  16 + to(controller: :repositories, action: :destroy, id: 1) }
  17 + it { is_expected.to route(:put, '/repositories/1').
  18 + to(controller: :repositories, action: :update, id: 1) }
  19 + it { is_expected.not_to route(:get, '/repositories').
  20 + to(controller: :repositories, action: :index) }
  21 + it { is_expected.to route(:post, '/repositories/1/state').
  22 + to(controller: :repositories, action: :state, id: 1) }
  23 + it { is_expected.to route(:post, '/repositories/1/state_with_date').
  24 + to(controller: :repositories, action: :state_with_date, id: 1) }
  25 + it { is_expected.to route(:get, '/repositories/1/process').
  26 + to(controller: :repositories, action: :process_repository, id: 1) }
27 27 it { is_expected.to route(:get, '/repository_branches').
28 28 to(controller: :repositories, action: :branches) }
  29 + it { is_expected.to route(:get, '/projects/1/repositories/new').
  30 + to(controller: :repositories, action: :new, project_id: 1) }
  31 + it { is_expected.to route(:post, '/projects/1/repositories').
  32 + to(controller: :repositories, action: :create, project_id: 1) }
29 33 end
30 34 end
... ...