ShadowBelmolve-formtastic 0.1.6

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 (c) 2008 Justin French
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.textile ADDED
@@ -0,0 +1,363 @@
1
+ h1. Formtastic 0.1.3
2
+
3
+ Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.
4
+
5
+ h2. The Story
6
+
7
+ One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms:
8
+
9
+ <pre>
10
+ <% semantic_form_for @article do |form| %>
11
+
12
+ <% form.inputs :name => "Basic" do %>
13
+ <%= form.input :title %>
14
+ <%= form.input :body %>
15
+ <%= form.input :section %>
16
+ <%= form.input :publication_state, :as => :radio %>
17
+ <%= form.input :category %>
18
+ <%= form.input :allow_comments, :label => "Allow commenting on this article" %>
19
+ <% end %>
20
+
21
+ <% form.inputs :name => "Advanced" do %>
22
+ <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
23
+ <%= form.input :extract, :required => false %>
24
+ <%= form.input :description, :required => false %>
25
+ <%= form.input :url_title, :required => false %>
26
+ <% end %>
27
+
28
+ <% form.inputs :name => "Author", :for => :author do |author_form| %>
29
+ <%= author_form.input :first_name %>
30
+ <%= author_form.input :last_name %>
31
+ <% end %>
32
+
33
+ <% form.buttons do %>
34
+ <%= form.commit_button %>
35
+ <% end %>
36
+
37
+ <% end %>
38
+ </pre>
39
+
40
+ I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in "Learning to Love Forms":http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07, hacking together enough Ruby to prove it could be done.
41
+
42
+
43
+ h2. It's better than _SomeOtherFormBuilder_ because...
44
+
45
+ * it can handle @belongs_to@ associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model
46
+ * it can handle @has_many@ and @has_and_belongs_to_many@ associations (like Post has_many :tags), rendering a multi-select with choices from the child models
47
+ * it's Rails 2.3-ready (including nested forms)
48
+ * it has internationalization (I18n)!
49
+ * it's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it
50
+ * there's heaps of elements, id and class attributes for you to hook in your CSS and JS
51
+ * it handles real world stuff like inline hints, inline error messages & help text
52
+ * it doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match)
53
+ * it's got absolutely awesome spec coverage
54
+ * there's a bunch of people using and working on it (it's not just one developer building half a solution)
55
+
56
+
57
+ h2. Why?
58
+
59
+ * web apps = lots of forms
60
+ * forms are so friggin' boring to code
61
+ * semantically rich & accessible forms really are possible
62
+ * the "V" is way behind the "M" and "C" in Rails' MVC – it's the ugly sibling
63
+ * best practices and common patterns have to start somewhere
64
+ * i need a challenge
65
+
66
+
67
+ h2. Opinions
68
+
69
+ * it should be easier to do things the right way than the wrong way
70
+ * sometimes _more mark-up_ is better
71
+ * elements and attribute hooks are _gold_ for stylesheet authors
72
+ * make the common things we do easy, yet still ensure uncommon things are still possible
73
+
74
+
75
+ h2. Installation
76
+
77
+ You can (and should) get it as a gem:
78
+
79
+ <pre>
80
+ gem install ShadowBelmolve-formtastic
81
+ </pre>
82
+
83
+ And then add it as a dependency in your environment.rb file:
84
+
85
+ <pre>
86
+ config.gem "ShadowBelmolve-formtastic",
87
+ :lib => 'formtastic',
88
+ :source => 'http://gems.github.com'
89
+ </pre>
90
+
91
+ If you're a little more old school, install it as a plugin:
92
+
93
+ <pre>
94
+ ./script/plugin install git://github.com/ShadowBelmolve/formtastic.git
95
+ </pre>
96
+
97
+
98
+ h2. Usage
99
+
100
+ Forms are really boring to code... you want to get onto the good stuff as fast as possible.
101
+
102
+ This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord belongs_to association), followed by a submit button:
103
+
104
+ <pre>
105
+ <% semantic_form_for @user do |form| %>
106
+ <%= form.inputs %>
107
+ <%= form.buttons %>
108
+ <% end %>
109
+ </pre>
110
+
111
+ If you want to specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't detect, you can pass in a list of field names to @inputs@ and list of button names to @buttons@:
112
+
113
+ <pre>
114
+ <% semantic_form_for @user do |form| %>
115
+ <%= form.inputs :title, :body, :section, :categories, :created_at %>
116
+ <%= form.buttons :commit %>
117
+ <% end %>
118
+ </pre>
119
+
120
+ If you want control over the input type Formtastic uses for each field, you can expand the @inputs@ and @buttons@ blocks. This specifies the :section input should be a set of radio buttons (rather than the default select box), and that the :created_at field should be a string (rather than the default datetime selects):
121
+
122
+ <pre>
123
+ <% semantic_form_for @post do |form| %>
124
+ <% form.inputs do %>
125
+ <%= form.input :title %>
126
+ <%= form.input :body %>
127
+ <%= form.input :section, :as => :radio %>
128
+ <%= form.input :categories %>
129
+ <%= form.input :created_at, :as => :string %>
130
+ <% end %>
131
+ <% form.buttons do %>
132
+ <%= form.commit_button %>
133
+ <% end %>
134
+ <% end %>
135
+ </pre>
136
+
137
+ If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive:
138
+
139
+ <pre>
140
+ <% semantic_form_for @post do |form| %>
141
+ <% form.inputs :name => "Basic", :id => "basic" do %>
142
+ <%= form.input :title %>
143
+ <%= form.input :body %>
144
+ <% end %>
145
+ <% form.inputs :name => "Advanced Options", :id => "advanced" do %>
146
+ <%= form.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
147
+ <%= form.input :section, :as => :radio %>
148
+ <%= form.input :user, :label => "Author", :label_method => :full_name, %>
149
+ <%= form.input :categories, :required => false %>
150
+ <%= form.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
151
+ <% end %>
152
+ <% form.buttons do %>
153
+ <%= form.commit_button %>
154
+ <% end %>
155
+ <% end %>
156
+ </pre>
157
+
158
+ If you want to customize html elements for any non button inputs you just need
159
+ to specify the :input_html options hash.
160
+
161
+ <pre>
162
+ <% semantic_form_for @post do |form| %>
163
+ <%= form.input :title, :input_html => {:size => 60} %>
164
+ <%= form.input :body %>
165
+ <%= form.input :created_at, :input_html => {:disabled => true} %>
166
+ <%= form.buttons %>
167
+ <% end %>
168
+ </pre>
169
+
170
+ To customize buttons, :button_html is available.
171
+
172
+ Nested forms (Rails 2.3) are also supported. You can do it in the Rails way:
173
+
174
+ <pre>
175
+ <% semantic_form_for @post do |form| %>
176
+ <%= form.inputs :title, :body, :created_at %>
177
+
178
+ <% form.semantic_fields_for :author do |author| %>
179
+ <%= author.inputs :first_name, :last_name, :name => 'Author' %>
180
+ <% end %>
181
+
182
+ <%= form.buttons %>
183
+ <% end %>
184
+ </pre>
185
+
186
+ Or in the formtastic way:
187
+
188
+ <pre>
189
+ <% semantic_form_for @post do |form| %>
190
+ <%= form.inputs :title, :body, :created_at %>
191
+
192
+ <%= form.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
193
+
194
+ <%= form.buttons %>
195
+ <% end %>
196
+ </pre>
197
+
198
+ When working in has many association, you can even supply "%i" in your fieldset
199
+ name that it will be properly interpolated with the child index. For example:
200
+
201
+ <pre>
202
+ <% semantic_form_for @post do |form| %>
203
+ <%= form.inputs %>
204
+ <%= form.inputs :name => 'Category #%i', :for => :categories %>
205
+ <%= form.buttons %>
206
+ <% end %>
207
+ </pre>
208
+
209
+ Each category will be wrapped in a fieldset with legend "Category #1",
210
+ "Category #2" and so on. But please notice that this works only with Rails 2.3.
211
+
212
+ h2. The Available Inputs
213
+
214
+ * :select (a select menu) - default for ActiveRecord associations (belongs_to, has_many, has_and_belongs_to_many)
215
+ * :radio (a set of radio inputs) - alternative to :select for ActiveRecord belongs_to associations
216
+ * :password (a password input) - default for :string column types with 'password' in the method name
217
+ * :text (a textarea) - default for :text column types
218
+ * :date (a date select) - default for :date column types
219
+ * :datetime (a date and time select) - default for :datetime and :timestamp column types
220
+ * :time (a time select) - default for :time column types
221
+ * :boolean (a checkbox) - default for :boolean column types
222
+ * :boolean_select (a yes/no select box)
223
+ * :string (a text field) - default for :string column types
224
+ * :numeric (a text field, like string) - default for :integer, :float and :decimal column types
225
+ * :file (a file field) - default for paperclip or attachment_fu attributes
226
+ * :check_boxes - alternative to :select when a model has_many :through other
227
+
228
+ The documentation is pretty good for each of these (what it does, what the output is, what the options are, etc) so go check it out.
229
+
230
+
231
+ h2. Configuration
232
+
233
+ If you wish, put something like this in config/initializers/formtastic_config.rb:
234
+
235
+ <pre>
236
+ # Set the default text field size when input is a string. Default is 50
237
+ Formtastic::SemanticFormBuilder.default_text_field_size = 30
238
+
239
+ # Should all fields be considered "required" by default
240
+ # Defaults to true, see ValidationReflection notes below
241
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
242
+
243
+ # Set the string that will be appended to the labels/fieldsets which are required
244
+ # It accepts string or procs and the default is a localized version of
245
+ # '<abbr title="required">*</abbr>'. In other words, if you configure formtastic.required
246
+ # in your locale, it will replace the abbr title properly. But if you don't want to use
247
+ # abbr tag, you can simply give a string as below
248
+ Formtastic::SemanticFormBuilder.required_string = "(required)"
249
+
250
+ # Set the string that will be appended to the labels/fieldsets which are optional
251
+ # Defaults to an empty string ("") and also accepts procs (see required_string above)
252
+ Formtastic::SemanticFormBuilder.optional_string = "(optional)"
253
+
254
+ # Set the way inline errors will be displayed.
255
+ # Defaults to :sentence, valid options are :sentence, :list and :none
256
+ Formtastic::SemanticFormBuilder.inline_errors = :list
257
+
258
+ # Set the method to call on label text to transform or format it for human-friendly
259
+ # reading when formtastic is user without object. Defaults to :humanize.
260
+ Formtastic::SemanticFormBuilder.label_str_method = :titleize
261
+
262
+ # Set the array of methods to try calling on parent objects in :select and :radio inputs
263
+ # for the text inside each @<option>@ tag or alongside each radio @<input>@. The first method
264
+ # that is found on the object will be used.
265
+ # Defaults to ["to_label", "display_name", "full_name", "name", "title", "username", "login", "value", "to_s"]
266
+ Formtastic::SemanticFormBuilder.collection_label_methods = ["title_and_author", "display_name", "login", "to_s"]
267
+
268
+ # Formtastic by default renders inside li tags the input, hints and then
269
+ # errors messages. Sometimes you want the hints to be rendered first than
270
+ # the input, in the following order: hints, input and errors. You can
271
+ # customize it doing just as below:
272
+ Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
273
+ </pre>
274
+
275
+
276
+ h2. Internationalization (I18n)
277
+
278
+ Supports I18n! ActiveRecord object names and attributes are, by default, taken from calling @object.human_name and @object.human_attribute_name(attr) respectively. There are a few words specific to Formtastic that can be translated. See lib/locale/en.yml for more information.
279
+
280
+
281
+ h2. ValidationReflection plugin
282
+
283
+ If you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin installed, you won't have to specify the :required option (it checks the validations on the model instead).
284
+
285
+
286
+ h2. Status
287
+
288
+ *THINGS ARE GOING TO CHANGE A BIT BEFORE WE HIT 1.0.*
289
+
290
+ It's a work in progress and a bit rough around the edges still, but I hope you try it and offer some suggestions and improvements anyway.
291
+
292
+ On the plus side, it has a comprehensive spec suite and contributions from at least ten independent developers.
293
+
294
+ "Wishlist":http://wiki.github.com/justinfrench/formtastic/wishlist on the wiki is serving as pretty good documentation for the roadmap to 1.0 and beyond right now, but I'll work on getting a real tracking system or something happening soon.
295
+
296
+
297
+ h2. Dependencies
298
+
299
+ There are none, but...
300
+
301
+ * if you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin is installed, you won't have to specify the :required option (it checks the validations on the model instead)
302
+ * rspec, rspec_hpricot_matchers and rcov gems (plus any of their own dependencies) are required for the test suite
303
+
304
+
305
+ h2. Compatibility
306
+
307
+ I'm only testing Formtastic with the latest Rails 2.2.x stable release, and it should be fine under Rails 2.3 as well (including nested forms). Patches are welcome to allow backwards compatibility, but I don't have the energy!
308
+
309
+
310
+
311
+ h2. What about Stylesheets?
312
+
313
+ A proof-of-concept (very much a work-in-progress) stylesheet is provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet so that the Formtastic styles can be updated without clobbering your changes.
314
+
315
+ 1. Use the generator to copy the formtastic.css and formtastic_changes.css into your public directory
316
+
317
+ <pre>
318
+ ./script/generate formtastic_stylesheets
319
+ </pre>
320
+
321
+ 2. Add both formtastic.css and formtastic_changes.css to your layout:
322
+
323
+ <pre>
324
+ <%= stylesheet_link_tag "formtastic" %>
325
+ <%= stylesheet_link_tag "formtastic_changes" %>
326
+ </pre>
327
+
328
+
329
+ h2. Contributors
330
+
331
+ Formtastic wouldn't be as awesome as it is today if it weren't for the wonderful contributions of these fine, fine coders. An extra huge thanks goes out to "José Valim":http://github.com/josevalim for nearly 50 patches.
332
+
333
+ * "Justin French":http://justinfrench.com
334
+ * "José Valim":http://github.com/josevalim
335
+ * "Jeff Smick":http://github.com/sprsquish
336
+ * "Tien Dung":http://github.com/tiendung
337
+ * "Mark Mansour":http://stateofflux.com
338
+ * "Andy Pearson":http://github.com/andypearson
339
+ * "negonicrac":http://github.com/negonicrac
340
+ * "Xavier Shay":http://rhnh.net
341
+ * "Pat Allan":http://github.com/freelancing-god
342
+ * "Gareth Townsend":http://github.com/quamen
343
+ * "Sascha Hoellger":http://github.com/mitnal
344
+ * "Andrew Carpenter":http://github.com/andrewcarpenter
345
+ * "Jack Dempsey":http://github.com/jackdempsey/
346
+ * "Greg Fitzgerald":http://github.com/gregf/
347
+ * "Hector E. Gomez Morales":http://github.com/hectoregm
348
+ * "Ben Hamill":http://blog.benhamill.com/
349
+ * "Simon Chiu":http://github.com/tolatomeow
350
+ * "Bin Dong":http://github.com/dongbin
351
+
352
+
353
+ h2. Hey, join the Google group!
354
+
355
+ Please join the "Formtastic Google Group":http://groups.google.com.au/group/formtastic, especially if you'd like to talk about a new feature, or report a bug.
356
+
357
+
358
+ h2. Project Info
359
+
360
+ Formtastic is hosted on Github: http://github.com/justinfrench/formtastic/, where your contributions, forkings, comments and feedback are greatly welcomed.
361
+
362
+
363
+ Copyright (c) 2007-2008 Justin French, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'spec/rake/spectask'
4
+
5
+ begin
6
+ GEM = "formtastic"
7
+ AUTHOR = "Justin French"
8
+ EMAIL = "justin@indent.com.au"
9
+ SUMMARY = "A Rails form builder plugin/gem with semantically rich and accessible markup"
10
+ HOMEPAGE = "http://github.com/justinfrench/formtastic/tree/master"
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |s|
14
+ s.name = GEM
15
+ s.summary = SUMMARY
16
+ s.email = EMAIL
17
+ s.homepage = HOMEPAGE
18
+ s.description = SUMMARY
19
+ s.author = AUTHOR
20
+
21
+ s.require_path = 'lib'
22
+ s.autorequire = GEM
23
+ s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+ desc 'Default: run unit specs.'
30
+ task :default => :spec
31
+
32
+ desc 'Test the formtastic plugin.'
33
+ Spec::Rake::SpecTask.new('spec') do |t|
34
+ t.spec_files = FileList['spec/**/*_spec.rb']
35
+ t.spec_opts = ["-c"]
36
+ end
37
+
38
+ desc 'Test the formtastic plugin with specdoc formatting and colors'
39
+ Spec::Rake::SpecTask.new('specdoc') do |t|
40
+ t.spec_files = FileList['spec/**/*_spec.rb']
41
+ t.spec_opts = ["--format specdoc", "-c"]
42
+ end
43
+
44
+ desc 'Generate documentation for the formtastic plugin.'
45
+ Rake::RDocTask.new(:rdoc) do |rdoc|
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = 'Formtastic'
48
+ rdoc.options << '--line-numbers' << '--inline-source'
49
+ rdoc.rdoc_files.include('README.textile')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
52
+
53
+ desc "Run all examples with RCov"
54
+ Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
55
+ t.spec_files = FileList['spec/**/*_spec.rb']
56
+ t.rcov = true
57
+ t.rcov_opts = ['--exclude', 'spec,Library']
58
+ end