bootstrap_form_builder_new 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Tom Canham
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.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = BootstrapFormBuilder
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'BootstrapFormBuilder'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ begin
24
+ require 'rspec/core/rake_task'
25
+
26
+ RSpec::Core::RakeTask.new('spec')
27
+
28
+ # If you want to make this the default task
29
+ task :default => :spec
30
+ rescue
31
+ end
32
+
33
+
34
+ Bundler::GemHelper.install_tasks
35
+
@@ -0,0 +1 @@
1
+ require 'bootstrap_form_builder/form_builder'
@@ -0,0 +1,229 @@
1
+ module BootstrapFormBuilder
2
+ class StringList < Array
3
+ def initialize(*args)
4
+ super()
5
+
6
+ args.each do |arg|
7
+ if arg.is_a?(Array)
8
+ concat(arg)
9
+ else
10
+ concat(Array((arg || '').to_s.split))
11
+ end
12
+ end
13
+ end
14
+
15
+ def sort(*args)
16
+ StringList.new(super)
17
+ end
18
+
19
+ def &(other)
20
+ case other
21
+ when String
22
+ StringList.new(super(other.split.flatten.compact.uniq))
23
+ else
24
+ StringList.new(super)
25
+ end
26
+ end
27
+
28
+ def +(*args)
29
+ StringList.new(super(Array(args)))
30
+ end
31
+
32
+ def -(*args)
33
+ StringList.new(super(Array(args)))
34
+ end
35
+
36
+ def ==(other)
37
+ case other
38
+ when StringList, Array
39
+ to_a.sort == normalize_array(other.to_a).sort
40
+ else
41
+ to_s == other.to_s
42
+ end
43
+ end
44
+
45
+ def to_a
46
+ normalize_array(super)
47
+ end
48
+
49
+ def to_s
50
+ to_a.join(' ')
51
+ end
52
+
53
+ def normalize!
54
+ flatten!
55
+ compact!
56
+ uniq!
57
+ self
58
+ end
59
+
60
+ private
61
+ def normalize_array(a)
62
+ a.flatten.compact.uniq
63
+ end
64
+ end
65
+
66
+ class ParsedArgs
67
+ attr_accessor :classes
68
+
69
+ def initialize(*args)
70
+ @options = args.extract_options!
71
+ @args = args
72
+ @classes = StringList.new(@options.delete(:class))
73
+ end
74
+
75
+ def options
76
+ if @classes.empty?
77
+ @options
78
+ else
79
+ @options.merge(class: @classes.to_s)
80
+ end
81
+ end
82
+
83
+ def shift
84
+ @args.shift
85
+ end
86
+
87
+ def to_a
88
+ if @classes.empty? && @options.empty?
89
+ @args
90
+ else
91
+ @args + [options]
92
+ end
93
+ end
94
+ end
95
+
96
+ class FormBuilder < ActionView::Helpers::FormBuilder
97
+ cattr_accessor :default_layout
98
+ attr_accessor :layout
99
+
100
+ def initialize(object_name, object, template, options, proc)
101
+ options[:html] ||= {}
102
+ form_classes = StringList.new(options[:html][:class])
103
+
104
+ unless form_classes.include?('form')
105
+ form_classes += 'form'
106
+ layout = options.delete(:layout).try(:to_sym) || BootstrapFormBuilder::FormBuilder.default_layout
107
+
108
+ if layout.present?
109
+ case layout
110
+ when :horizontal
111
+ form_classes -= 'form-inline'
112
+ form_classes += 'form-horizontal'
113
+ when :inline
114
+ form_classes -= 'form-horizontal'
115
+ form_classes += 'form-inline'
116
+ end
117
+
118
+ options[:html][:class] = form_classes.to_s
119
+ end
120
+ end
121
+
122
+ super
123
+ end
124
+
125
+ def text_field(*args)
126
+ control_group(*args) { super }
127
+ end
128
+
129
+ def select(*args)
130
+ control_group(*args) { super }
131
+ end
132
+
133
+ def check_box(method, *args)
134
+ args = ParsedArgs.new(*args)
135
+ tooltip = args.options.delete(:tooltip)
136
+
137
+ @template.content_tag(:div, class: "control-group") do
138
+ @template.content_tag(:div, class: "controls") do
139
+ @template.content_tag(:label, class: "checkbox") do
140
+ super + method.to_s.titleize +
141
+ (tooltip.present? ? help_icon(tooltip) : ''.html_safe)
142
+ end
143
+ end
144
+ end
145
+ end
146
+
147
+ def number_field(*args)
148
+ control_group(*args) { super }
149
+ end
150
+
151
+ def password_field(*args)
152
+ control_group(*args) { super }
153
+ end
154
+
155
+ def email_field(*args)
156
+ control_group(*args) { super }
157
+ end
158
+
159
+ def actions()
160
+ @template.content_tag(:div, class: 'form-actions') do
161
+ yield
162
+ end
163
+ end
164
+
165
+ def submit(text=nil, *args)
166
+ options = args.extract_options!
167
+ super(text || options.delete(:submit_text), merge_classes(options, "btn btn-primary"))
168
+ end
169
+
170
+ private
171
+ def control_group(*args)
172
+ args = ParsedArgs.new(*args)
173
+
174
+ method = args.shift
175
+ tooltip = args.options.delete(:tooltip)
176
+ label_text = args.options.delete(:label_text)
177
+
178
+ args.classes << 'control-group'
179
+ if @object.errors[method].any?
180
+ args.classes << 'error'
181
+ end
182
+
183
+ @template.content_tag(:div, class: args.classes.to_s) do
184
+ label(method, label_text, *args) +
185
+ controls(method, *args) do
186
+ @template.capture { yield } +
187
+ (tooltip.present? ? help_icon(tooltip) : ''.html_safe)
188
+ end
189
+ end
190
+ end
191
+
192
+ def controls(*args)
193
+ @template.content_tag(:div, class: 'controls') do
194
+ yield
195
+ end
196
+ end
197
+
198
+ def help_icon(text, placement = nil)
199
+ @template.link_to('#', rel: 'tooltip', data: {:'original-title' => text}) do
200
+ @template.content_tag(:i, ' ', class: classes('icon-question-sign', 'icon-blue', 'help-hover-over'))
201
+ end
202
+ end
203
+
204
+ def merge_classes(options, *args)
205
+ options.merge({class: classes(options[:class], *args)})
206
+ end
207
+
208
+ def classes(*args)
209
+ result_parts = []
210
+ args.each do |arg|
211
+ unless arg.nil?
212
+ if arg.is_a?(String)
213
+ arg = arg.split
214
+ end
215
+
216
+ result_parts += arg.map{|a| a.to_s.strip}
217
+ end
218
+ end
219
+
220
+ result_parts.join(' ')
221
+ end
222
+
223
+ def label(method, text=nil, *args)
224
+ args = ParsedArgs.new(args)
225
+ args.classes += 'control-label'
226
+ super(method, text, args.options)
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,3 @@
1
+ module BootstrapFormBuilder
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bootstrap_form_builder do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap_form_builder_new
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Canham
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
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.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: capybara
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Adds a Twitter Bootstrap FormBuilder.
63
+ email:
64
+ - alphasimian@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/bootstrap_form_builder/form_builder.rb
70
+ - lib/bootstrap_form_builder/version.rb
71
+ - lib/bootstrap_form_builder.rb
72
+ - lib/tasks/bootstrap_form_builder_tasks.rake
73
+ - MIT-LICENSE
74
+ - Rakefile
75
+ - README.rdoc
76
+ homepage: https://github.com/tomcanham
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: A simple Twitter Bootstrap Form Builder helper.
100
+ test_files: []