Skip to content

Commit ee2ca5f

Browse files
committed
Merge pull request #6 from workgena/master
Add data datepicker_options for DateTimeRangeInput, default time form…
2 parents 6fa602e + d1ca3e0 commit ee2ca5f

File tree

5 files changed

+76
-48
lines changed

5 files changed

+76
-48
lines changed

lib/active_admin_datetimepicker.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
require "activeadmin"
2-
require "xdan-datetimepicker-rails"
3-
require "active_admin_datetimepicker/version"
4-
require "active_admin_datetimepicker/inputs/date_time_picker_input"
5-
require "active_admin_datetimepicker/inputs/filters/date_time_range_input"
1+
require 'activeadmin'
2+
require 'xdan-datetimepicker-rails'
3+
require 'active_admin_datetimepicker/version'
4+
require 'active_admin_datetimepicker/base'
5+
require 'active_admin_datetimepicker/inputs/date_time_picker_input'
6+
require 'active_admin_datetimepicker/inputs/filters/date_time_range_input'
67

78
module ActiveAdminDatetimepicker
8-
module Rails
9-
class Engine < ::Rails::Engine
10-
11-
end
9+
module Rails
10+
class Engine < ::Rails::Engine
1211
end
13-
12+
end
1413
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module ActiveAdminDatetimepicker
2+
module Base
3+
mattr_accessor :default_datetime_picker_options do
4+
{}
5+
end
6+
7+
mattr_accessor :format do
8+
'%Y-%m-%d %H:%M'
9+
end
10+
11+
def html_class
12+
'date-time-picker'
13+
end
14+
15+
def input_html_data
16+
{}
17+
end
18+
19+
def input_html_options(input_name = nil)
20+
options = {}
21+
options[:class] = [self.options[:class], html_class].compact.join(' ')
22+
options[:data] ||= input_html_data
23+
options[:data].merge!(datepicker_options: datetime_picker_options)
24+
options[:value] ||= input_value(input_name)
25+
options[:maxlength] = 19
26+
options
27+
end
28+
29+
def input_value(input_name = nil)
30+
val = object.public_send(input_name || method)
31+
return DateTime.new(val.year, val.month, val.day, val.hour, val.min).strftime(format) if val.is_a?(Time)
32+
val.to_s
33+
end
34+
35+
def datetime_picker_options
36+
@datetime_picker_options ||= begin
37+
# backport support both :datepicker_options AND :datetime_picker_options
38+
options = self.options.fetch(:datepicker_options, {})
39+
options = self.options.fetch(:datetime_picker_options, options)
40+
options = Hash[options.map { |k, v| [k.to_s.camelcase(:lower), v] }]
41+
_default_datetime_picker_options.merge(options)
42+
end
43+
end
44+
45+
protected
46+
47+
def _default_datetime_picker_options
48+
res = default_datetime_picker_options.map do |k, v|
49+
if v.respond_to?(:call) || v.is_a?(Proc)
50+
[k, v.call]
51+
else
52+
[k, v]
53+
end
54+
end
55+
Hash[res]
56+
end
57+
end
58+
end
59+
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
module ActiveAdmin
22
module Inputs
33
class DateTimePickerInput < ::Formtastic::Inputs::StringInput
4-
def input_html_options
5-
super.tap do |options|
6-
options[:class] = [options[:class], "date-time-picker"].compact.join(' ')
7-
options[:data] ||= {}
8-
options[:data].merge! datepicker_options
9-
options[:value] ||= value
10-
end
11-
end
12-
13-
def value
14-
val = object.send(method)
15-
return DateTime.new(val.year, val.month, val.day, val.hour, val.min).strftime("%Y-%m-%d %H:%M") if val.is_a?(Time)
16-
return val if val.nil?
17-
val.to_s
18-
end
19-
20-
private
21-
def datepicker_options
22-
options = self.options.fetch(:datepicker_options, {})
23-
options = Hash[options.map{ |k, v| [k.to_s.camelcase(:lower), v] }]
24-
{ datepicker_options: options }
25-
end
4+
include ActiveAdminDatetimepicker::Base
265
end
276
end
28-
end
7+
end

lib/active_admin_datetimepicker/inputs/filters/date_time_range_input.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@ module Inputs
33
module Filters
44
class DateTimeRangeInput < DateRangeInput
55
include Base
6-
7-
def html_class
8-
"date-time-picker"
9-
end
10-
11-
def format
12-
"%Y-%m-%d %H:%M"
13-
end
6+
include ActiveAdminDatetimepicker::Base
147

158
def input_html_options(input_name = gt_input_name)
16-
current_value = @object.public_send input_name
17-
{ size: 12,
18-
class: html_class,
19-
max: 10,
20-
value: current_value.respond_to?(:strftime) ? current_value.strftime(format) : "" }
9+
super.tap do |options|
10+
options[:class] = html_class
11+
end
2112
end
2213
end
2314
end
2415
end
25-
end
16+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ActiveAdminDatetimepicker
2-
VERSION = "0.1.0"
2+
VERSION = '0.1.1'
33
end

0 commit comments

Comments
 (0)