Commit b108aae5a63f6ee4779dfe62c847fff182b6ad0d

Authored by Diego Camarinha
2 parents 9e08db9a 139dbe2c

Merge pull request #185 from mezuro/refactor_repository_state

RepositoriesController#state splited into two simpler actions
app/controllers/repositories_controller.rb
... ... @@ -2,10 +2,10 @@ include OwnershipAuthentication
2 2 include ResourceFinder
3 3  
4 4 class RepositoriesController < ApplicationController
5   - before_action :authenticate_user!, except: [:show, :state]
  5 + before_action :authenticate_user!, except: [:show, :state, :state_with_date]
6 6 before_action :project_owner?, only: [:new, :create]
7 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 10 # GET /projects/1/repositories/1
11 11 # GET /projects/1/repositories/1.json
... ... @@ -64,27 +64,22 @@ class RepositoriesController &lt; ApplicationController
64 64 # POST /projects/1/repositories/1/state
65 65 def state
66 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 70 else
84 71 head :ok, :content_type => 'text/html' # Just don't do anything
85 72 end
86 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 83 # GET /projects/1/repositories/1/process
89 84 def process_repository
90 85 @repository.process
... ... @@ -128,4 +123,16 @@ private
128 123 failed_action(format, 'new')
129 124 end
130 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 138 end
... ...
app/views/repositories/show.html.erb
... ... @@ -34,12 +34,11 @@
34 34  
35 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 38 <p>
39 39 Day: <%= select_tag(:day, options_for_select(day_options), :style => "width:55px; margin-top:5px") %>
40 40 Month: <%= select_tag(:month, options_for_select(month_options), :style => "width:55px; margin-top:5px") %>
41 41 Year: <%= select_tag(:year, options_for_select(year_options), :style => "width:70px; margin-top:5px") %>
42   - <%= hidden_field_tag(:last_state, "") %>
43 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 43 </p>
45 44 <% end %>
... ...
config/routes.rb
... ... @@ -6,6 +6,7 @@ Rails.application.routes.draw do
6 6 resources :repositories, except: [:update, :index]
7 7 get '/repositories/:id/modules/:module_result_id' => 'repositories#show', as: :repository_module
8 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 10 put '/repositories/:id' => 'repositories#update', as: :repository_update
10 11 get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process
11 12 end
... ...
spec/controllers/repositories_controller_spec.rb
... ... @@ -295,19 +295,6 @@ describe RepositoriesController, :type =&gt; :controller do
295 295 it { is_expected.not_to render_with_layout }
296 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 299 context 'with a ERROR state' do
313 300 let(:errored_processing) { FactoryGirl.build(:errored_processing) }
... ... @@ -324,6 +311,21 @@ describe RepositoriesController, :type =&gt; :controller do
324 311 end
325 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 329 describe 'process_repository' do
328 330 let(:repository) { FactoryGirl.build(:repository) }
329 331 before :each do
... ...
spec/routing/repositories_routing_spec.rb
... ... @@ -20,6 +20,8 @@ describe RepositoriesController, :type =&gt; :routing do
20 20 to(controller: :repositories, action: :index, project_id: 1) }
21 21 it { is_expected.to route(:post, '/projects/1/repositories/1/state').
22 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 25 it { is_expected.to route(:get, '/projects/1/repositories/1/process').
24 26 to(controller: :repositories, action: :process_repository, project_id: 1, id: 1) }
25 27 end
... ...