diff --git a/spec/per_element_options_spec.rb b/spec/per_element_options_spec.rb index 69c7d54..0e77a80 100644 --- a/spec/per_element_options_spec.rb +++ b/spec/per_element_options_spec.rb @@ -27,4 +27,16 @@ def value_of(wrapper_id) expect(value_of('post_created_at_input')).to eq(today.strftime('%Y-%m-%d 00:00:00')) end + + # show_time passed to the call is the documented way to turn time on for a + # group of inputs. data-show-time="false" is then the only way for one of + # them to opt out, and the attribute was write-only: the guard assigned + # true and never false. + it 'lets data-show-time="false" opt back out of a call that asked for time' do + page.execute_script("$('.manual_preset_pair').date_range_ext_preset({ show_time: true });") + + pick_today_in('post_title_input') + + expect(value_of('post_title_input')).to eq((today + 1).strftime('%Y-%m-%d')) + end end diff --git a/spec/support/admin.rb b/spec/support/admin.rb index 2548392..e6f62d1 100644 --- a/spec/support/admin.rb +++ b/spec/support/admin.rb @@ -37,6 +37,10 @@ def add_post_resource_with_two_pairs f.input :created_at, as: :string, wrapper_html: { class: 'datetime_preset_pair', data: { show_time: 'true' } } f.input :updated_at, as: :string + # Not .datetime_preset_pair, so the auto-init leaves it alone and the + # spec can drive it with explicit options. + f.input :title, as: :string, + wrapper_html: { class: 'manual_preset_pair', data: { show_time: 'false' } } end f.actions end diff --git a/vendor/assets/javascripts/active_admin_date_range_preset.js b/vendor/assets/javascripts/active_admin_date_range_preset.js index 4ed2d9a..ad2a75e 100644 --- a/vendor/assets/javascripts/active_admin_date_range_preset.js +++ b/vendor/assets/javascripts/active_admin_date_range_preset.js @@ -60,8 +60,11 @@ $(function () { // click time rather than here, so the leak was not even order-dependent. let el_opts = $.extend({}, opts); - if (typeof $this.data('show-time') != 'undefined' && $this.data('show-time').toString() == 'true') { - el_opts.show_time = true; + // The attribute is authoritative in both directions when present. Only + // ever assigning true made it impossible for one element to opt out of + // a show_time the call or the defaults had turned on. + if (typeof $this.data('show-time') != 'undefined') { + el_opts.show_time = $this.data('show-time').toString() == 'true'; } // detect inputs