my97-rails 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/my97-rails.rb +12 -1
- data/lib/my97-rails/My97_form_build.rb~ +20 -0
- data/lib/my97-rails/engine.rb +1 -1
- data/lib/my97-rails/my97_form_builder.rb +173 -0
- data/lib/my97-rails/my97_form_builder.rb~ +132 -0
- data/lib/my97-rails/version.rb +1 -1
- metadata +4 -1
data/lib/my97-rails.rb
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
require "my97-rails/my97_form_builder"
|
2
|
+
#
|
3
|
+
# # Make this the default Form Builder. You can delete this if you don't want
|
4
|
+
# form_for to use
|
5
|
+
# # the bootstrap form builder by default
|
6
|
+
ActionView::Base.default_form_builder = My97FormBuilder::FormBuilder
|
7
|
+
#
|
8
|
+
# # Add in our FormHelper methods, so you can use bootstrap_form_for.
|
9
|
+
#ActionView::Base.send :include, My97FormBuilder::FormHelper
|
10
|
+
#
|
1
11
|
require "my97-rails/engine"
|
2
12
|
module My97engine
|
3
|
-
end
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module My97_form_build
|
2
|
+
module FormHelper
|
3
|
+
[:form_for, :fields_for,:simple_form_for].each do |method|
|
4
|
+
module_eval do
|
5
|
+
define_method "bootstrap_#{method}" do |record, *args, &block|
|
6
|
+
# add the TwitterBootstrap builder to the options
|
7
|
+
options = args.extract_options!
|
8
|
+
options[:builder] = BootstrapFormBuilder::FormBuilder
|
9
|
+
|
10
|
+
if method == :form_for
|
11
|
+
options[:html] ||= {}
|
12
|
+
options[:html][:class] ||= 'form-horizontal'
|
13
|
+
end
|
14
|
+
|
15
|
+
# call the original method with our overridden options
|
16
|
+
send method, record, *(args << options), &block
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/my97-rails/engine.rb
CHANGED
@@ -0,0 +1,173 @@
|
|
1
|
+
module My97FormBuilder
|
2
|
+
module FormHelper
|
3
|
+
[:form_for, :fields_for,:simple_form_for].each do |method|
|
4
|
+
module_eval do
|
5
|
+
define_method "#{method}" do |record, *args, &block|
|
6
|
+
options = args.extract_options!
|
7
|
+
options[:builder] = My97FormBuilder::FormBuilder
|
8
|
+
|
9
|
+
if method == :form_for
|
10
|
+
options[:html] ||= {}
|
11
|
+
options[:html][:class] ||= 'form-horizontal'
|
12
|
+
end
|
13
|
+
|
14
|
+
# call the original method with our overridden options
|
15
|
+
send method, record, *(args << options), &block
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
21
|
+
#include FormHelper
|
22
|
+
|
23
|
+
def get_error_text(object, field, options)
|
24
|
+
if object.nil? || options[:hide_errors]
|
25
|
+
""
|
26
|
+
else
|
27
|
+
errors = object.errors[field.to_sym]
|
28
|
+
if errors.empty? then "" else errors.first end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_object_id(field, options)
|
33
|
+
object = @template.instance_variable_get("@#{@object_name}")
|
34
|
+
return options[:id] || object.class.name.underscore + '_' + field.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_label(field, options)
|
38
|
+
labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
|
39
|
+
text = labelOptions[:text] || nil
|
40
|
+
labelTag = label(field, text, labelOptions)
|
41
|
+
end
|
42
|
+
def my97_date_select(field,options ={})
|
43
|
+
id = get_object_id(field, options)
|
44
|
+
options[:class]="Wdate"
|
45
|
+
date =
|
46
|
+
if options['start_date']
|
47
|
+
options['start_date']
|
48
|
+
elsif object.nil?
|
49
|
+
Date.now
|
50
|
+
else
|
51
|
+
object.send(field.to_sym)
|
52
|
+
end
|
53
|
+
date_picker_script = "<script type='text/javascript'>" +
|
54
|
+
"$('##{id}')" +
|
55
|
+
".focus( function() {
|
56
|
+
var options;
|
57
|
+
options = {
|
58
|
+
dateFmt: 'yyyy-MM-dd',
|
59
|
+
autoPickDate: null
|
60
|
+
};
|
61
|
+
return WdatePicker($.extend(options, $(this).data()));
|
62
|
+
});" +"</script>"
|
63
|
+
return basic_date_select(field, options.merge(javascript: date_picker_script))
|
64
|
+
end
|
65
|
+
def basic_date_select(field, options = {})
|
66
|
+
placeholder_text = options[:placeholder_text] || ''
|
67
|
+
id = get_object_id(field, options)
|
68
|
+
|
69
|
+
errorText = get_error_text(object, field, options)
|
70
|
+
wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
|
71
|
+
errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
|
72
|
+
|
73
|
+
labelTag = get_label(field, options)
|
74
|
+
|
75
|
+
date =
|
76
|
+
if options[:start_date]
|
77
|
+
options[:start_date]
|
78
|
+
elsif object.nil?
|
79
|
+
Date.now.utc
|
80
|
+
else
|
81
|
+
object.send(field.to_sym)
|
82
|
+
end
|
83
|
+
|
84
|
+
javascript = options[:javascript] ||
|
85
|
+
"
|
86
|
+
<script>
|
87
|
+
$(function() {
|
88
|
+
var el = $('##{id}');
|
89
|
+
var currentValue = el.val();
|
90
|
+
if(currentValue.trim() == '') return;
|
91
|
+
el.val(new Date(currentValue).toString('dd MMM, yyyy'));
|
92
|
+
});
|
93
|
+
</script>"
|
94
|
+
("<div class='#{wrapperClass}'>" +
|
95
|
+
labelTag +
|
96
|
+
"<div class='controls'>" +
|
97
|
+
text_field(field, {
|
98
|
+
:id => id, :placeholder => placeholder_text, :value => date.to_s,
|
99
|
+
:class => options[:class]
|
100
|
+
}.merge(options[:text_field] || {})) +
|
101
|
+
errorSpan +
|
102
|
+
javascript +
|
103
|
+
"</div>" +
|
104
|
+
"</div>").html_safe
|
105
|
+
end
|
106
|
+
|
107
|
+
def my97_datetime_select(field,options ={})
|
108
|
+
id = get_object_id(field, options)
|
109
|
+
#options[:class]="Wdate"
|
110
|
+
date =
|
111
|
+
if options['start_date']
|
112
|
+
options['start_date']
|
113
|
+
elsif object.nil?
|
114
|
+
Date.now
|
115
|
+
else
|
116
|
+
object.send(field.to_sym)
|
117
|
+
end
|
118
|
+
date_picker_script = "<script type='text/javascript'>" +
|
119
|
+
"$('##{id}')" +
|
120
|
+
".focus( function() {
|
121
|
+
var options;
|
122
|
+
options = {
|
123
|
+
dateFmt: 'yyyy-MM-dd HH:mm:ss',
|
124
|
+
autoPickDate: null
|
125
|
+
};
|
126
|
+
return WdatePicker($.extend(options, $(this).data()));
|
127
|
+
});" +"</script>"
|
128
|
+
return basic_datetime_select(field, options.merge(javascript: date_picker_script))
|
129
|
+
end
|
130
|
+
def basic_datetime_select(field, options = {})
|
131
|
+
placeholder_text = options[:placeholder_text] || ''
|
132
|
+
id = get_object_id(field, options)
|
133
|
+
|
134
|
+
errorText = get_error_text(object, field, options)
|
135
|
+
wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
|
136
|
+
errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
|
137
|
+
|
138
|
+
labelTag = get_label(field, options)
|
139
|
+
|
140
|
+
date_time =
|
141
|
+
if options[:start_time]
|
142
|
+
options[:start_time]
|
143
|
+
elsif object.nil?
|
144
|
+
DateTime.now.utc
|
145
|
+
else
|
146
|
+
object.send(field.to_sym)
|
147
|
+
end
|
148
|
+
|
149
|
+
javascript = options[:javascript] ||
|
150
|
+
"
|
151
|
+
<script>
|
152
|
+
$(function() {
|
153
|
+
var el = $('##{id}');
|
154
|
+
var currentValue = el.val();
|
155
|
+
if(currentValue.trim() == '') return;
|
156
|
+
el.val(new Date(currentValue).toString('dd MMM, yyyy HH:mm'));
|
157
|
+
});
|
158
|
+
</script>"
|
159
|
+
|
160
|
+
("<div class='#{wrapperClass}'>" +
|
161
|
+
labelTag +
|
162
|
+
"<div class='controls'>" +
|
163
|
+
text_field(field, {
|
164
|
+
:id => id, :placeholder => placeholder_text, :value => date_time.to_s,
|
165
|
+
:class => options[:class]
|
166
|
+
}.merge(options[:text_field] || {})) +
|
167
|
+
errorSpan +
|
168
|
+
javascript +
|
169
|
+
"</div>" +
|
170
|
+
"</div>").html_safe
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module My97FormBuilder
|
2
|
+
module FormHelper
|
3
|
+
[:form_for, :fields_for,:simple_form_for].each do |method|
|
4
|
+
module_eval do
|
5
|
+
define_method "#{method}" do |record, *args, &block|
|
6
|
+
options = args.extract_options!
|
7
|
+
options[:builder] = My97FormBuilder::FormBuilder
|
8
|
+
|
9
|
+
if method == :form_for
|
10
|
+
options[:html] ||= {}
|
11
|
+
options[:html][:class] ||= 'form-horizontal'
|
12
|
+
end
|
13
|
+
|
14
|
+
# call the original method with our overridden options
|
15
|
+
send method, record, *(args << options), &block
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
21
|
+
#include FormHelper
|
22
|
+
|
23
|
+
def get_error_text(object, field, options)
|
24
|
+
if object.nil? || options[:hide_errors]
|
25
|
+
""
|
26
|
+
else
|
27
|
+
errors = object.errors[field.to_sym]
|
28
|
+
if errors.empty? then "" else errors.first end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_object_id(field, options)
|
33
|
+
object = @template.instance_variable_get("@#{@object_name}")
|
34
|
+
return options[:id] || object.class.name.underscore + '_' + field.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_label(field, options)
|
38
|
+
labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
|
39
|
+
text = labelOptions[:text] || nil
|
40
|
+
labelTag = label(field, text, labelOptions)
|
41
|
+
end
|
42
|
+
def my97_date_select(field,options ={})
|
43
|
+
id = get_object_id(field, options)
|
44
|
+
options[:class]="Wdate"
|
45
|
+
date =
|
46
|
+
if options['start_date']
|
47
|
+
options['start_date']
|
48
|
+
elsif object.nil?
|
49
|
+
Date.now
|
50
|
+
else
|
51
|
+
object.send(field.to_sym)
|
52
|
+
end
|
53
|
+
date_picker_script = "<script type='text/javascript'>" +
|
54
|
+
"$('##{id}')" +
|
55
|
+
".focus( function() {
|
56
|
+
var options;
|
57
|
+
options = {
|
58
|
+
dateFmt: 'yyyy-MM-dd',
|
59
|
+
autoPickDate: null
|
60
|
+
};
|
61
|
+
return WdatePicker($.extend(options, $(this).data()));
|
62
|
+
});" +"</script>"
|
63
|
+
return basic_date_select(field, options.merge(javascript: date_picker_script))
|
64
|
+
end
|
65
|
+
def basic_date_select(field, options = {})
|
66
|
+
placeholder_text = options[:placeholder_text] || ''
|
67
|
+
id = get_object_id(field, options)
|
68
|
+
|
69
|
+
errorText = get_error_text(object, field, options)
|
70
|
+
wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
|
71
|
+
errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
|
72
|
+
|
73
|
+
labelTag = get_label(field, options)
|
74
|
+
|
75
|
+
date =
|
76
|
+
if options[:start_date]
|
77
|
+
options[:start_date]
|
78
|
+
elsif object.nil?
|
79
|
+
Date.now.utc
|
80
|
+
else
|
81
|
+
object.send(field.to_sym)
|
82
|
+
end
|
83
|
+
|
84
|
+
javascript = options[:javascript] ||
|
85
|
+
"
|
86
|
+
<script>
|
87
|
+
$(function() {
|
88
|
+
var el = $('##{id}');
|
89
|
+
var currentValue = el.val();
|
90
|
+
if(currentValue.trim() == '') return;
|
91
|
+
el.val(new Date(currentValue).toString('dd MMM, yyyy'));
|
92
|
+
});
|
93
|
+
</script>"
|
94
|
+
("<div class='#{wrapperClass}'>" +
|
95
|
+
labelTag +
|
96
|
+
"<div class='controls'>" +
|
97
|
+
text_field(field, {
|
98
|
+
:id => id, :placeholder => placeholder_text, :value => date.to_s,
|
99
|
+
:class => options[:class]
|
100
|
+
}.merge(options[:text_field] || {})) +
|
101
|
+
errorSpan +
|
102
|
+
javascript +
|
103
|
+
"</div>" +
|
104
|
+
"</div>").html_safe
|
105
|
+
end
|
106
|
+
|
107
|
+
def my97_datetime_select(field,options ={})
|
108
|
+
id = get_object_id(field, options)
|
109
|
+
#options[:class]="Wdate"
|
110
|
+
date =
|
111
|
+
if options['start_date']
|
112
|
+
options['start_date']
|
113
|
+
elsif object.nil?
|
114
|
+
Date.now
|
115
|
+
else
|
116
|
+
object.send(field.to_sym)
|
117
|
+
end
|
118
|
+
date_picker_script = "<script type='text/javascript'>" +
|
119
|
+
"$('##{id}')" +
|
120
|
+
".focus( function() {
|
121
|
+
var options;
|
122
|
+
options = {
|
123
|
+
dateFmt: 'yyyy-MM-dd HH:mm:ss',
|
124
|
+
autoPickDate: null
|
125
|
+
};
|
126
|
+
return WdatePicker($.extend(options, $(this).data()));
|
127
|
+
});" +"</script>"
|
128
|
+
return basic_date_select(field, options.merge(javascript: date_picker_script))
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
data/lib/my97-rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my97-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -52,7 +52,10 @@ extra_rdoc_files: []
|
|
52
52
|
files:
|
53
53
|
- lib/my97-rails/engine.rb
|
54
54
|
- lib/my97-rails/engine.rb~
|
55
|
+
- lib/my97-rails/my97_form_builder.rb
|
56
|
+
- lib/my97-rails/my97_form_builder.rb~
|
55
57
|
- lib/my97-rails/version.rb
|
58
|
+
- lib/my97-rails/My97_form_build.rb~
|
56
59
|
- lib/my97-rails.rb
|
57
60
|
- lib/tasks/my97engine_tasks.rake
|
58
61
|
- MIT-LICENSE
|