diff options
Diffstat (limited to 'app/controllers/workbenches_controller.rb')
| -rw-r--r-- | app/controllers/workbenches_controller.rb | 88 |
1 files changed, 42 insertions, 46 deletions
diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index d03841356..22a71863a 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -7,21 +7,13 @@ class WorkbenchesController < BreadcrumbController def show scope = resource.all_referentials scope = ransack_associated_lines(scope) - scope = ransack_periode(scope) + scope = ransack_period(scope) scope = ransack_status(scope) - # 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.delete('associated_lines_id_eq') - - @q = scope.ransack(ransack_params) - @wbench_refs = sort_result(@q.result).paginate(page: params[:page], per_page: 30) - @wbench_refs = ModelDecorator.decorate( - @wbench_refs, - with: ReferentialDecorator - ) + @q_for_form = scope.ransack(params[:q]) + @q_for_result = scope.ransack(ransack_params) + @wbench_refs = sort_result(@q_for_result.result).paginate(page: params[:page], per_page: 30) + @wbench_refs = ModelDecorator.decorate(@wbench_refs, with: ReferentialDecorator) show! do build_breadcrumb :show @@ -55,29 +47,34 @@ 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 end # Fake ransack filter - def ransack_periode scope - return scope unless params[:q] && params[:q]['validity_period'] - periode = params[:q]['validity_period'] - return scope if periode['end_lteq(1i)'].blank? || periode['begin_gteq(1i)'].blank? - - begin_range = Date.civil(periode["begin_gteq(1i)"].to_i, periode["begin_gteq(2i)"].to_i, periode["begin_gteq(3i)"].to_i) - end_range = Date.civil(periode["end_lteq(1i)"].to_i, periode["end_lteq(2i)"].to_i, periode["end_lteq(3i)"].to_i) + def ransack_period scope + period = params[:q]['validity_period'] + return scope unless period + + begin + if period['begin_gteq'].kind_of?(Array) + begin_range = Date.new(*period['begin_gteq'].map(&:to_i)) + end_range = Date.new(*period['end_lteq'].map(&:to_i)) + else + begin_range = Date.new(period["begin_gteq(1i)"].to_i, period["begin_gteq(2i)"].to_i, period["begin_gteq(3i)"].to_i) + end_range = Date.new(period["end_lteq(1i)"].to_i, period["end_lteq(2i)"].to_i, period["end_lteq(3i)"].to_i) + end + rescue Exception => e + return scope + end if begin_range > end_range flash.now[:error] = t('referentials.errors.validity_period') @@ -89,29 +86,28 @@ 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 + + # 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 + def ransack_params + copy_params = params[:q].clone + copy_params.delete('associated_lines_id_eq') + copy_params.delete('archived_at_not_null') + copy_params.delete('archived_at_null') + copy_params + end end |
