Skip to content

Commit

Permalink
Allow to provide default start/end date for date range
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Kushnir committed Jan 6, 2016
1 parent 0b0aeea commit cb1c5cf
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
26 changes: 15 additions & 11 deletions app/cells/date_range/date_range.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#= require jquery
#= require underscore
#= require moment
#= require ui_components/domchange
#= require bootstrap-daterangepicker

$(document).on 'uic:domchange', (e) ->
today = -> moment().startOf('day')
yesterday = today().subtract(1, 'days')
Expand All @@ -18,30 +24,28 @@ $(document).on 'uic:domchange', (e) ->

defaults = {
ranges: ranges,
startDate: yesterday,
endDate: yesterday,
opens: 'right',
format: 'YYYY-MM-DD',
format: 'YYYY-MM-DD'
}

$(e.target).find('.ui-components-date-range').each (_i, el) ->
$el = $(el)
$start_input = $($el.data().start)
$end_input = $($el.data().end)

start_date = _.find [$start_input.val(), yesterday],
start_date = _.find [$el.data('startDate'), $start_input.val(), yesterday],
(val) -> val && val.toString().length > 0
end_date = _.find [$end_input.val(), yesterday],
end_date = _.find [$el.data('endDate'), $end_input.val(), yesterday],
(val) -> val && val.toString().length > 0

start_date = moment(start_date)
end_date = moment(end_date)
options = _.extend({},
defaults,
_.pick($el.data(), ['dateLimit', 'ranges']),
{ startDate: start_date, endDate: end_date })

options = {}
_.extend(options, defaults)
_.extend(options, _.pick($el.data(), ['dateLimit', 'ranges']))
_.extend(options, { startDate: start_date, endDate: end_date })
options.ranges = _.mapObject(options.ranges, (v, k) -> _.map(v, (v) -> moment(v)))
options.startDate = moment(options.startDate)
options.endDate = moment(options.endDate)

callback = (start, end) ->
end.startOf('day')
Expand Down
7 changes: 5 additions & 2 deletions app/cells/date_range/date_range_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class DateRangeCell < FormCellBase
'start and end dates. Can have any property you can add to a ' \
'[moment](http://momentjs.com/docs/#/durations/creating/) object (i.e. ' \
'days, months)'
attribute :start_date, description: 'Default start date'
attribute :end_date, description: 'Default end date'

def show
[
Expand Down Expand Up @@ -35,8 +37,9 @@ def select_div
end

def data
options.slice(:ranges, :date_limit)
.merge(start: "##{id}_from", end: "##{id}_to")
options.slice(:ranges, :date_limit).merge(
start_date: start_date.to_s, end_date: end_date.to_s,
start: "##{id}_from", end: "##{id}_to")
end

def id
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/app/views/components/date_range.html.slim
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
= bootstrap_form_for :fox do |form|
= ui_component(:date_range, form: form, name: 'report_range')
= bootstrap_form_for :my_form do |form|
= ui_component(:date_range, form: form, name: 'my_date_range')
4 changes: 2 additions & 2 deletions spec/dummy/app/views/components/date_range_custom_ranges.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= bootstrap_form_for :fox do |form|
= bootstrap_form_for :my_form do |form|
= ui_component(:date_range,
form: form,
name: 'report_range',
name: 'my_date_range',
ranges: { 'Next 10 days' => [Date.new(2015, 6, 22), Date.new(2015, 7, 1)] })
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= bootstrap_form_for(:my_form, url: '/') do |f|
= ui_component 'date_range', form: f,
name: 'my_date_range',
date_limit: { days: 7 },
start_date: Date.new(2016, 1, 1),
end_date: Date.new(2016, 1, 21)
11 changes: 10 additions & 1 deletion spec/integration/date_range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def select_range(text)
end

def date(type)
find(:fillable_field, "fox[report_range_#{type}]", visible: false).value
find(:fillable_field, "my_form[my_date_range_#{type}]", visible: false).value
end

def datepicker(type)
Expand Down Expand Up @@ -69,4 +69,13 @@ def datepicker(type)

expect(field.text).to eq('Next 10 days (2015-06-22 - 2015-07-01)')
end

scenario 'start/end dates are configurable' do
visit '/date_range_start_end_date'
field = find('.ui-components-date-range')

expect(field.text).to eq('Custom Range (2016-01-01 - 2016-01-21)')
expect(date(:from)).to eq('2016-01-01')
expect(date(:to)).to eq('2016-01-21')
end
end

0 comments on commit cb1c5cf

Please sign in to comment.