active_admin_datetimepicker 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fcbf5dabe33507c8eac39d407c1f3834daf47773d6d0ebd7aca2d77291d5401
4
- data.tar.gz: 99facdae8b8ba1ed1f1c5b5be44a888899ccd71524bd735852726715457499f4
3
+ metadata.gz: 1e644517745842200b47507077f8a036c9f546cbdf520cdf032f68b3b3fa3526
4
+ data.tar.gz: c93eb424cc31fd240a759d763db274fbdaa13e737d02026957715a1c03701c13
5
5
  SHA512:
6
- metadata.gz: 8d07649d493e428c8e922025f5374389e89e2340e52087bff17eb963dbb3b82e11f6989643924330410ebd767c29b32e1997d0fba73c90af7a889b5a0b613b61
7
- data.tar.gz: b3505a963858aac440411d5e9a908e8d8290d75ecd353929112187f4e8ada1d688e28ad63e0a115f10793f7942b515f472ec35cf2c043cca23170233fa05ac00
6
+ metadata.gz: 4f54ad3394010f7dcdc6ea7f148bc11ec299b71171394c933d36949234736513f4bcdf58e1c0fec6f4d07e3bf23b04f85f65bdac72d3083a70b78dafe8f6bee3
7
+ data.tar.gz: e003821ff3320c6ead3d802cab1787f9bf758ea9179c3c48d385a9592d2051d3d8c64cc7456cbe7a803ff686da06286c253812b5383d4fd88b00ac5cb2a98a00
data/README.md CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  Adds XDSoft's DateTime picker as a `date_time_picker` input for forms, and `date_time_range` for filters.
10
10
 
11
+ On ActiveAdmin 4 the same inputs render the browser's native date and time controls instead; see [ActiveAdmin 4](#activeadmin-4).
12
+
11
13
  ![ActiveAdminDatetimepicker](https://raw.githubusercontent.com/activeadmin-plugins/active_admin_datetimepicker/master/screen/screen.png "ActiveAdminDatetimepicker")
12
14
 
13
15
  ## Installation
@@ -89,6 +91,40 @@ The `date_time_picker` input accepts many of the options available to the standa
89
91
  end
90
92
  ```
91
93
 
94
+ ## ActiveAdmin 4
95
+
96
+ ActiveAdmin 4 ships without jQuery and Sprockets, so on ActiveAdmin 4 the gem
97
+ does not load the XDSoft picker. The `date_time_picker` input and the
98
+ `date_time_range` filter render native `<input type="datetime-local">` fields
99
+ (`type="date"` for a `date` column), styled by ActiveAdmin itself. There is no
100
+ JavaScript or CSS to install: skip the Sprockets and NPM steps above.
101
+
102
+ Values use the HTML format, `YYYY-MM-DDTHH:MM` (`YYYY-MM-DD` for date fields).
103
+ `ActiveAdminDatetimepicker::Base.format` applies to ActiveAdmin 3 only.
104
+
105
+ Only these `datepicker_options` (and `default_datetime_picker_options`) have a
106
+ native equivalent and are applied on ActiveAdmin 4. Everything else is ignored.
107
+
108
+ | Option | Native attribute |
109
+ | --- | --- |
110
+ | `min_date`, `min_date_time` | `min` |
111
+ | `max_date`, `max_date_time` | `max` |
112
+ | `step` (minutes) | `step` (seconds, `datetime-local` only) |
113
+
114
+ `min`/`max` must be absolute dates (a `Date`, `Time` or a string like
115
+ `"2024-01-31"`); relative values such as `"+3D"` are dropped. An explicit
116
+ `input_html: { min: ... }` wins over the mapped value.
117
+
118
+ Unlike the XDSoft picker, the browser enforces `min`, `max` and `step` when the
119
+ form is submitted. A stored value outside the range, or off the step (10:07 with
120
+ `step: 15`), blocks the save until it is changed.
121
+
122
+ The markup carries `data-*` hooks for tests and other plugins:
123
+
124
+ - every input: `data-datetimepicker-input`
125
+ - the filter wrapper: `data-datetimepicker-range` (on ActiveAdmin 4 it also keeps the `filter_date_time_range` class)
126
+ - the filter inputs: `data-datetimepicker-range-input="from"` and `"to"`
127
+
92
128
  ## Override behaviour in initializer
93
129
 
94
130
  ```ruby
@@ -1,7 +1,16 @@
1
+ # ActiveAdmin 4 submits native datetime-local values ("2024-01-31T10:00").
2
+ # Typed columns cast them to times; for an untyped ransacker the value stays a
3
+ # string, so the "T" is swapped for the space the database compares against.
4
+ iso_to_db = lambda do |value|
5
+ value.is_a?(String) ? value.sub(/\A(\d{4}-\d{2}-\d{2})T(?=\d{2}:\d{2})/, '\1 ') : value
6
+ end
7
+
1
8
  Ransack.configure do |config|
2
9
  config.add_predicate 'gteq_datetime_picker',
3
- arel_predicate: 'gteq'
10
+ arel_predicate: 'gteq',
11
+ formatter: iso_to_db
4
12
 
5
13
  config.add_predicate 'lteq_datetime_picker',
6
- arel_predicate: 'lt'
14
+ arel_predicate: 'lt',
15
+ formatter: iso_to_db
7
16
  end
@@ -5,6 +5,12 @@ module ActiveAdminDatetimepicker
5
5
  mattr_accessor :format
6
6
  @@format = '%Y-%m-%d %H:%M'
7
7
 
8
+ # Value formats the browser accepts for native inputs (HTML spec), used on ActiveAdmin 4.
9
+ NATIVE_FORMATS = {
10
+ 'datetime-local' => '%Y-%m-%dT%H:%M',
11
+ 'date' => '%Y-%m-%d'
12
+ }.freeze
13
+
8
14
  def html_class
9
15
  'date-time-picker'
10
16
  end
@@ -17,9 +23,14 @@ module ActiveAdminDatetimepicker
17
23
  super().tap do |options|
18
24
  options[:class] = [self.options[:class], html_class].compact.join(' ')
19
25
  options[:data] ||= input_html_data
20
- options[:data].merge!(datepicker_options: datetime_picker_options)
21
- options[:value] = input_value(input_name)
22
- options[:maxlength] = 19
26
+ if ActiveAdminDatetimepicker.aa_v4?
27
+ native_input_html_options(options, input_name)
28
+ else
29
+ options[:data].merge!(datepicker_options: datetime_picker_options)
30
+ options[:value] = input_value(input_name)
31
+ options[:maxlength] = 19
32
+ end
33
+ options[:data].merge!(datetimepicker_input: true)
23
34
  options[:placeholder] = placeholder unless placeholder.nil?
24
35
  end
25
36
  end
@@ -45,8 +56,76 @@ module ActiveAdminDatetimepicker
45
56
  end
46
57
  end
47
58
 
59
+ # A date column gets a date-only control; datetime columns and
60
+ # ransackers without a column get a date and time control.
61
+ def native_input_type
62
+ date_column? ? 'date' : 'datetime-local'
63
+ end
64
+
65
+ # Covers DB columns and attributes declared with the Attributes API.
66
+ def date_column?
67
+ return column.type == :date if column&.type
68
+
69
+ object.class.respond_to?(:attribute_types) && object.class.attribute_types[method.to_s]&.type == :date
70
+ end
71
+
72
+ # Stored strings are parsed as leniently as input_value does on ActiveAdmin 3,
73
+ # so a non-ISO value is not blanked and wiped on save.
74
+ def native_input_value(input_name = nil)
75
+ format_native(object.public_send(input_name || method), strict: false)
76
+ end
77
+
48
78
  protected
49
79
 
80
+ def native_input_html_options(options, input_name)
81
+ options[:type] = native_input_type
82
+ options[:value] = native_input_value(input_name)
83
+ options.delete(:maxlength)
84
+ options.delete(:size)
85
+ native_picker_attributes.each { |name, value| options[name] = value unless options.key?(name) }
86
+ end
87
+
88
+ # Only the xdsoft options with a native equivalent are mapped; the rest
89
+ # configure the jQuery widget, which ActiveAdmin 4 pages do not load.
90
+ def native_picker_attributes
91
+ picker_options = datetime_picker_options.transform_keys { |k| k.to_s.camelcase(:lower) }
92
+ attributes = {}
93
+ min = format_native(picker_options['minDateTime']) || native_day_bound(picker_options['minDate'], '00:00')
94
+ max = format_native(picker_options['maxDateTime']) || native_day_bound(picker_options['maxDate'], '23:59')
95
+ attributes[:min] = min if min
96
+ attributes[:max] = max if max
97
+ step = picker_options['step'].to_i
98
+ # xdsoft counts the step in minutes, the HTML attribute in seconds.
99
+ attributes[:step] = step * 60 if step > 0 && native_input_type == 'datetime-local'
100
+ attributes
101
+ end
102
+
103
+ # xdsoft's minDate and maxDate are whole days, so a datetime-local bound
104
+ # starts at 00:00 of the first day and ends at 23:59 of the last.
105
+ def native_day_bound(value, time)
106
+ bound = format_native(value)
107
+ bound && native_input_type == 'datetime-local' ? "#{bound[0, 10]}T#{time}" : bound
108
+ end
109
+
110
+ def format_native(value, strict: true)
111
+ value = value.call if value.respond_to?(:call)
112
+ return nil if value.blank?
113
+ if value.respond_to?(:strftime)
114
+ # Times are shown in the app's zone, like the record values.
115
+ value = value.in_time_zone if value.is_a?(Time) || value.is_a?(DateTime)
116
+ else
117
+ # Relative xdsoft values such as "+3D" have no native equivalent.
118
+ return nil if strict && !value.to_s.match?(%r{\A\d{4}[-/]\d{1,2}[-/]\d{1,2}})
119
+ # A string with an offset names an instant; one without is local wall-clock time.
120
+ offset = Date._parse(value.to_s).key?(:offset)
121
+ value = DateTime.parse(value.to_s)
122
+ value = value.in_time_zone if offset
123
+ end
124
+ value.strftime(NATIVE_FORMATS.fetch(native_input_type))
125
+ rescue ArgumentError
126
+ nil
127
+ end
128
+
50
129
  def _default_datetime_picker_options
51
130
  res = default_datetime_picker_options.map do |k, v|
52
131
  if v.respond_to?(:call) || v.is_a?(Proc)
@@ -59,4 +138,3 @@ module ActiveAdminDatetimepicker
59
138
  end
60
139
  end
61
140
  end
62
-
@@ -5,6 +5,29 @@ module ActiveAdmin
5
5
  include Base
6
6
  include ActiveAdminDatetimepicker::Base
7
7
 
8
+ # ActiveAdmin 4's DateRangeInput renders date_field pairs, which drop
9
+ # the time; this keeps its markup but emits native datetime fields.
10
+ def to_html
11
+ return super unless ActiveAdminDatetimepicker.aa_v4?
12
+
13
+ input_wrapping do
14
+ [ label_html,
15
+ '<div class="filters-form-input-group">',
16
+ builder.text_field(gt_input_name, input_html_options_for(gt_input_name, gt_input_placeholder)),
17
+ builder.text_field(lt_input_name, input_html_options_for(lt_input_name, lt_input_placeholder)),
18
+ '</div>'
19
+ ].join("\n").html_safe
20
+ end
21
+ end
22
+
23
+ def wrapper_html_options
24
+ super.tap do |options|
25
+ # ActiveAdmin 4 dropped the filter_<as> class that active_admin_date_range_preset looks for.
26
+ options[:class] = "#{options[:class]} filter_date_time_range" if ActiveAdminDatetimepicker.aa_v4?
27
+ options[:data] = (options[:data] || {}).merge(datetimepicker_range: true)
28
+ end
29
+ end
30
+
8
31
  def input_html_options(input_name = gt_input_name, placeholder = gt_input_placeholder)
9
32
  super.tap do |options|
10
33
  options[:class] = html_class
@@ -13,16 +36,22 @@ module ActiveAdmin
13
36
 
14
37
  # This method is for compatibility for ActiveAdmin 2.6
15
38
  def input_html_options_for(input_name, placeholder)
16
- super.merge placeholder: placeholder,
17
- value: input_value(input_name)
39
+ options = super
40
+ value = ActiveAdminDatetimepicker.aa_v4? ? native_input_value(input_name) : input_value(input_name)
41
+ bound = input_name == gt_input_name ? 'from' : 'to'
42
+ options.merge(
43
+ placeholder: placeholder,
44
+ value: value,
45
+ data: (options[:data] || {}).merge(datetimepicker_range_input: bound)
46
+ )
18
47
  end
19
48
 
20
49
  def gt_input_name
21
- column && column.type == :date ? super : "#{method}_gteq_datetime_picker"
50
+ date_column? ? super : "#{method}_gteq_datetime_picker"
22
51
  end
23
52
 
24
53
  def lt_input_name
25
- column && column.type == :date ? super : "#{method}_lteq_datetime_picker"
54
+ date_column? ? super : "#{method}_lteq_datetime_picker"
26
55
  end
27
56
 
28
57
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveAdminDatetimepicker
2
- VERSION = "2.2.0"
2
+ VERSION = "2.3.0"
3
3
  end
@@ -1,4 +1,15 @@
1
1
  require 'activeadmin'
2
+
3
+ module ActiveAdminDatetimepicker
4
+ # ActiveAdmin 4 drops jQuery and Sprockets, so the plugin renders native
5
+ # date/time inputs there instead of the xdsoft picker.
6
+ def self.aa_v4?
7
+ return @aa_v4 if defined?(@aa_v4)
8
+
9
+ @aa_v4 = Gem::Version.new(ActiveAdmin::VERSION) >= Gem::Version.new('4.0.0.beta1')
10
+ end
11
+ end
12
+
2
13
  require 'active_admin_datetimepicker/version'
3
14
  require 'active_admin_datetimepicker/base'
4
15
  require 'active_admin_datetimepicker/inputs/date_time_picker_input'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_admin_datetimepicker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Fedoronchuk
@@ -18,7 +18,7 @@ dependencies:
18
18
  version: '3.0'
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
- version: '4.0'
21
+ version: 5.0.0.a
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -28,8 +28,9 @@ dependencies:
28
28
  version: '3.0'
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
- version: '4.0'
32
- description: Integrate jQuery xdan datetimepicker plugin to ActiveAdmin
31
+ version: 5.0.0.a
32
+ description: Integrate jQuery xdan datetimepicker plugin to ActiveAdmin 3, native
33
+ date and time inputs on ActiveAdmin 4
33
34
  email:
34
35
  - fedoronchuk@gmail.com
35
36
  executables: []
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  - !ruby/object:Gem::Version
67
68
  version: '0'
68
69
  requirements: []
69
- rubygems_version: 4.0.20
70
+ rubygems_version: 3.6.9
70
71
  specification_version: 4
71
72
  summary: datetimepicker extension for ActiveAdmin
72
73
  test_files: []