diff options
| author | Xinhui | 2017-07-03 16:23:00 +0200 |
|---|---|---|
| committer | Xinhui | 2017-07-03 16:35:51 +0200 |
| commit | adc8e323d6f76baf50d649b47bf83f08468681c0 (patch) | |
| tree | f7b01912df37e67e522f3c86f5ec6c7e0a548616 | |
| parent | 69723fff92b21b4fff722cefe1ca169ed308f5b2 (diff) | |
| download | chouette-core-adc8e323d6f76baf50d649b47bf83f08468681c0.tar.bz2 | |
Refactoring workbench_controller ransack_status
refs #3936
| -rw-r--r-- | app/controllers/workbenches_controller.rb | 41 | ||||
| -rw-r--r-- | spec/features/workbenches_spec.rb | 48 |
2 files changed, 53 insertions, 36 deletions
diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index d03841356..a53e21269 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -13,8 +13,10 @@ class WorkbenchesController < BreadcrumbController # Ignore archived_at_not_null/archived_at_null managed by ransack_status scope # We clone params[:q] so we can delete fake ransack filter arguments before calling search method, # which will allow us to preserve params[:q] for sorting - ransack_params = params[:q].merge(archived_at_not_null: nil, archived_at_null: nil).clone + ransack_params = params[:q].clone ransack_params.delete('associated_lines_id_eq') + ransack_params.delete('archived_at_not_null') + ransack_params.delete('archived_at_null') @q = scope.ransack(ransack_params) @wbench_refs = sort_result(@q.result).paginate(page: params[:page], per_page: 30) @@ -55,16 +57,13 @@ class WorkbenchesController < BreadcrumbController end def query_params - if params[:q].present? - params[:q].delete_if { |query, value| value.blank? } - else - params[:q] = { "archived_at_not_null"=>"1", "archived_at_null"=>"1" } - end + params[:q] ||= {} + params[:q].delete_if { |query, value| value.blank? } end # Fake ransack filter def ransack_associated_lines scope - if params[:q] && params[:q]['associated_lines_id_eq'] + if params[:q]['associated_lines_id_eq'] scope = scope.include_metadatas_lines([params[:q]['associated_lines_id_eq']]) end scope @@ -89,29 +88,17 @@ class WorkbenchesController < BreadcrumbController scope end - # Fake (again) ransack filter + # Fake ransack filter def ransack_status scope - return scope unless params[:q] - - archived_at_not_null = params[:q]['archived_at_not_null'] == '1' - archived_at_null = params[:q]['archived_at_null'] == '1' - - if !archived_at_not_null and !archived_at_null - return scope.none - end + archived = !params[:q]['archived_at_not_null'].to_i.zero? + unarchived = !params[:q]['archived_at_null'].to_i.zero? - if archived_at_not_null and archived_at_null - return scope - end - - if archived_at_null - return scope.where(archived_at: nil) - end - - if archived_at_not_null - return scope.where("archived_at is not null") - end + # Both status checked, means no filter + return scope unless archived || unarchived + return scope if archived && unarchived + scope = scope.where(archived_at: nil) if unarchived + scope = scope.where("archived_at is not null") if archived scope end end diff --git a/spec/features/workbenches_spec.rb b/spec/features/workbenches_spec.rb index 0d05711ca..3c7d03bcb 100644 --- a/spec/features/workbenches_spec.rb +++ b/spec/features/workbenches_spec.rb @@ -42,22 +42,52 @@ describe 'Workbenches', type: :feature do end end - context 'filter by organisation' do - it 'should be possible to filter by organisation' do - find("#q_organisation_name_eq_any_#{@user.organisation.name.parameterize.underscore}").set(true) + # context 'filter by organisation' do + # it 'should be possible to filter by organisation' do + # find("#q_organisation_name_eq_any_#{@user.organisation.name.parameterize.underscore}").set(true) + # click_button 'Filtrer' + + # expect(page).to have_content(referential.name) + # expect(page).not_to have_content(other_referential.name) + # end + + # it 'should be possible to filter by multiple organisation' do + # find("#q_organisation_name_eq_any_#{@user.organisation.name.parameterize.underscore}").set(true) + # find("#q_organisation_name_eq_any_#{another_organisation.name.parameterize.underscore}").set(true) + # click_button 'Filtrer' + + # expect(page).to have_content(referential.name) + # expect(page).to have_content(other_referential.name) + # end + # end + + context 'filter by status' do + it 'should display archived referentials' do + other_referential.update_attribute(:archived_at, Date.today) + find("#q_archived_at_not_null").set(true) + click_button 'Filtrer' + expect(page).to have_content(other_referential.name) + expect(page).to_not have_content(referential.name) + end + it 'should display both archived and unarchived referentials' do + other_referential.update_attribute(:archived_at, Date.today) + find("#q_archived_at_not_null").set(true) + find("#q_archived_at_null").set(true) + + click_button 'Filtrer' expect(page).to have_content(referential.name) - expect(page).not_to have_content(other_referential.name) + expect(page).to have_content(other_referential.name) end - it 'should be possible to filter by multiple organisation' do - find("#q_organisation_name_eq_any_#{@user.organisation.name.parameterize.underscore}").set(true) - find("#q_organisation_name_eq_any_#{another_organisation.name.parameterize.underscore}").set(true) - click_button 'Filtrer' + it 'should display unarchived referentials' do + other_referential.update_attribute(:archived_at, Date.today) + find("#q_archived_at_null").set(true) + click_button 'Filtrer' expect(page).to have_content(referential.name) - expect(page).to have_content(other_referential.name) + expect(page).to_not have_content(other_referential.name) end end end |
