aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcedricnjanga2017-12-20 09:24:45 +0100
committerLuc Donnet2017-12-21 13:57:03 +0100
commitb6606b25148b958a1f8d5502d95c7db12ef224b0 (patch)
tree1abca4c4cd646830a37f30cc8fb15eceb242b5bc
parente3f7317345555584464c93ce81423d3defd82d57 (diff)
downloadchouette-core-b6606b25148b958a1f8d5502d95c7db12ef224b0.tar.bz2
Fix purchase window date filter
-rw-r--r--app/controllers/purchase_windows_controller.rb13
-rw-r--r--app/models/chouette/purchase_window.rb4
-rw-r--r--app/views/purchase_windows/_filters.html.slim9
3 files changed, 16 insertions, 10 deletions
diff --git a/app/controllers/purchase_windows_controller.rb b/app/controllers/purchase_windows_controller.rb
index b676623ae..a70535150 100644
--- a/app/controllers/purchase_windows_controller.rb
+++ b/app/controllers/purchase_windows_controller.rb
@@ -2,7 +2,7 @@ class PurchaseWindowsController < ChouetteController
include ReferentialSupport
include RansackDateFilter
include PolicyChecker
- before_action only: [:index] { set_date_time_params("bounding_dates", Date) }
+ before_action :ransack_contains_date, only: [:index]
defaults :resource_class => Chouette::PurchaseWindow, collection_name: 'purchase_windows', instance_name: 'purchase_window'
belongs_to :referential
@@ -44,4 +44,15 @@ class PurchaseWindowsController < ChouetteController
}
)
end
+
+ def ransack_contains_date
+ date =[]
+ if params[:q] && !params[:q]['date_ranges(1i)'].empty?
+ ['date_ranges(1i)', 'date_ranges(2i)', 'date_ranges(3i)'].each do |key|
+ date << params[:q][key].to_i
+ params[:q].delete(key)
+ end
+ params[:q]['date_ranges'] = Date.new(*date) rescue nil
+ end
+ end
end
diff --git a/app/models/chouette/purchase_window.rb b/app/models/chouette/purchase_window.rb
index ab5514ee6..8786c7252 100644
--- a/app/models/chouette/purchase_window.rb
+++ b/app/models/chouette/purchase_window.rb
@@ -14,9 +14,7 @@ module Chouette
validates_presence_of :name, :referential
- scope :overlapping, -> (period_range) do
- where("(date_ranges.begin <= :end AND date_ranges.end >= :begin)", {begin: period_range.begin, end: period_range.end})
- end
+ scope :contains_date, ->(date) { where('date ? <@ any (date_ranges)', date) }
def local_id
"IBOO-#{self.referential.id}-#{self.id}"
diff --git a/app/views/purchase_windows/_filters.html.slim b/app/views/purchase_windows/_filters.html.slim
index 8540920cd..9c83d20db 100644
--- a/app/views/purchase_windows/_filters.html.slim
+++ b/app/views/purchase_windows/_filters.html.slim
@@ -6,12 +6,9 @@
button.btn.btn-default#search_btn type='submit'
span.fa.fa-search
- .form-group.togglable
- = f.label Chouette::PurchaseWindow.human_attribute_name(:bounding_dates), required: false, class: 'control-label'
- .filter_menu
- = f.simple_fields_for :bounding_dates do |p|
- = p.input :start_date, as: :date, label: t('simple_form.from'), wrapper_html: { class: 'date smart_date filter_menu-item' }, default: @begin_range, include_blank: @begin_range ? false : true
- = p.input :end_date, as: :date, label: t('simple_form.to'), wrapper_html: { class: 'date smart_date filter_menu-item' }, default: @end_range, include_blank: @end_range ? false : true
+ .form-group
+ = f.label Chouette::PurchaseWindow.human_attribute_name(:date), class: 'control-label'
+ = f.input :date_ranges, as: :date, label: false, wrapper_html: { class: 'date smart_date' }, class: 'form-control', include_blank: true
.actions
= link_to t('actions.erase'), referential_purchase_windows_path, class: 'btn btn-link'