jquery-ui-form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.document +5 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +109 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +25 -0
  7. data/Rakefile +53 -0
  8. data/VERSION +1 -0
  9. data/jquery-ui-form.gemspec +149 -0
  10. data/lib/generators/jquery_ui_form/install/install_generator.rb +18 -0
  11. data/lib/generators/templates/jquery-ui-form.js +46 -0
  12. data/lib/generators/templates/jquery-ui-form.rb +5 -0
  13. data/lib/generators/templates/jquery-ui-form.sass +115 -0
  14. data/lib/jquery-ui-form.rb +1 -0
  15. data/lib/jquery_ui_form.rb +7 -0
  16. data/lib/jquery_ui_form/form_builder.rb +43 -0
  17. data/lib/jquery_ui_form/helpers.rb +17 -0
  18. data/lib/jquery_ui_form/helpers/button_helper.rb +39 -0
  19. data/lib/jquery_ui_form/helpers/error_helper.rb +39 -0
  20. data/lib/jquery_ui_form/helpers/form_helper.rb +19 -0
  21. data/lib/jquery_ui_form/helpers/input_helper.rb +145 -0
  22. data/lib/jquery_ui_form/helpers/label_helper.rb +33 -0
  23. data/lib/jquery_ui_form/helpers/wrapper_helper.rb +41 -0
  24. data/lib/jquery_ui_form/inputs.rb +20 -0
  25. data/lib/jquery_ui_form/inputs/boolean_input.rb +16 -0
  26. data/lib/jquery_ui_form/inputs/check_boxes_input.rb +75 -0
  27. data/lib/jquery_ui_form/inputs/date_input.rb +24 -0
  28. data/lib/jquery_ui_form/inputs/email_input.rb +12 -0
  29. data/lib/jquery_ui_form/inputs/file_input.rb +12 -0
  30. data/lib/jquery_ui_form/inputs/hidden_input.rb +11 -0
  31. data/lib/jquery_ui_form/inputs/number_input.rb +12 -0
  32. data/lib/jquery_ui_form/inputs/password_input.rb +11 -0
  33. data/lib/jquery_ui_form/inputs/phone_input.rb +12 -0
  34. data/lib/jquery_ui_form/inputs/radio_input.rb +33 -0
  35. data/lib/jquery_ui_form/inputs/range_input.rb +12 -0
  36. data/lib/jquery_ui_form/inputs/search_input.rb +12 -0
  37. data/lib/jquery_ui_form/inputs/select_input.rb +19 -0
  38. data/lib/jquery_ui_form/inputs/string_input.rb +11 -0
  39. data/lib/jquery_ui_form/inputs/text_input.rb +13 -0
  40. data/lib/jquery_ui_form/inputs/url_input.rb +12 -0
  41. data/lib/jquery_ui_form/railtie.rb +14 -0
  42. data/spec/form_helper_spec.rb +22 -0
  43. data/spec/helpers/button_helper_spec.rb +54 -0
  44. data/spec/helpers/error_helper_spec.rb +81 -0
  45. data/spec/helpers/input_helper_spec.rb +18 -0
  46. data/spec/helpers/label_helper_spec.rb +31 -0
  47. data/spec/helpers/wrapper_helper_spec.rb +73 -0
  48. data/spec/inputs/boolean_input_spec.rb +25 -0
  49. data/spec/inputs/check_boxes_input_spec.rb +39 -0
  50. data/spec/inputs/date_input_spec.rb +47 -0
  51. data/spec/inputs/email_input_spec.rb +24 -0
  52. data/spec/inputs/file_input_spec.rb +24 -0
  53. data/spec/inputs/hidden_input_spec.rb +24 -0
  54. data/spec/inputs/number_input_spec.rb +24 -0
  55. data/spec/inputs/password_input_spec.rb +23 -0
  56. data/spec/inputs/phone_input_spec.rb +24 -0
  57. data/spec/inputs/radio_input_spec.rb +39 -0
  58. data/spec/inputs/range_input_spec.rb +24 -0
  59. data/spec/inputs/search_input_spec.rb +24 -0
  60. data/spec/inputs/select_input_spec.rb +28 -0
  61. data/spec/inputs/string_input_spec.rb +24 -0
  62. data/spec/inputs/text_input_spec.rb +24 -0
  63. data/spec/inputs/url_input_spec.rb +24 -0
  64. data/spec/spec_helper.rb +63 -0
  65. metadata +272 -0
@@ -0,0 +1,5 @@
1
+ JqueryUiForm::FormBuilder.use_i18n = false
2
+ JqueryUiForm::FormBuilder.required_string = proc{"<abbr>*</abbr>"}
3
+ JqueryUiForm::FormBuilder.html5_inputs = true
4
+ JqueryUiForm::FormBuilder.placeholder_elements = %w(date email file number password phone search string text url)
5
+ JqueryUiForm::FormBuilder.use_autofocus = true
@@ -0,0 +1,115 @@
1
+ .ui-base-error-messages
2
+ @extend .ui-state-error
3
+ @extend .ui-corner-all
4
+ :margin 0px 0px 5px 0px
5
+ :padding 3px 5px 3px 5px
6
+ .ui-icon
7
+ :float left
8
+ :margin-right 3px
9
+ p
10
+ :padding-bottom 2px
11
+ .ui-form
12
+ @extend .ui-widget-content
13
+ @extend .ui-corner-all
14
+ .ui-fieldset
15
+ @extend .ui-widget-content
16
+ @extend .ui-corner-all
17
+ clear: both
18
+ padding: 10px
19
+ margin: 10px
20
+ .ui-fieldset
21
+ padding: 5px
22
+ margin-left: -5px
23
+ legend
24
+ padding: 3px 10px 3px 10px
25
+ margin-left: 10px
26
+ .ui-form-buttons
27
+ display: block
28
+ clear: both
29
+ padding: 5px 25px 5px 124px
30
+ text-align: left
31
+ .ui-button.ui-state-error
32
+ float: right
33
+ .ui-form-column
34
+ display: block
35
+ float: left
36
+ clear: both
37
+ margin-bottom: 5px
38
+ position: relative
39
+ width: 335px
40
+ &.ui-input-error input, &.ui-input-error textarea, &.ui-input-error select
41
+ //Taken from .ui-state-error-text
42
+ border-color: #cd0a0a
43
+ .ui-form-column
44
+ float: left
45
+ clear: none
46
+ .ui-form-row
47
+ float: left
48
+ clear: both
49
+ .ui-form-column
50
+ float: left
51
+ clear: none
52
+ .ui-form-column.ui-check_boxes-input
53
+ .ui-form-column
54
+ float: left
55
+ clear: none
56
+ .ui-form-row.ui-radio-input, .ui-form-column.ui-radio-input, .ui-form-row.ui-check_boxes-input, .ui-form-column.ui-check_boxes-input
57
+ clear: both
58
+ width: auto
59
+ .ui-form-row.ui-check_boxes-input, .ui-form-column.ui-check_boxes-input
60
+ clear: both
61
+ width: auto
62
+ .ui-form-column.ui-select-input, .ui-form-row.ui-select-input
63
+ height: 24px
64
+ .ui-form-column.ui-hidden-input, .ui-form-row.ui-hidden-input
65
+ display: none
66
+ .ui-fieldset.ui-buttonset
67
+ width: 100%
68
+
69
+ .ui-input-error-message
70
+ font-size: 80%
71
+ @extend .ui-state-error-text
72
+ text-align: right
73
+ font-style: italic
74
+ .ui-check_boxes-input .ui-input-error-message, .ui-radio-input .ui-input-error-message
75
+ margin-top: -7px
76
+ .ui-input-hint
77
+ position: absolute
78
+ top: 5px
79
+ left: 129px
80
+ font-size: 80%
81
+ opacity: .45
82
+ filter: Alpha(Opacity = 45)
83
+ .ui-check_boxes-input .ui-input-hint, .ui-radio-input .ui-input-hint
84
+ top: 27px
85
+ left: 21px
86
+ .ui-boolean-input .ui-input-hint
87
+ left: 139px
88
+ label
89
+ width: 120px
90
+ float: left
91
+ text-align: right
92
+ margin-right: 5px
93
+ margin-top: 4px
94
+ input.ui-string-input, input.ui-password-input, input.ui-email-input, input.ui-phone-input, input.ui-url-input, input.ui-search-input, input.ui-number-input, input.ui-file-input, input.ui-range-input, input.ui-date-input, input.ui-datetime-input, input.ui-time-input
95
+ width: 200px
96
+ padding: 2px
97
+ margin: 0px
98
+ line-height: 1.5em
99
+ input.ui-date-input
100
+ width: 100px
101
+ input.ui-range-input
102
+ margin-top: 2px
103
+ padding-bottom: 0px
104
+ input.ui-search-input
105
+ margin-top: 3px
106
+ textarea.ui-text-input
107
+ width: 200px
108
+ padding: 2px
109
+ margin: 0px
110
+ input.ui-boolean-input, input.ui-radio-input
111
+ margin: 7px 0px 5px 0px
112
+ select.ui-select-input
113
+ max-width: 200px
114
+ margin: 2px 0px 0px 0px
115
+ height: 22px
@@ -0,0 +1 @@
1
+ require 'jquery_ui_form'
@@ -0,0 +1,7 @@
1
+ module JqueryUiForm
2
+ extend ActiveSupport::Autoload
3
+ autoload :FormBuilder
4
+ autoload :Inputs
5
+ autoload :Helpers
6
+ end
7
+ require 'jquery_ui_form/railtie' if defined?(Rails)
@@ -0,0 +1,43 @@
1
+ module JqueryUiForm
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+
4
+ attr_accessor :template
5
+ attr_accessor :autofocus
6
+
7
+ def self.configure(name, value = nil)
8
+ class_attribute(name)
9
+ self.send(:"#{name}=", value)
10
+ end
11
+
12
+ configure :use_i18n, :true
13
+ configure :required_string, proc{"<abbr>*</abbr>"}
14
+ configure :html5_inputs, :true
15
+ configure :placeholder_elements, %w(date email password phone search string text url)
16
+ configure :use_autofocus, true
17
+
18
+ include JqueryUiForm::Helpers::WrapperHelper
19
+ include JqueryUiForm::Helpers::InputHelper
20
+ include JqueryUiForm::Helpers::ErrorHelper
21
+ include JqueryUiForm::Helpers::LabelHelper
22
+ include JqueryUiForm::Helpers::ButtonHelper
23
+
24
+ include JqueryUiForm::Inputs::StringInput
25
+ include JqueryUiForm::Inputs::PasswordInput
26
+ include JqueryUiForm::Inputs::BooleanInput
27
+ include JqueryUiForm::Inputs::SelectInput
28
+ include JqueryUiForm::Inputs::RadioInput
29
+ include JqueryUiForm::Inputs::CheckBoxesInput
30
+ include JqueryUiForm::Inputs::TextInput
31
+ include JqueryUiForm::Inputs::HiddenInput
32
+ include JqueryUiForm::Inputs::FileInput
33
+ # HTML5
34
+ include JqueryUiForm::Inputs::EmailInput
35
+ include JqueryUiForm::Inputs::PhoneInput
36
+ include JqueryUiForm::Inputs::UrlInput
37
+ include JqueryUiForm::Inputs::SearchInput
38
+ include JqueryUiForm::Inputs::NumberInput
39
+ include JqueryUiForm::Inputs::RangeInput
40
+ include JqueryUiForm::Inputs::DateInput
41
+ end
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ module JqueryUiForm
2
+ module Helpers
3
+ autoload :FormHelper, 'jquery_ui_form/helpers/form_helper'
4
+ autoload :WrapperHelper, 'jquery_ui_form/helpers/wrapper_helper'
5
+ autoload :InputHelper, 'jquery_ui_form/helpers/input_helper'
6
+
7
+ autoload :LabelHelper, 'jquery_ui_form/helpers/label_helper'
8
+ autoload :TextFieldHelper, 'jquery_ui_form/helpers/text_field_helper'
9
+ autoload :PasswordFieldHelper, 'jquery_ui_form/helpers/password_field_helper'
10
+ autoload :CheckBoxHelper, 'jquery_ui_form/helpers/check_box_helper'
11
+ autoload :RadioButtonHelper, 'jquery_ui_form/helpers/radio_button_helper'
12
+ autoload :ButtonHelper, 'jquery_ui_form/helpers/button_helper'
13
+ autoload :SelectHelper, 'jquery_ui_form/helpers/select_helper'
14
+
15
+ autoload :ErrorHelper, 'jquery_ui_form/helpers/error_helper'
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ module JqueryUiForm
2
+ module Helpers
3
+ module ButtonHelper
4
+
5
+ def button(name, options = {})
6
+ if use_i18n && !options.delete(:translate)
7
+ name = I18n.t(name)
8
+ elsif name == name.to_sym
9
+ name = name.to_s.upcase.humanize
10
+ end
11
+ (options[:class] ||= "") << " ui-button"
12
+ options[:type] ||= "submit"
13
+
14
+ if (icon = options.delete(:icon))
15
+ options["data-icon"] = "ui-icon-#{icon}"
16
+ end
17
+ template.content_tag(:button,name, options)
18
+ end
19
+
20
+ def submit(name=nil, options = {})
21
+ options.merge!(name) && name = nil if name.is_a?(Hash)
22
+ options[:type] ||= "submit"
23
+ options[:icon] ||= "check"
24
+ name ||= (@object && @object.respond_to?(:new_record?) && @object.new_record? ? :create : :update )
25
+ button(name, options)
26
+ end
27
+
28
+ def cancel(name=nil, options = {})
29
+ options.merge!(name) && name = nil if name.is_a?(Hash)
30
+ options[:type] ||= "reset"
31
+ options[:icon] ||= "close"
32
+ (options[:class] ||= "") << " ui-state-error"
33
+ name ||= :cancel
34
+ button(name, options)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module JqueryUiForm
2
+ module Helpers
3
+ module ErrorHelper
4
+
5
+ def has_errors?(method)
6
+ @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank?
7
+ end
8
+
9
+ def error_message_on(method)
10
+ @object.errors[method.to_sym]
11
+ end
12
+
13
+ def error_messages_on_base
14
+ @object.errors[:base]
15
+ end
16
+
17
+ def inline_error(method)
18
+ return "" unless has_errors?(method)
19
+ template.content_tag(:div, error_message_on(method), :class => "ui-input-error-message")
20
+ end
21
+
22
+ def model_errors(errors=[])
23
+ errors = error_messages_on_base.to_a + errors.to_a
24
+ return "" if errors.empty?
25
+ template.content_tag(:div, :class => "ui-base-error-messages") do
26
+ errors.each do |message|
27
+ template.concat(
28
+ template.content_tag(:p) do
29
+ template.concat(template.content_tag(:span, "", :class => "ui-icon ui-icon-alert"))
30
+ template.concat(template.content_tag(:strong, message))
31
+ end
32
+ )
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ module JqueryUiForm
2
+ module Helpers
3
+ module FormHelper
4
+
5
+ def jquery_form_for(name, *args, &block)
6
+ options = args.last.is_a?(Hash) ? args.pop : {}
7
+ options[:builder] ||= JqueryUiForm::FormBuilder
8
+ options[:html] ||= {}
9
+ (options[:html][:class] ||= "") << " ui-form"
10
+ form_for(name, *(args << options), &block)
11
+ end
12
+
13
+ # def form_for(name, *args, &block)
14
+ # super(name, *args, &block)
15
+ # end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,145 @@
1
+ module JqueryUiForm
2
+ module Helpers
3
+ module InputHelper
4
+
5
+ def input(method, options={})
6
+ row_options = options.delete(:html_row) || {}
7
+
8
+ options[:as] ||= default_input_type(method, options)
9
+ options[:required] ||= method_required?(method)
10
+
11
+ row_options[:class] ||= ""
12
+ row_options[:class] << " ui-#{options[:as]}-input"
13
+
14
+ options[:class] ||= ""
15
+ options[:class] << " ui-#{options[:as]}-input"
16
+ options[:class] << " ui-input-error" if has_errors?(method)
17
+ if use_autofocus && (!self.autofocus)
18
+ options[:autofocus] = true
19
+ self.autofocus = true
20
+ end
21
+ column(row_options) do
22
+ send(:"#{options.delete(:as)}_input", method, options)
23
+ end
24
+ end
25
+
26
+ def basic_input_helper(form_helper_method, type, method, options) #:nodoc:
27
+ label_options = options.delete(:html_label) || {}
28
+ label_options[:required] = options[:required]
29
+ label_options[:label] = options.delete(:label)
30
+ hint = options[:placeholder] = options.delete(:hint)
31
+ label(method, label_options) <<
32
+ send(respond_to?(form_helper_method) ? form_helper_method : :text_field, method, options) <<
33
+ inline_hint(hint, type) <<
34
+ inline_error(method)
35
+ end
36
+
37
+ def inline_hint(hint, type = nil)
38
+ return "" if hint.blank?
39
+ if use_i18n
40
+ hint = I18n.t(hint,:scope => [:helpers, :hints])
41
+ end
42
+ return "" if html5_inputs && placeholder_elements.include?(type.to_s)
43
+ template.content_tag(:div, hint, :class => "ui-input-hint")
44
+ end
45
+
46
+ protected
47
+
48
+ # Determins if the attribute should be considered required or not.
49
+ #
50
+ # * if the :required option isn't provided in the options hash, and the object is
51
+ # an ActiveModel, true is returned
52
+ # if the validates_presence_of macro has been used in the class for this attribute, or false
53
+ # otherwise.
54
+ def method_required?(attribute) #:nodoc:
55
+ attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym
56
+ if @object && @object.class.respond_to?(:validators_on)
57
+ !@object.class.validators_on(attribute_sym).find{|validator| (validator.kind == :presence || validator.kind == :inclusion) && (validator.options.present? ? options_require_validation?(validator.options) : true)}.nil?
58
+ else
59
+ false
60
+ end
61
+ end
62
+
63
+ # Determines whether the given options evaluate to true
64
+ def options_require_validation?(options) #nodoc
65
+ allow_blank = options[:allow_blank]
66
+ return !allow_blank unless allow_blank.nil?
67
+ if_condition = !options[:if].nil?
68
+ condition = if_condition ? options[:if] : options[:unless]
69
+
70
+ condition = if condition.respond_to?(:call)
71
+ condition.call(@object)
72
+ elsif condition.is_a?(::Symbol) && @object.respond_to?(condition)
73
+ @object.send(condition)
74
+ else
75
+ condition
76
+ end
77
+
78
+ if_condition ? !!condition : !condition
79
+ end
80
+
81
+ def default_input_type(method, options)
82
+ if column = column_for(method)
83
+ # Special cases where the column type doesn't map to an input method.
84
+ case column.type
85
+ when :string
86
+ return :password if method.to_s =~ /password/
87
+ if html5_inputs
88
+ return :search if method.to_s =~ /^search$/
89
+ return :phone if method.to_s =~ /^phone$|fax$|mobile/
90
+ return :url if method.to_s =~ /^url$|^website$|_url$/
91
+ return :email if method.to_s =~ /email/
92
+ # return :date if method.to_s =~ /date$/
93
+ end
94
+ when :integer
95
+ return :select unless options.key?(:collection)
96
+ return :number
97
+ when :float, :decimal
98
+ return :number
99
+ when :timestamp
100
+ return :datetime
101
+ when :date
102
+ return :date
103
+ when :text
104
+ return :text
105
+ end
106
+
107
+ # Try look for hints in options hash. Quite common senario: Enum keys stored as string in the database.
108
+ return :select if column.type == :string && options.key?(:collection)
109
+ # Try 3: Assume the input name will be the same as the column type (e.g. string_input).
110
+ return column.type
111
+ else
112
+ if @object
113
+ return :select if reflection_for(method)
114
+ end
115
+
116
+ return :select if options.key?(:collection)
117
+ return :password if method.to_s =~ /password/
118
+ return :text if method.to_s =~ /notes/
119
+ return :file if method.to_s =~ /attachment/
120
+ if html5_inputs
121
+ return :email if method.to_s =~ /email/
122
+ return :phone if method.to_s =~ /^phone$|fax$|mobile/
123
+ return :url if method.to_s =~ /^url$|^website$|_url$/
124
+ return :search if method.to_s =~ /^search$/
125
+ return :number if method.to_s =~ /^amount|size/
126
+ end
127
+ return :string
128
+ end
129
+ end
130
+
131
+ # Get a column object for a specified attribute method - if possible.
132
+ def column_for(method) #:nodoc:
133
+ @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute)
134
+ end
135
+
136
+ def reflection_for(method) #:nodoc:
137
+ @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
138
+ end
139
+
140
+ def name_to_id(name)
141
+ name.to_s.gsub(/\s/, "_").gsub(/\W/, "").gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "").downcase
142
+ end
143
+ end
144
+ end
145
+ end