datefield 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ module ActionView
2
+ DATEFIELD_DEFAULT_OPTIONS = {
3
+ :show_other_months => true,
4
+ :select_other_months => true,
5
+ :date_format => 'yy-mm-dd',
6
+ :show_button_panel => true,
7
+ :constrain_input => false,
8
+ :show_on => 'button',
9
+ :button_image => '/calendar.gif',
10
+ :button_image_only => true,
11
+ :today_button_triggers_today => true
12
+ }
13
+
14
+ module Helpers
15
+ module FormHelper
16
+ def date_field(object_name, method, tag_options = {})
17
+ date_options = tag_options.delete(:date_field) || {}
18
+ date_options.reverse_merge!(DATEFIELD_DEFAULT_OPTIONS)
19
+ today_button = date_options.delete(:today_button_triggers_today)
20
+
21
+ new_date_options = {}
22
+ date_options.each_key do |k|
23
+ new_date_options[k.javascript_camelize] = date_options[k]
24
+ end
25
+
26
+ output = <<-OUTPUT
27
+ #{tag = text_field(object_name, method, tag_options)}
28
+ <script type="text/javascript">
29
+ if ('undefined' === typeof(today_fields)) {
30
+ var today_fields = [];
31
+ }
32
+ $(document).ready(function(ev) {
33
+ $("##{tag.match(/id="([^"]+)"/)[1]}").datepicker(#{new_date_options.to_json});
34
+ OUTPUT
35
+
36
+ if today_button
37
+ output += <<-OUTPUT
38
+ $.datepicker._gotoToday = function(id) {
39
+ var target = $(id);
40
+ var inst = this._getInst(target[0]);
41
+ if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
42
+ inst.selectedDay = inst.currentDay;
43
+ inst.drawMonth = inst.selectedMonth = inst.currentMonth;
44
+ inst.drawYear = inst.selectedYear = inst.currentYear;
45
+ }
46
+ else {
47
+ var date = new Date();
48
+ inst.selectedDay = date.getDate();
49
+ inst.drawMonth = inst.selectedMonth = date.getMonth();
50
+ inst.drawYear = inst.selectedYear = date.getFullYear();
51
+ if ($.inArray(id, today_fields) !== -1) {
52
+ this._setDateDatepicker(target, date);
53
+ this._selectDate(id, this._getDateDatepicker(target));
54
+ }
55
+ }
56
+ this._notifyChange(inst);
57
+ this._adjustDate(target);
58
+ }
59
+ OUTPUT
60
+ end
61
+ output += "})</script>"
62
+ output
63
+ end
64
+ end
65
+
66
+ module FormTagHelper
67
+ def date_field_tag(name, value = nil, options = {})
68
+ date_options = options.delete(:date_field) || {}
69
+ date_options.reverse_merge!(DATEFIELD_DEFAULT_OPTIONS)
70
+ today_button = date_options.delete(:today_button_triggers_today)
71
+
72
+ new_date_options = {}
73
+ date_options.each_key do |k|
74
+ new_date_options[k.javascript_camelize] = date_options[k]
75
+ end
76
+
77
+ output = <<-OUTPUT
78
+ #{tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)}
79
+ <script type="text/javascript">
80
+ if ('undefined' === typeof(today_fields)) {
81
+ var today_fields = [];
82
+ }
83
+ $(document).ready(function(ev) {
84
+ $("##{sanitize_to_id(name)}").datepicker(#{new_date_options.to_json});
85
+ OUTPUT
86
+ if today_button
87
+ output += <<-OUTPUT
88
+ today_fields.push("##{sanitize_to_id(name)}");
89
+ $.datepicker._gotoToday = function(id) {
90
+ var target = $(id);
91
+ var inst = this._getInst(target[0]);
92
+ if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
93
+ inst.selectedDay = inst.currentDay;
94
+ inst.drawMonth = inst.selectedMonth = inst.currentMonth;
95
+ inst.drawYear = inst.selectedYear = inst.currentYear;
96
+ }
97
+ else {
98
+ var date = new Date();
99
+ inst.selectedDay = date.getDate();
100
+ inst.drawMonth = inst.selectedMonth = date.getMonth();
101
+ inst.drawYear = inst.selectedYear = date.getFullYear();
102
+ if ($.inArray(id, today_fields) !== -1) {
103
+ this._setDateDatepicker(target, date);
104
+ this._selectDate(id, this._getDateDatepicker(target));
105
+ }
106
+ }
107
+ this._notifyChange(inst);
108
+ this._adjustDate(target);
109
+ }
110
+ OUTPUT
111
+ end
112
+ output += "})</script>"
113
+ output
114
+ end
115
+ end
116
+
117
+ class FormBuilder
118
+ def date_field(method, options = {})
119
+ @template.send(
120
+ 'date_field',
121
+ @object_name,
122
+ method,
123
+ objectify_options(options))
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,11 @@
1
+ class String
2
+ def javascript_camelize
3
+ self.gsub(/_([a-zA-Z])/) {$1.capitalize}
4
+ end
5
+ end
6
+
7
+ class Symbol
8
+ def javascript_camelize
9
+ self.to_s.javascript_camelize
10
+ end
11
+ end
data/lib/datefield.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'datefield/js_camelize'
2
+ require 'datefield/forms'
3
+
4
+ module Datefield
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ initializer 'static_assets.load_static_assets' do |app|
8
+ app.middleware.use ::ActionDispatch::Static, "#{root}/vendor"
9
+ end
10
+ end
11
+ end
12
+ end
Binary file
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datefield
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Michael Madison
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ description: jQuery adds a datepicker that can be placed on certain input tags, this
31
+ enables users to use regular form tags to do date fields.
32
+ email: cadetstar@hotmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/datefield/forms.rb
38
+ - lib/datefield/js_camelize.rb
39
+ - lib/datefield.rb
40
+ - vendor/calendar.gif
41
+ homepage: https://github.com/simu-michaelm/datefield
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>'
57
+ - !ruby/object:Gem::Version
58
+ version: 1.3.1
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Adds jQuery Datefield helpers to ActionView
65
+ test_files: []