diff options
| author | cedricnjanga | 2018-04-26 11:31:05 -0700 |
|---|---|---|
| committer | Johan Van Ryseghem | 2018-05-28 12:48:49 +0200 |
| commit | 9f988815907182dd9194d546b19ac4911c804d0e (patch) | |
| tree | 5f6838981ce7b4ce55251f688e82230e018f0e20 | |
| parent | cd3c110893a7b1845e5ff420f7f28d30b0b78825 (diff) | |
| download | chouette-core-9f988815907182dd9194d546b19ac4911c804d0e.tar.bz2 | |
Refs #6433 Use ransackable scope for stop area status filter
| -rw-r--r-- | app/controllers/stop_areas_controller.rb | 26 | ||||
| -rw-r--r-- | app/models/chouette/stop_area.rb | 20 | ||||
| -rw-r--r-- | app/views/referential_stop_areas/show.html.slim | 2 | ||||
| -rw-r--r-- | app/views/routes/show.html.slim | 2 | ||||
| -rw-r--r-- | app/views/stop_areas/_filters.html.slim | 6 | ||||
| -rw-r--r-- | app/views/stop_areas/index.html.slim | 2 | ||||
| -rw-r--r-- | app/views/stop_areas/show.html.slim | 2 |
7 files changed, 27 insertions, 33 deletions
diff --git a/app/controllers/stop_areas_controller.rb b/app/controllers/stop_areas_controller.rb index c3d27cc6c..38363e8ef 100644 --- a/app/controllers/stop_areas_controller.rb +++ b/app/controllers/stop_areas_controller.rb @@ -120,7 +120,6 @@ class StopAreasController < ChouetteController def collection scope = parent.present? ? parent.stop_areas : referential.stop_areas - scope = ransack_status(scope) if params[:q] && params[:q][:status_eq_any] @q = scope.search(params[:q]) if sort_column && sort_direction @@ -206,29 +205,4 @@ class StopAreasController < ChouetteController ] + permitted_custom_fields_params(Chouette::StopArea.custom_fields(stop_area_referential.workgroup)) params.require(:stop_area).permit(fields) end - - # Fake ransack filter - def ransack_status scope - status_param = params[:q][:status_eq_any].reject(&:blank?) - params[:q].delete :status_eq_any - - return scope unless status_param - return scope if status_param.empty? || status_param.length == 3 - - - @status = Hash.new do |hash| - hash[:status_eq_any] = { - in_creation: status_param.include?('in_creation'), - confirmed: status_param.include?('confirmed'), - deactivated: status_param.include?('deactivated'), - } - end - - scope = Chouette::StopArea.where( - "confirmed_at #{(@status[:status_eq_any][:confirmed] || @status[:status_eq_any][:deactivated]) ? "IS NOT NULL" : "IS NULL"} - AND deleted_at #{@status[:status_eq_any][:deactivated] ? "IS NOT NULL" : "IS NULL"}" - ) - - scope - end end diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index b933e1944..479407469 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -383,6 +383,26 @@ module Chouette end end + def self.ransackable_scopes(auth_object = nil) + [:by_status] + end + + + def self.by_status(*statuses) + return Chouette::StopArea.all if statuses.reject(&:blank?).length == 3 || statuses.reject(&:blank?).empty? + + status = { + in_creation: statuses.include?('in_creation'), + confirmed: statuses.include?('confirmed'), + deactivated: statuses.include?('deactivated'), + } + + Chouette::StopArea.where( + "confirmed_at #{status[:confirmed] ? "IS NOT NULL" : "IS NULL"} + AND deleted_at #{status[:deactivated] ? "IS NOT NULL" : "IS NULL"}" + ) + end + def activated? !!(deleted_at.nil? && confirmed_at) end diff --git a/app/views/referential_stop_areas/show.html.slim b/app/views/referential_stop_areas/show.html.slim index beee0383f..06096bbaa 100644 --- a/app/views/referential_stop_areas/show.html.slim +++ b/app/views/referential_stop_areas/show.html.slim @@ -7,7 +7,7 @@ .col-lg-6.col-md-6.col-sm-12.col-xs-12 = definition_list t('metadatas'), { t('id_reflex') => @stop_area.try(:user_objectid), - Chouette::StopArea.tmf(:status) => stop_area_status(@stop_area), + Chouette::StopArea.tmf(:status) => stop_area_status(@stop_area.status), Chouette::StopArea.tmf(:comment) => @stop_area.try(:comment), Chouette::StopArea.tmf(:stop_area_type) => t("area_types.label.#{@stop_area.stop_area_type}"), Chouette::StopArea.tmf(:registration_number) => @stop_area.registration_number, diff --git a/app/views/routes/show.html.slim b/app/views/routes/show.html.slim index 2218bd85f..615913faa 100644 --- a/app/views/routes/show.html.slim +++ b/app/views/routes/show.html.slim @@ -36,7 +36,7 @@ TableBuilderHelper::Column.new( \ key: :status, \ name: Chouette::StopArea.tmf('status'), \ - attribute: Proc.new { |s| stop_area_status(s.try(:stop_area)) } \ + attribute: Proc.new { |s| stop_area_status(s.try(:stop_area).try(:status) ) } \ ), \ TableBuilderHelper::Column.new( \ key: :zip_code, \ diff --git a/app/views/stop_areas/_filters.html.slim b/app/views/stop_areas/_filters.html.slim index 2bc59ce75..88ed6a772 100644 --- a/app/views/stop_areas/_filters.html.slim +++ b/app/views/stop_areas/_filters.html.slim @@ -16,9 +16,9 @@ = f.label Chouette::StopArea.tmf('area_type'), required: false, class: 'control-label' = f.input :area_type_eq_any, checked: params[:q] && params[:q][:area_type_eq_any], collection: Chouette::AreaType.options, as: :check_boxes, label: false, label_method: lambda{|w| ("<span>" + w[0] + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list' } - .form-group.togglable class=filter_item_class(@status, :status_eq_any) - = f.label Chouette::StopArea.tmf('state'), required: false, class: 'control-label' - = f.input :status_eq_any, checked: lambda { |w| @status.try(:[], :status_eq_any).try(:[], w)}, collection: [:in_creation, :confirmed, :deactivated], as: :check_boxes, label: false, label_method: lambda{|w| ("<span>" + stop_area_status(w) + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list' } + .form-group.togglable class=filter_item_class(params[:q], :by_status) + = f.label Chouette::StopArea.tmf('status'), required: false, class: 'control-label' + = f.input :by_status, checked: params[:q] && params[:q][:by_status], collection: [:in_creation, :confirmed, :deactivated], as: :check_boxes, label: false, label_method: lambda{|w| ("<span>" + stop_area_status(w) + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list' } .actions = link_to t('actions.erase'), @workbench, class: 'btn btn-link' diff --git a/app/views/stop_areas/index.html.slim b/app/views/stop_areas/index.html.slim index 62b873c36..e5ca82ee2 100644 --- a/app/views/stop_areas/index.html.slim +++ b/app/views/stop_areas/index.html.slim @@ -33,7 +33,7 @@ ), \ TableBuilderHelper::Column.new( \ key: :status, \ - attribute: Proc.new { |s| stop_area_status(s) } \ + attribute: Proc.new { |s| stop_area_status(s.status) } \ ), \ TableBuilderHelper::Column.new( \ key: :zip_code, \ diff --git a/app/views/stop_areas/show.html.slim b/app/views/stop_areas/show.html.slim index c10d22bfb..c1965c161 100644 --- a/app/views/stop_areas/show.html.slim +++ b/app/views/stop_areas/show.html.slim @@ -21,7 +21,7 @@ Chouette::StopArea.tmf('city_name') => @stop_area.city_name, Chouette::StopArea.tmf('country_code') => @stop_area.country_code.presence || '-', Chouette::StopArea.tmf('time_zone') => @stop_area.time_zone.presence || '-', - Chouette::StopArea.tmf('status') => stop_area_status(@stop_area), + Chouette::StopArea.tmf('status') => stop_area_status(@stop_area.status), Chouette::StopArea.tmf('comment') => @stop_area.try(:comment), }) - @stop_area.custom_fields.each do |code, field| |
