aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcedricnjanga2017-09-06 01:06:47 +0200
committercedricnjanga2017-09-06 09:47:25 +0200
commit53804667d4ba62e29ff687995dfa6d2566139075 (patch)
tree2a1ca835d7d7abccc2e905c6872333ff4916fbb5
parentbcea52e515f1eef544da5cfcbfb5e900c69b1081 (diff)
downloadchouette-core-53804667d4ba62e29ff687995dfa6d2566139075.tar.bz2
Updated imports#index date filter : used the same logic for the Calendar#index date filter (ransackable scope)
I realized that we could submit the form with incomplete date which caused an 500 error. So I added a small JS script to prevent user from subnitting filter form with incomplete dates Since it was really similar with previous work I added two folders : /helpers & /filters to be more DRY The JS files are loaded in 4 views for the moment, hence the update in the assets.rb file
-rw-r--r--app/assets/javascripts/filters/calendar.js6
-rw-r--r--app/assets/javascripts/filters/import.js6
-rw-r--r--app/assets/javascripts/filters/time_table.js7
-rw-r--r--app/assets/javascripts/filters/workbench.js6
-rw-r--r--app/assets/javascripts/helpers/date_filters.js38
-rw-r--r--app/assets/javascripts/time_table.coffee14
-rw-r--r--app/assets/javascripts/workbench.coffee18
-rw-r--r--app/controllers/imports_controller.rb12
-rw-r--r--app/models/import.rb6
-rw-r--r--app/views/calendars/_filters.html.slim2
-rw-r--r--app/views/calendars/index.html.slim6
-rw-r--r--app/views/imports/_filters.html.slim6
-rw-r--r--app/views/imports/index.html.slim2
-rw-r--r--app/views/time_tables/index.html.slim2
-rw-r--r--app/views/workbenches/show.html.slim2
-rw-r--r--config/initializers/assets.rb2
16 files changed, 96 insertions, 39 deletions
diff --git a/app/assets/javascripts/filters/calendar.js b/app/assets/javascripts/filters/calendar.js
new file mode 100644
index 000000000..a4e84777c
--- /dev/null
+++ b/app/assets/javascripts/filters/calendar.js
@@ -0,0 +1,6 @@
+const DateFilter = require('../helpers/date_filters')
+
+$(document).ready(function(){
+ const calendarDF = new DateFilter("#calendar_filter_btn", "Tous les champs du filtre de date doivent ĂȘtre remplis", "#q_contains_date_NUMi")
+ calendarDF()
+})
diff --git a/app/assets/javascripts/filters/import.js b/app/assets/javascripts/filters/import.js
new file mode 100644
index 000000000..d0b96da10
--- /dev/null
+++ b/app/assets/javascripts/filters/import.js
@@ -0,0 +1,6 @@
+const DateFilter = require('../helpers/date_filters')
+
+$(document).ready(function(){
+ const importDF = new DateFilter("#import_filter_btn", "Tous les champs du filtre de date doivent ĂȘtre remplis", "#q_started_on_date_NUMi")
+ importDF()
+})
diff --git a/app/assets/javascripts/filters/time_table.js b/app/assets/javascripts/filters/time_table.js
new file mode 100644
index 000000000..9e24d03fe
--- /dev/null
+++ b/app/assets/javascripts/filters/time_table.js
@@ -0,0 +1,7 @@
+$(document).ready(function(){
+ const DateFilter = require('../helpers/date_filters')
+
+ const timetableDF = new DateFilter("#time_table_filter_btn", window.I18n.fr.time_tables.error_period_filter, "#q_start_date_gteq_NUMi", "#q_end_date_lteq_NUMi")
+
+ timetableDF()
+})
diff --git a/app/assets/javascripts/filters/workbench.js b/app/assets/javascripts/filters/workbench.js
new file mode 100644
index 000000000..af3e13c59
--- /dev/null
+++ b/app/assets/javascripts/filters/workbench.js
@@ -0,0 +1,6 @@
+const DateFilter = require('../helpers/date_filters')
+
+$(document).ready(function(){
+ const workbenchDF = new DateFilter("#referential_filter_btn", window.I18n.fr.referentials.error_period_filter, "#q_validity_period_begin_gteq_NUMi", "#q_validity_period_end_lteq_NUMi")
+ workbenchDF()
+})
diff --git a/app/assets/javascripts/helpers/date_filters.js b/app/assets/javascripts/helpers/date_filters.js
new file mode 100644
index 000000000..1f48bb28f
--- /dev/null
+++ b/app/assets/javascripts/helpers/date_filters.js
@@ -0,0 +1,38 @@
+const DateFilter = function(buttonId, message, ...inputIds) {
+ this.buttonId = buttonId
+ this.inputIds = inputIds
+ this.message = message
+
+ const getVal = (str, key) => {
+ let newStr = str.replace(/NUM/, key)
+ return $(newStr).val()
+ }
+
+ const getDates = () => {
+ return this.inputIds.reduce((arr, id) => {
+ let newIds = [1, 2, 3].map(key => getVal(id, key))
+ arr.push(...newIds)
+ return arr
+ },[])
+ }
+
+ const allInputFilled = () => {
+ return getDates().every(date => !!date)
+ }
+
+ const noInputFilled = () => {
+ return getDates().every(date => !date)
+ }
+
+ const execute = () => {
+ $(this.buttonId).on("click", (e) => {
+ if (allInputFilled() == false && noInputFilled() == false) {
+ e.preventDefault()
+ alert(this.message)
+ }
+ })
+ }
+ return execute
+}
+
+module.exports = DateFilter
diff --git a/app/assets/javascripts/time_table.coffee b/app/assets/javascripts/time_table.coffee
deleted file mode 100644
index 8789cb226..000000000
--- a/app/assets/javascripts/time_table.coffee
+++ /dev/null
@@ -1,14 +0,0 @@
- $(document).on("click", "#time_table_filter_btn", (e) ->
- dates = [1, 2, 3].reduce (arr, key) ->
- arr.push $("#q_start_date_gteq_#{key}i").val(), $("#q_end_date_lteq_#{key}i").val()
- arr
- , []
-
- validDate = dates.every (date) -> !!date
-
- noDate = dates.every (date) -> !date
-
- unless (validDate || noDate)
- e.preventDefault()
- alert(window.I18n.fr.time_tables.error_period_filter)
- )
diff --git a/app/assets/javascripts/workbench.coffee b/app/assets/javascripts/workbench.coffee
deleted file mode 100644
index 0e9fe62a3..000000000
--- a/app/assets/javascripts/workbench.coffee
+++ /dev/null
@@ -1,18 +0,0 @@
- $(document).on("click", "#referential_filter_btn", (e) ->
- dates = [1, 2, 3].reduce (arr, key) ->
- arr.push $("#q_validity_period_begin_gteq_#{key}i").val(), $("#q_validity_period_end_lteq_#{key}i").val()
- arr
- , []
-
- validDate = dates.every (date) -> !!date
-
- noDate = dates.every (date) -> !date
-
- console.log("valid dates :", validDate)
- console.log("no dates :", noDate)
- console.log(dates)
-
- unless (validDate || noDate)
- e.preventDefault()
- alert(window.I18n.fr.referentials.error_period_filter)
- )
diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb
index b1b34731b..97a8f91aa 100644
--- a/app/controllers/imports_controller.rb
+++ b/app/controllers/imports_controller.rb
@@ -1,6 +1,7 @@
class ImportsController < BreadcrumbController
skip_before_action :authenticate_user!, only: [:download]
defaults resource_class: Import, collection_name: 'imports', instance_name: 'import'
+ before_action :ransack_started_on_date, only: [:index]
respond_to :html
belongs_to :workbench
@@ -55,6 +56,17 @@ class ImportsController < BreadcrumbController
private
+ def ransack_started_on_date
+ date =[]
+ if params[:q] && !params[:q]['started_on_date(1i)'].empty?
+ ['started_on_date(1i)', 'started_on_date(2i)', 'started_on_date(3i)'].each do |key|
+ date << params[:q][key].to_i
+ params[:q].delete(key)
+ end
+ params[:q]['started_on_date'] = DateTime.new(*date) rescue nil
+ end
+ end
+
def build_resource
@import ||= WorkbenchImport.new(*resource_params) do |import|
import.workbench = parent
diff --git a/app/models/import.rb b/app/models/import.rb
index ff2f57efc..30aa98273 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -9,6 +9,8 @@ class Import < ActiveRecord::Base
has_many :resources, class_name: "ImportResource", dependent: :destroy
has_many :children, foreign_key: :parent_id, class_name: "Import", dependent: :destroy
+ scope :started_on_date, ->(date) { where('started_at BETWEEN ? AND ?', date.beginning_of_day, date.end_of_day) }
+
extend Enumerize
enumerize :status, in: %i(new pending successful warning failed running aborted canceled), scope: true
@@ -17,6 +19,10 @@ class Import < ActiveRecord::Base
before_create :initialize_fields
+ def self.ransackable_scopes(auth_object = nil)
+ [:started_on_date]
+ end
+
def self.model_name
ActiveModel::Name.new Import, Import, "Import"
end
diff --git a/app/views/calendars/_filters.html.slim b/app/views/calendars/_filters.html.slim
index 4c30f69dc..b5283c1e8 100644
--- a/app/views/calendars/_filters.html.slim
+++ b/app/views/calendars/_filters.html.slim
@@ -19,4 +19,4 @@
.actions
= link_to 'Effacer', calendars_path, class: 'btn btn-link'
- = f.submit 'Filtrer', id: 'filter_btn', class: 'btn btn-default'
+ = f.submit 'Filtrer', id: 'calendar_filter_btn', class: 'btn btn-default'
diff --git a/app/views/calendars/index.html.slim b/app/views/calendars/index.html.slim
index 1e38786b9..56232b0af 100644
--- a/app/views/calendars/index.html.slim
+++ b/app/views/calendars/index.html.slim
@@ -13,7 +13,7 @@
.row
.col-lg-12
= render 'filters'
-
+
- if @calendars.any?
.row
.col-lg-12
@@ -39,8 +39,10 @@
cls: 'table has-filter'
= new_pagination @calendars, 'pull-right'
-
+
- unless @calendars.any?
.row.mt-xs
.col-lg-12
= replacement_msg t('calendars.search_no_results')
+
+= javascript_include_tag 'filters/calendar.js'
diff --git a/app/views/imports/_filters.html.slim b/app/views/imports/_filters.html.slim
index 99fcb0232..a216019b6 100644
--- a/app/views/imports/_filters.html.slim
+++ b/app/views/imports/_filters.html.slim
@@ -9,13 +9,13 @@
.ffg-row
.form-group.togglable
= f.label Import.human_attribute_name(:status), required: false, class: 'control-label'
- = f.input :status_eq_any, collection: @imports.map(&:status).uniq.compact, as: :check_boxes, label: false, label_method: lambda{|l| ("<span>" + l + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list'}
+ = f.input :status_eq_any, collection: @imports.map(&:status).uniq.compact, as: :check_boxes, label: false, label_method: lambda{|l| ("<span>" + import_status(l) + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list'}, input_html: { checked: true}
.form-group.togglable
= f.label Import.human_attribute_name(:started_at), required: false, class: 'control-label'
.filter_menu
- = f.input :started_at_eq, as: :date, label: false, wrapper_html: { class: 'date smart_date filter_menu-item' }, include_blank: true
+ = f.input :started_on_date, as: :date, label: false, wrapper_html: { class: 'date smart_date filter_menu-item' }, include_blank: true
.actions
= link_to t('actions.erase'), workbench_imports_path(@workbench), class: 'btn btn-link'
- = f.submit t('actions.filter'), class: 'btn btn-default'
+ = f.submit t('actions.filter'), id: 'import_filter_btn', class: 'btn btn-default'
diff --git a/app/views/imports/index.html.slim b/app/views/imports/index.html.slim
index 37ae3b3c1..2203d3584 100644
--- a/app/views/imports/index.html.slim
+++ b/app/views/imports/index.html.slim
@@ -47,3 +47,5 @@
.row.mt-xs
.col-lg-12
= replacement_msg t('imports.search_no_results')
+
+= javascript_include_tag 'filters/import.js'
diff --git a/app/views/time_tables/index.html.slim b/app/views/time_tables/index.html.slim
index 1ea5256aa..a1b9c4e09 100644
--- a/app/views/time_tables/index.html.slim
+++ b/app/views/time_tables/index.html.slim
@@ -68,3 +68,5 @@
= javascript_tag do
| window.I18n = #{(I18n.backend.send(:translations).to_json).html_safe};
+
+= javascript_include_tag 'filters/timetable.js'
diff --git a/app/views/workbenches/show.html.slim b/app/views/workbenches/show.html.slim
index 80451ec34..50c719d12 100644
--- a/app/views/workbenches/show.html.slim
+++ b/app/views/workbenches/show.html.slim
@@ -77,3 +77,5 @@
= javascript_tag do
| window.I18n = #{(I18n.backend.send(:translations).to_json).html_safe};
+
+= javascript_include_tag 'filters/workbench.js'
diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb
index b53dba3d8..2ee5982f3 100644
--- a/config/initializers/assets.rb
+++ b/config/initializers/assets.rb
@@ -5,4 +5,4 @@ Rails.application.config.assets.version = '1.0'
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
-Rails.application.config.assets.precompile += %w( base.css es6_browserified/*.js )
+Rails.application.config.assets.precompile += %w( base.css es6_browserified/*.js helpers/*.js filters/*.js)