Commit b108aae5a63f6ee4779dfe62c847fff182b6ad0d
Exists in
colab
and in
4 other branches
Merge pull request #185 from mezuro/refactor_repository_state
RepositoriesController#state splited into two simpler actions
Showing
5 changed files
with
43 additions
and
32 deletions
Show diff stats
app/controllers/repositories_controller.rb
@@ -2,10 +2,10 @@ include OwnershipAuthentication | @@ -2,10 +2,10 @@ include OwnershipAuthentication | ||
2 | include ResourceFinder | 2 | include ResourceFinder |
3 | 3 | ||
4 | class RepositoriesController < ApplicationController | 4 | class RepositoriesController < ApplicationController |
5 | - before_action :authenticate_user!, except: [:show, :state] | 5 | + before_action :authenticate_user!, except: [:show, :state, :state_with_date] |
6 | before_action :project_owner?, only: [:new, :create] | 6 | before_action :project_owner?, only: [:new, :create] |
7 | before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository] | 7 | before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository] |
8 | - before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :process_repository] | 8 | + before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :state_with_date, :process_repository] |
9 | 9 | ||
10 | # GET /projects/1/repositories/1 | 10 | # GET /projects/1/repositories/1 |
11 | # GET /projects/1/repositories/1.json | 11 | # GET /projects/1/repositories/1.json |
@@ -64,27 +64,22 @@ class RepositoriesController < ApplicationController | @@ -64,27 +64,22 @@ class RepositoriesController < ApplicationController | ||
64 | # POST /projects/1/repositories/1/state | 64 | # POST /projects/1/repositories/1/state |
65 | def state | 65 | def state |
66 | if params[:last_state] != 'READY' | 66 | if params[:last_state] != 'READY' |
67 | - if params[:day].nil? | ||
68 | - @processing = @repository.last_processing | ||
69 | - else | ||
70 | - year, month, day = params[:year], params[:month], params[:day] | ||
71 | - @processing = Processing.processing_with_date_of(@repository.id, "#{year}-#{month}-#{day}") | ||
72 | - end | 67 | + @processing = @repository.last_processing |
73 | 68 | ||
74 | - respond_to do |format| | ||
75 | - if @processing.state == 'READY' | ||
76 | - format.js { render action: 'load_ready_processing' } | ||
77 | - elsif @processing.state == 'ERROR' | ||
78 | - format.js { render action: 'load_error' } | ||
79 | - else | ||
80 | - format.js { render action: 'reload_processing' } | ||
81 | - end | ||
82 | - end | 69 | + respond_to_processing_state |
83 | else | 70 | else |
84 | head :ok, :content_type => 'text/html' # Just don't do anything | 71 | head :ok, :content_type => 'text/html' # Just don't do anything |
85 | end | 72 | end |
86 | end | 73 | end |
87 | 74 | ||
75 | + # POST /projects/1/repositories/1/state_with_date | ||
76 | + def state_with_date | ||
77 | + year, month, day = params[:year], params[:month], params[:day] | ||
78 | + @processing = Processing.processing_with_date_of(@repository.id, "#{year}-#{month}-#{day}") | ||
79 | + | ||
80 | + respond_to_processing_state | ||
81 | + end | ||
82 | + | ||
88 | # GET /projects/1/repositories/1/process | 83 | # GET /projects/1/repositories/1/process |
89 | def process_repository | 84 | def process_repository |
90 | @repository.process | 85 | @repository.process |
@@ -128,4 +123,16 @@ private | @@ -128,4 +123,16 @@ private | ||
128 | failed_action(format, 'new') | 123 | failed_action(format, 'new') |
129 | end | 124 | end |
130 | end | 125 | end |
126 | + | ||
127 | + def respond_to_processing_state | ||
128 | + respond_to do |format| | ||
129 | + if @processing.state == 'READY' | ||
130 | + format.js { render action: 'load_ready_processing' } | ||
131 | + elsif @processing.state == 'ERROR' | ||
132 | + format.js { render action: 'load_error' } | ||
133 | + else | ||
134 | + format.js { render action: 'reload_processing' } | ||
135 | + end | ||
136 | + end | ||
137 | + end | ||
131 | end | 138 | end |
app/views/repositories/show.html.erb
@@ -34,12 +34,11 @@ | @@ -34,12 +34,11 @@ | ||
34 | 34 | ||
35 | <p><strong> Retrieve the closest processing information from: </strong></p> | 35 | <p><strong> Retrieve the closest processing information from: </strong></p> |
36 | 36 | ||
37 | -<%= form_tag(project_repository_state_path(@repository.project_id, @repository.id), method: "post", remote: true) do %> | 37 | +<%= form_tag(project_repository_state_with_date_path(@repository.project_id, @repository.id), method: "post", remote: true) do %> |
38 | <p> | 38 | <p> |
39 | Day: <%= select_tag(:day, options_for_select(day_options), :style => "width:55px; margin-top:5px") %> | 39 | Day: <%= select_tag(:day, options_for_select(day_options), :style => "width:55px; margin-top:5px") %> |
40 | Month: <%= select_tag(:month, options_for_select(month_options), :style => "width:55px; margin-top:5px") %> | 40 | Month: <%= select_tag(:month, options_for_select(month_options), :style => "width:55px; margin-top:5px") %> |
41 | Year: <%= select_tag(:year, options_for_select(year_options), :style => "width:70px; margin-top:5px") %> | 41 | Year: <%= select_tag(:year, options_for_select(year_options), :style => "width:70px; margin-top:5px") %> |
42 | - <%= hidden_field_tag(:last_state, "") %> | ||
43 | <%= submit_tag("Search", class: 'btn btn-info', style: 'margin-bottom:5px', onClick: "Module.Repository.set_loader('#{image_tag 'loader.gif'} Loading data. Please, wait.')") %> | 42 | <%= submit_tag("Search", class: 'btn btn-info', style: 'margin-bottom:5px', onClick: "Module.Repository.set_loader('#{image_tag 'loader.gif'} Loading data. Please, wait.')") %> |
44 | </p> | 43 | </p> |
45 | <% end %> | 44 | <% end %> |
config/routes.rb
@@ -6,6 +6,7 @@ Rails.application.routes.draw do | @@ -6,6 +6,7 @@ Rails.application.routes.draw do | ||
6 | resources :repositories, except: [:update, :index] | 6 | resources :repositories, except: [:update, :index] |
7 | get '/repositories/:id/modules/:module_result_id' => 'repositories#show', as: :repository_module | 7 | get '/repositories/:id/modules/:module_result_id' => 'repositories#show', as: :repository_module |
8 | post '/repositories/:id/state' => 'repositories#state', as: :repository_state | 8 | post '/repositories/:id/state' => 'repositories#state', as: :repository_state |
9 | + post '/repositories/:id/state_with_date' => 'repositories#state_with_date', as: :repository_state_with_date | ||
9 | put '/repositories/:id' => 'repositories#update', as: :repository_update | 10 | put '/repositories/:id' => 'repositories#update', as: :repository_update |
10 | get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process | 11 | get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process |
11 | end | 12 | end |
spec/controllers/repositories_controller_spec.rb
@@ -295,19 +295,6 @@ describe RepositoriesController, :type => :controller do | @@ -295,19 +295,6 @@ describe RepositoriesController, :type => :controller do | ||
295 | it { is_expected.not_to render_with_layout } | 295 | it { is_expected.not_to render_with_layout } |
296 | end | 296 | end |
297 | 297 | ||
298 | - context 'with a given date' do | ||
299 | - let(:processing) { FactoryGirl.build(:processing) } | ||
300 | - | ||
301 | - before :each do | ||
302 | - Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | ||
303 | - Processing.expects(:processing_with_date_of).with(repository.id, "2013-11-11").returns(processing) | ||
304 | - | ||
305 | - xhr :get, :state, {project_id: project.id.to_s, id: repository.id, last_state: '', day: '11', month: '11', year: '2013'} | ||
306 | - end | ||
307 | - | ||
308 | - it { is_expected.to respond_with(:ok) } | ||
309 | - it { is_expected.not_to render_with_layout } | ||
310 | - end | ||
311 | 298 | ||
312 | context 'with a ERROR state' do | 299 | context 'with a ERROR state' do |
313 | let(:errored_processing) { FactoryGirl.build(:errored_processing) } | 300 | let(:errored_processing) { FactoryGirl.build(:errored_processing) } |
@@ -324,6 +311,21 @@ describe RepositoriesController, :type => :controller do | @@ -324,6 +311,21 @@ describe RepositoriesController, :type => :controller do | ||
324 | end | 311 | end |
325 | end | 312 | end |
326 | 313 | ||
314 | + describe 'state_with_date' do | ||
315 | + let(:processing) { FactoryGirl.build(:processing) } | ||
316 | + let(:repository) { FactoryGirl.build(:repository) } | ||
317 | + | ||
318 | + before :each do | ||
319 | + Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | ||
320 | + Processing.expects(:processing_with_date_of).with(repository.id, "2013-11-11").returns(processing) | ||
321 | + | ||
322 | + xhr :get, :state_with_date, {project_id: project.id.to_s, id: repository.id, day: '11', month: '11', year: '2013'} | ||
323 | + end | ||
324 | + | ||
325 | + it { is_expected.to respond_with(:ok) } | ||
326 | + it { is_expected.not_to render_with_layout } | ||
327 | + end | ||
328 | + | ||
327 | describe 'process_repository' do | 329 | describe 'process_repository' do |
328 | let(:repository) { FactoryGirl.build(:repository) } | 330 | let(:repository) { FactoryGirl.build(:repository) } |
329 | before :each do | 331 | before :each do |
spec/routing/repositories_routing_spec.rb
@@ -20,6 +20,8 @@ describe RepositoriesController, :type => :routing do | @@ -20,6 +20,8 @@ describe RepositoriesController, :type => :routing do | ||
20 | to(controller: :repositories, action: :index, project_id: 1) } | 20 | to(controller: :repositories, action: :index, project_id: 1) } |
21 | it { is_expected.to route(:post, '/projects/1/repositories/1/state'). | 21 | it { is_expected.to route(:post, '/projects/1/repositories/1/state'). |
22 | to(controller: :repositories, action: :state, project_id: 1, id: 1) } | 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) } | ||
23 | it { is_expected.to route(:get, '/projects/1/repositories/1/process'). | 25 | it { is_expected.to route(:get, '/projects/1/repositories/1/process'). |
24 | to(controller: :repositories, action: :process_repository, project_id: 1, id: 1) } | 26 | to(controller: :repositories, action: :process_repository, project_id: 1, id: 1) } |
25 | end | 27 | end |