hellobase-formtastic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17f40d559f349df5e85efb5b5498851bed3223a4758e302fb38532c7307b862f
4
+ data.tar.gz: 94535d5ea41c3f828de0b8dd119b0faf09c12459fd9fe3f4a56f32bb2437b725
5
+ SHA512:
6
+ metadata.gz: b7afe70befc28d9bec5a43ef3b4eb1f98ebe831f20a8b3448f7d56a2254d9f5c0d8e2fb4842a03bada14bcf72576619363e372661703c4290b68068b43550d8f
7
+ data.tar.gz: 27ff84df284ee222b63cfdfc7d0b4827daa2d373cd5537a4881e714035b527f6d3f80fc2634563801a3b4405acb8edb78534552ad797674e085e7c0fba3e2664
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 AENEA LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ module Hellobase
2
+ module Formtastic
3
+ module Inputs
4
+ class ContentInput < ::Formtastic::Inputs::StringInput
5
+ def to_html
6
+ input_wrapping do
7
+ label_html <<
8
+ template.content_tag(:span, options[:content])
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,104 @@
1
+ require 'active_admin/inputs'
2
+
3
+ module Hellobase
4
+ module Formtastic
5
+ module Inputs
6
+ class DatepickerWithTimeInput
7
+ include ::Formtastic::Inputs::Base
8
+
9
+ def self.virtual_attributes(base)
10
+ [:"_dpwt_#{base}_d", :"_dpwt_#{base}_h", :"_dpwt_#{base}_m"]
11
+ end
12
+
13
+ def self.define_virtual_attributes(klass, base)
14
+ attrs = virtual_attributes(base)
15
+ ivars = attrs.map {|a| :"@#{a}" }
16
+ method = :"_dpwt_set_#{base}"
17
+
18
+ klass.class_eval do
19
+ define_method attrs[0] do
20
+ send(base)&.strftime('%Y-%m-%d')
21
+ end
22
+
23
+ define_method attrs[1] do
24
+ send(base)&.strftime('%H')
25
+ end
26
+
27
+ define_method attrs[2] do
28
+ send(base)&.strftime('%M')
29
+ end
30
+
31
+ attrs.each_with_index do |a, i|
32
+ define_method :"#{a}=" do |val|
33
+ instance_variable_set ivars[i], val
34
+ send(method)
35
+
36
+ val
37
+ end
38
+ end
39
+
40
+ define_method method do
41
+ send :"#{base}=",
42
+ ivars.any? {|v| instance_variable_get(v).blank? } ?
43
+ nil :
44
+ [
45
+ instance_variable_get(ivars[0]),
46
+ [
47
+ instance_variable_get(ivars[1]),
48
+ instance_variable_get(ivars[2])
49
+ ].join(':')
50
+ ].join(' ')
51
+ end
52
+
53
+ private method
54
+ end
55
+ end
56
+
57
+ def to_html
58
+ datepicker_html = ::ActiveAdmin::Inputs::DatepickerInput.new(builder, template, object, object_name, :"_dpwt_#{method}_d", datepicker_options).to_html
59
+ hour_select_html = ::Formtastic::Inputs::SelectInput.new(builder, template, object, object_name, :"_dpwt_#{method}_h", hour_select_options).to_html
60
+ minute_select_html = ::Formtastic::Inputs::SelectInput.new(builder, template, object, object_name, :"_dpwt_#{method}_m", minute_select_options).to_html
61
+ zone_display_html = options[:time_zone] ? template.content_tag(:span, options[:time_zone]) : nil
62
+
63
+ wrapper_contents = [
64
+ replace_li_tag(datepicker_html),
65
+ replace_li_tag(hour_select_html),
66
+ replace_li_tag(minute_select_html),
67
+ zone_display_html,
68
+ error_html,
69
+ hint_html,
70
+ ].compact.join("\n").html_safe
71
+
72
+ template.content_tag(:li, wrapper_contents, wrapper_html_options)
73
+ end
74
+
75
+ private
76
+
77
+ def datepicker_options
78
+ {
79
+ label: label_text,
80
+ }.merge(options[:datepicker_options] || {})
81
+ end
82
+
83
+ def hour_select_options
84
+ {
85
+ label: false,
86
+ collection: (0..23).map {|i| '%02i' % i },
87
+ }.merge(options[:hour_select_options] || {})
88
+ end
89
+
90
+ def minute_select_options
91
+ {
92
+ label: false,
93
+ collection: (0..59).map {|i| '%02i' % i },
94
+ }.merge(options[:minute_select_options] || {})
95
+ end
96
+
97
+ # formtastic inputs generate <li> with no way to override
98
+ def replace_li_tag(html)
99
+ html.strip.sub(/^<li /, '<span ').sub(/<\/li>$/, '</span>').html_safe
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,15 @@
1
+ module Hellobase
2
+ module Formtastic
3
+ module Inputs
4
+ class InlineCheckboxInput < ::Formtastic::Inputs::BooleanInput
5
+ def to_html
6
+ input_wrapping do
7
+ hidden_field_html <<
8
+ label_html <<
9
+ check_box_html
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Hellobase
2
+ module Formtastic
3
+ module Inputs
4
+ class TimeOfDayInput < ::Formtastic::Inputs::SelectInput
5
+ def initialize(*)
6
+ super
7
+
8
+ @options[:collection] ||= build_times_collection
9
+ end
10
+
11
+ private
12
+
13
+ def build_times_collection
14
+ hours = 0..23
15
+ minutes = (0..59).select {|m| m % 5 == 0 }
16
+ seconds = (0..59).select {|m| m % 60 == 0 }
17
+ times = hours.map {|h| minutes.map {|m| seconds.map {|s| Tod::TimeOfDay.new(h, m, s) } } }.flatten
18
+
19
+ format = '%I:%M %p'
20
+ times.map {|t| [t.strftime(format), t.to_s] }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'hellobase/formtastic/inputs/content_input'
2
+ require 'hellobase/formtastic/inputs/datepicker_with_time_input'
3
+ require 'hellobase/formtastic/inputs/inline_checkbox_input'
4
+ require 'hellobase/formtastic/inputs/time_of_day_input'
5
+
6
+ module Hellobase
7
+ module Formtastic
8
+ module Inputs
9
+ end
10
+ end
11
+ end
12
+
13
+ ::Hellobase::Formtastic::Inputs.constants.each do |c|
14
+ Object.const_set c, ::Hellobase::Formtastic::Inputs.const_get(c)
15
+ end
@@ -0,0 +1,5 @@
1
+ module Hellobase
2
+ module Formtastic
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_support' # formtastic needs ActiveSupport::Autoload
2
+ require 'formtastic'
3
+
4
+ require 'hellobase/formtastic/inputs'
5
+
6
+ module Hellobase
7
+ module Formtastic
8
+ end
9
+ end
data/lib/hellobase.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'hellobase/formtastic'
2
+
3
+ module Hellobase
4
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hellobase-formtastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeadmin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tod
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: actionview
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5'
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1'
125
+ description:
126
+ email:
127
+ - awang@kodia.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - lib/hellobase.rb
134
+ - lib/hellobase/formtastic.rb
135
+ - lib/hellobase/formtastic/inputs.rb
136
+ - lib/hellobase/formtastic/inputs/content_input.rb
137
+ - lib/hellobase/formtastic/inputs/datepicker_with_time_input.rb
138
+ - lib/hellobase/formtastic/inputs/inline_checkbox_input.rb
139
+ - lib/hellobase/formtastic/inputs/time_of_day_input.rb
140
+ - lib/hellobase/formtastic/version.rb
141
+ homepage: https://github.com/K0D1A/hellobase-formtastic
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubygems_version: 3.2.32
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Custom Formtastic inputs
164
+ test_files: []