formtastic 1.1.0 → 1.2.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +1 -1
- data/README.textile +80 -66
- data/generators/form/form_generator.rb +37 -46
- data/generators/formtastic/formtastic_generator.rb +10 -8
- data/lib/formtastic.rb +318 -227
- data/lib/formtastic/i18n.rb +8 -6
- data/lib/formtastic/layout_helper.rb +6 -4
- data/lib/formtastic/railtie.rb +3 -1
- data/lib/formtastic/util.rb +2 -0
- data/lib/generators/formtastic/form/form_generator.rb +5 -7
- data/lib/generators/formtastic/install/install_generator.rb +2 -9
- data/lib/generators/templates/_form.html.erb +5 -0
- data/lib/generators/templates/_form.html.haml +4 -0
- data/{generators/formtastic → lib/generators}/templates/formtastic.css +25 -11
- data/{generators/formtastic → lib/generators}/templates/formtastic.rb +19 -2
- data/{generators/formtastic → lib/generators}/templates/formtastic_changes.css +0 -0
- metadata +58 -98
- data/Rakefile +0 -127
- data/generators/form/templates/view__form.html.erb +0 -5
- data/generators/form/templates/view__form.html.haml +0 -4
- data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +0 -16
- data/init.rb +0 -5
- data/rails/init.rb +0 -2
- data/spec/buttons_spec.rb +0 -166
- data/spec/commit_button_spec.rb +0 -401
- data/spec/custom_builder_spec.rb +0 -98
- data/spec/defaults_spec.rb +0 -20
- data/spec/error_proc_spec.rb +0 -27
- data/spec/errors_spec.rb +0 -105
- data/spec/form_helper_spec.rb +0 -142
- data/spec/helpers/layout_helper_spec.rb +0 -21
- data/spec/i18n_spec.rb +0 -152
- data/spec/include_blank_spec.rb +0 -74
- data/spec/input_spec.rb +0 -786
- data/spec/inputs/boolean_input_spec.rb +0 -104
- data/spec/inputs/check_boxes_input_spec.rb +0 -426
- data/spec/inputs/country_input_spec.rb +0 -118
- data/spec/inputs/date_input_spec.rb +0 -168
- data/spec/inputs/datetime_input_spec.rb +0 -310
- data/spec/inputs/file_input_spec.rb +0 -34
- data/spec/inputs/hidden_input_spec.rb +0 -78
- data/spec/inputs/numeric_input_spec.rb +0 -44
- data/spec/inputs/password_input_spec.rb +0 -46
- data/spec/inputs/radio_input_spec.rb +0 -243
- data/spec/inputs/select_input_spec.rb +0 -546
- data/spec/inputs/string_input_spec.rb +0 -64
- data/spec/inputs/text_input_spec.rb +0 -34
- data/spec/inputs/time_input_spec.rb +0 -206
- data/spec/inputs/time_zone_input_spec.rb +0 -110
- data/spec/inputs_spec.rb +0 -476
- data/spec/label_spec.rb +0 -89
- data/spec/semantic_errors_spec.rb +0 -98
- data/spec/semantic_fields_for_spec.rb +0 -45
- data/spec/spec.opts +0 -2
- data/spec/spec_helper.rb +0 -289
- data/spec/support/custom_macros.rb +0 -494
- data/spec/support/output_buffer.rb +0 -4
- data/spec/support/test_environment.rb +0 -45
data/MIT-LICENSE
CHANGED
data/README.textile
CHANGED
@@ -2,6 +2,8 @@ h1. Formtastic
|
|
2
2
|
|
3
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
4
|
|
5
|
+
<a href='http://www.pledgie.com/campaigns/2178'><img alt='Click here to lend your support to: formtastic and make a donation at www.pledgie.com !' src='http://www.pledgie.com/campaigns/2178.png?skin_name=chrome' border='0' /></a>
|
6
|
+
|
5
7
|
h2. The Story
|
6
8
|
|
7
9
|
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:
|
@@ -44,7 +46,7 @@ h2. It's better than _SomeOtherFormBuilder_ because...
|
|
44
46
|
|
45
47
|
* 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
48
|
* 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
|
49
|
+
* it's Rails 2.x and 3.x compatible (including nested forms).
|
48
50
|
* it has internationalization (I18n)!
|
49
51
|
* 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
52
|
* there's heaps of elements, id and class attributes for you to hook in your CSS and JS.
|
@@ -77,50 +79,30 @@ h2. Documentation
|
|
77
79
|
RDoc documentation _should_ be automatically generated after each commit and made available on the "rdoc.info website":http://rdoc.info/projects/justinfrench/formtastic.
|
78
80
|
|
79
81
|
|
80
|
-
h2.
|
81
|
-
|
82
|
-
Formtastic 1.0 only supports the latest stable Rails 2.x. We're maintaining a rails3 branch which aims to provide both Rails 2.x and 3.x support, which is targeted for the 1.1 release around the same time Rails 3.0 final ships.
|
82
|
+
h2. Installation
|
83
83
|
|
84
|
-
|
84
|
+
Formtastic is now compatible with both Rails 2 and Rails 3, and the gem is "hosted":http://rubygems.org/gems/formtastic on RubyGems.org.
|
85
85
|
|
86
|
-
|
87
|
-
gem install formtastic --pre
|
88
|
-
</pre>
|
86
|
+
*You'll need to use Bundler (yes, even under Rails 2, due to the many ways gem dependencies suck). Follow "this tutorial":http://gembundler.com/rails23.html.*
|
89
87
|
|
90
|
-
|
88
|
+
Simply add Formtastic to your Gemfile and bundle it up:
|
91
89
|
|
92
90
|
<pre>
|
93
|
-
gem 'formtastic', '1.1.0
|
91
|
+
gem 'formtastic', '~> 1.1.0'
|
94
92
|
</pre>
|
95
93
|
|
96
|
-
|
94
|
+
Optionally, run the generator to copy some stylesheets and a configuration initializer into your application:
|
97
95
|
|
98
96
|
<pre>
|
99
|
-
|
97
|
+
# Rails 3:
|
98
|
+
$ rails generate formtastic:install
|
99
|
+
|
100
|
+
# Or Rails 2:
|
101
|
+
$ ./script/generate formtastic
|
100
102
|
</pre>
|
101
103
|
|
102
|
-
Please be aware that Rails 3 is still under heavy development (even in the RCs), as is Formtastic, so our beta gems and the rails3 branch is very much "on the edge", just like Rails. If you find compatibility issues with Rails 2 or 3, please report them on Github.
|
103
|
-
|
104
|
-
|
105
|
-
h2. Installation under Rails 2.x
|
106
104
|
|
107
|
-
|
108
|
-
|
109
|
-
<pre>
|
110
|
-
sudo gem install formtastic
|
111
|
-
</pre>
|
112
|
-
|
113
|
-
And add it to your environment.rb configuration as a gem dependency:
|
114
|
-
|
115
|
-
<pre>
|
116
|
-
config.gem 'formtastic'
|
117
|
-
</pre>
|
118
|
-
|
119
|
-
Optionally, run @./script/generate formtastic@ to copy the following files into your app:
|
120
|
-
|
121
|
-
* @config/initializers/formtastic.rb@ - a commented out Formtastic config initializer
|
122
|
-
* @public/stylesheets/formtastic.css@
|
123
|
-
* @public/stylesheets/formtastic_changes.css@
|
105
|
+
h2. Stylesheets
|
124
106
|
|
125
107
|
A proof-of-concept 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. If you want to use these stylesheets, add both to your layout with this helper:
|
126
108
|
|
@@ -183,7 +165,7 @@ If you want to customize the label text, or render some hint text below the fiel
|
|
183
165
|
<% form.inputs :name => "Advanced Options", :id => "advanced" do %>
|
184
166
|
<%= form.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
|
185
167
|
<%= form.input :section, :as => :radio %>
|
186
|
-
<%= form.input :user, :label => "Author", :label_method => :full_name
|
168
|
+
<%= form.input :user, :label => "Author", :label_method => :full_name %>
|
187
169
|
<%= form.input :categories, :required => false %>
|
188
170
|
<%= form.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
|
189
171
|
<% end %>
|
@@ -193,7 +175,7 @@ If you want to customize the label text, or render some hint text below the fiel
|
|
193
175
|
<% end %>
|
194
176
|
</pre>
|
195
177
|
|
196
|
-
Nested forms
|
178
|
+
Nested forms are also supported (don't forget your models need to be setup correctly with @accepts_nested_attributes_for@). You can do it in the Rails way:
|
197
179
|
|
198
180
|
<pre>
|
199
181
|
<% semantic_form_for @post do |form| %>
|
@@ -225,6 +207,17 @@ When working in has many association, you can even supply @"%i"@ in your fieldse
|
|
225
207
|
<% end %>
|
226
208
|
</pre>
|
227
209
|
|
210
|
+
If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide
|
211
|
+
a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate html id. For example:
|
212
|
+
|
213
|
+
<pre>
|
214
|
+
<% semantic_form_for(@post, :namespace => 'cat_form') do |form| %>
|
215
|
+
<%= form.input :title %> # id="cat_form_post_title"
|
216
|
+
<%= form.input :body %> # id="cat_form_post_body"
|
217
|
+
<%= form.input :created_at %> # id="cat_form_post_created_at"
|
218
|
+
<%= form.buttons %>
|
219
|
+
<% end %>
|
220
|
+
</pre>
|
228
221
|
|
229
222
|
Customize HTML attributes for any input using the @:input_html@ option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like "autogrow":http://plugins.jquery.com/project/autogrow textareas:
|
230
223
|
|
@@ -248,7 +241,7 @@ The same can be done for buttons with the @:button_html@ option:
|
|
248
241
|
<% end %>
|
249
242
|
</pre>
|
250
243
|
|
251
|
-
Customize the HTML attributes for the @<li>@ wrapper around every input with the @:wrapper_html@ option hash. There's one special key in the hash (@:class@), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like @"required string error"@)
|
244
|
+
Customize the HTML attributes for the @<li>@ wrapper around every input with the @:wrapper_html@ option hash. There's one special key in the hash (@:class@), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like @"required string error"@).
|
252
245
|
|
253
246
|
<pre>
|
254
247
|
<% semantic_form_for @post do |form| %>
|
@@ -259,6 +252,14 @@ Customize the HTML attributes for the @<li>@ wrapper around every input with the
|
|
259
252
|
<% end %>
|
260
253
|
</pre>
|
261
254
|
|
255
|
+
Customize the default class used for hints on each attribute or globally in the @config/formtastic.rb@ file. Similarly you can customize the error classes on an attribute level or globally.
|
256
|
+
|
257
|
+
<pre>
|
258
|
+
<% semantic_form_for @post do |form| %>
|
259
|
+
<%= form.input :title, :hint_class => 'custom-html-class', :error_class => 'custom-error-class' %>
|
260
|
+
<% end %>
|
261
|
+
</pre>
|
262
|
+
|
262
263
|
Many inputs provide a collection of options to choose from (like @:select@, @:radio@, @:check_boxes@, @:boolean@). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the @:collection@ option is what you want. You can pass in an Array of objects, an array of Strings, a Hash... Throw almost anything at it! Examples:
|
263
264
|
|
264
265
|
<pre>
|
@@ -295,6 +296,10 @@ The Formtastic input types:
|
|
295
296
|
* @:numeric@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@.
|
296
297
|
* @:file@ - a file field. Default for file-attachment attributes matching: "paperclip":http://github.com/thoughtbot/paperclip or "attachment_fu":http://github.com/technoweenie/attachment_fu.
|
297
298
|
* @:country@ - a select menu of country names. Default for column types: :string with name @"country"@ - requires a *country_select* plugin to be installed.
|
299
|
+
* @:email@ - a text field (just like string). Default for columns with name matching @"email"@. New in HTML5. Works on some mobile browsers already.
|
300
|
+
* @:url@ - a text field (just like string). Default for columns with name matching @"url"@. New in HTML5. Works on some mobile browsers already.
|
301
|
+
* @:phone@ - a text field (just like string). Default for columns with name matching @"phone"@ or @"fax"@. New in HTML5.
|
302
|
+
* @:search@ - a text field (just like string). Default for columns with name matching @"search"@. New in HTML5. Works on Safari.
|
298
303
|
* @:hidden@ - a hidden field. Creates a hidden field (added for compatibility).
|
299
304
|
|
300
305
|
The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out.
|
@@ -481,51 +486,67 @@ If you have the "ValidationReflection":http://github.com/redinger/validation_ref
|
|
481
486
|
|
482
487
|
h2. Configuration
|
483
488
|
|
484
|
-
Run @./script/generate formtastic@ to copy a commented out config file into @config/initializers/formtastic.rb@. You can "view the configuration file on GitHub":http://github.com/justinfrench/formtastic/blob/master/generators/formtastic/templates/formtastic.rb
|
489
|
+
Run @rails generate formtastic:install@ (Rails 3) or @./script/generate formtastic@ (Rails 2) to copy a commented out config file into @config/initializers/formtastic.rb@. You can "view the configuration file on GitHub":http://github.com/justinfrench/formtastic/blob/master/generators/formtastic/templates/formtastic.rb
|
485
490
|
|
486
491
|
|
487
492
|
h2. Form Generator
|
488
493
|
|
489
|
-
There's a Formtastic form code generator to make your transition to Formtastic easier. All you have to do is to *specify an existing model name*, and optionally specify view template framework (ERB/HAML), and you are good to go. *Note:* This won't overwrite any of
|
494
|
+
There's a Formtastic form code generator to make your transition to Formtastic easier. All you have to do is to *specify an existing model name*, and optionally specify view template framework (ERB/HAML), and you are good to go. *Note:* This won't overwrite any of your stuff.
|
495
|
+
|
496
|
+
h3. Basic usage
|
490
497
|
|
491
|
-
|
498
|
+
Rails 3:
|
492
499
|
|
493
500
|
<pre>
|
494
|
-
$
|
495
|
-
|
496
|
-
# GENERATED FORMTASTIC CODE
|
497
|
-
# ---------------------------------------------------------
|
501
|
+
$ rails generate formtastic:form ModelName
|
502
|
+
</pre>
|
498
503
|
|
499
|
-
|
500
|
-
<%= f.input :title, :label => 'Title' %>
|
501
|
-
<%= f.input :body, :label => 'Body' %>
|
502
|
-
<%= f.input :published, :label => 'Published' %>
|
503
|
-
<% end %>
|
504
|
+
Rails 2:
|
504
505
|
|
505
|
-
|
506
|
-
|
506
|
+
<pre>
|
507
|
+
$ ./script/generate form ModelName
|
507
508
|
</pre>
|
508
509
|
|
509
|
-
|
510
|
+
The results are something like this, with the ERB code printed out to the terminal
|
510
511
|
|
511
512
|
<pre>
|
512
|
-
$
|
513
|
+
$ rails generate formtastic:form Post
|
514
|
+
# ---------------------------------------------------------
|
515
|
+
# GENERATED FORMTASTIC CODE
|
516
|
+
# ---------------------------------------------------------
|
517
|
+
|
518
|
+
<% f.inputs do %>
|
519
|
+
<%= f.input :title, :label => 'Title' %>
|
520
|
+
<%= f.input :body, :label => 'Body' %>
|
521
|
+
<%= f.input :published, :label => 'Published' %>
|
522
|
+
<% end %>
|
523
|
+
|
524
|
+
# ---------------------------------------------------------
|
525
|
+
Copied to clipboard - just paste it!
|
526
|
+
</pre>
|
527
|
+
|
528
|
+
h3. Generating a partial with @--partial@
|
529
|
+
|
530
|
+
You can also ask Formtastic to *generate a partial* with the @--partial@ option, placing it in the correct location inside your @app/views@ directory:
|
531
|
+
|
532
|
+
<pre>
|
533
|
+
$ rails generate formastic:form Post --partial
|
513
534
|
exists app/views/posts
|
514
535
|
create app/views/posts/_form.html.erb
|
515
536
|
</pre>
|
516
537
|
|
517
|
-
|
538
|
+
h3. Specifying HAML instead of ERB with @--haml@
|
518
539
|
|
519
540
|
<pre>
|
520
|
-
$
|
541
|
+
$ rails generate formastic:form Post --haml
|
521
542
|
exists app/views/admin/posts
|
522
543
|
create app/views/admin/posts/_form.html.haml
|
523
544
|
</pre>
|
524
545
|
|
525
|
-
|
546
|
+
h3. Specifying controller namespace with @--controller@
|
526
547
|
|
527
548
|
<pre>
|
528
|
-
$
|
549
|
+
$ rails generate formastic:form Post --partial --controller admin/posts
|
529
550
|
exists app/views/admin/posts
|
530
551
|
create app/views/admin/posts/_form.html.erb
|
531
552
|
</pre>
|
@@ -537,6 +558,7 @@ If you want to add your own input types to encapsulate your own logic or interfa
|
|
537
558
|
|
538
559
|
@Formtastic::SemanticFormHelper.builder = MyCustomBuilder@
|
539
560
|
|
561
|
+
|
540
562
|
h2. Security
|
541
563
|
|
542
564
|
By default formtastic escapes html entities in both labels and hints unless a string is marked as html_safe. If you are using an older rails version which doesn't know html_safe, or you want to globally turn this feature off, you can set the following in your initializer:
|
@@ -544,13 +566,6 @@ By default formtastic escapes html entities in both labels and hints unless a st
|
|
544
566
|
Formtastic::SemanticFormBuilder.escape_html_entities_in_hints_and_labels = false
|
545
567
|
|
546
568
|
|
547
|
-
h2. Focus
|
548
|
-
|
549
|
-
Formtastic 1.0 is compatible with Rails 2.x *only*. We've also been working hard on a Rails 2 *and* 3 compatible version in the rails3 branch, targeting a 1.1 release shortly after Rails 3.0 final ships.
|
550
|
-
|
551
|
-
There's heaps more we want to do, but we have to do this first.
|
552
|
-
|
553
|
-
|
554
569
|
h2. Dependencies
|
555
570
|
|
556
571
|
There are none, but...
|
@@ -562,8 +577,7 @@ There are none, but...
|
|
562
577
|
|
563
578
|
h2. Compatibility
|
564
579
|
|
565
|
-
* We're only testing Formtastic with the latest Rails 2.x stable
|
566
|
-
* Development of a Rails 2 *and* 3 compatible version of Formtastic is underway in the rails3 branch, targeting a Formtastic 1.1 release shortly after Rails 3.0 ships.
|
580
|
+
* We're only testing Formtastic with the latest Rails 2.x and 3.x stable releases. Patches are welcome to allow backwards compatibility with older versions of Rails, of course.
|
567
581
|
* Formtastic, much like Rails 2, is very ActiveRecord-centric. Many people are using Formtastic (especially the rails3 branch) successfully with other ActiveModel-like ORMs and classes (DataMapper, MongoMapper, Mongoid, Authlogic, Devise...) but we're not guaranteeing anything at this stage. Patches are welcome, but it's not our core focus right now.
|
568
582
|
|
569
583
|
|
@@ -593,4 +607,4 @@ h2. Project Info
|
|
593
607
|
Formtastic is hosted on Github: "http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtastic, where your contributions, forkings, comments and feedback are greatly welcomed.
|
594
608
|
|
595
609
|
|
596
|
-
Copyright (c) 2007-
|
610
|
+
Copyright (c) 2007-2010 Justin French, released under the MIT license.
|
@@ -1,41 +1,27 @@
|
|
1
|
-
#
|
2
|
-
# Get current OS - needed for clipboard functionality
|
3
|
-
case RUBY_PLATFORM
|
4
|
-
when /darwin/ then
|
5
|
-
CURRENT_OS = :osx
|
6
|
-
when /win32/
|
7
|
-
CURRENT_OS = :win
|
8
|
-
begin
|
9
|
-
require 'win32/clipboard'
|
10
|
-
rescue LoadError
|
11
|
-
# Do nothing
|
12
|
-
end
|
13
|
-
else
|
14
|
-
CURRENT_OS = :x
|
15
|
-
end
|
1
|
+
# encoding: utf-8
|
16
2
|
|
17
3
|
class FormGenerator < Rails::Generator::NamedBase
|
18
|
-
|
4
|
+
|
19
5
|
default_options :haml => false,
|
20
6
|
:partial => false
|
21
|
-
|
7
|
+
|
22
8
|
VIEWS_PATH = File.join('app', 'views').freeze
|
23
9
|
IGNORED_COLUMNS = [:updated_at, :created_at].freeze
|
24
|
-
|
25
|
-
attr_reader
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
10
|
+
|
11
|
+
attr_reader :controller_file_name,
|
12
|
+
:controller_class_path,
|
13
|
+
:controller_class_nesting,
|
14
|
+
:controller_class_nesting_depth,
|
15
|
+
:controller_class_name,
|
16
|
+
:template_type
|
17
|
+
|
32
18
|
def initialize(runtime_args, runtime_options = {})
|
33
19
|
super
|
34
20
|
base_name, @controller_class_path = extract_modules(@name.pluralize)
|
35
21
|
controller_class_name_without_nesting, @controller_file_name = inflect_names(base_name)
|
36
22
|
@template_type = options[:haml] ? :haml : :erb
|
37
23
|
end
|
38
|
-
|
24
|
+
|
39
25
|
def manifest
|
40
26
|
record do |m|
|
41
27
|
if options[:partial]
|
@@ -43,13 +29,13 @@ class FormGenerator < Rails::Generator::NamedBase
|
|
43
29
|
# Ensure directory exists.
|
44
30
|
m.directory File.join(VIEWS_PATH, controller_and_view_path)
|
45
31
|
# Create a form partial for the model as "_form" in it's views path.
|
46
|
-
m.template "
|
32
|
+
m.template "_form.html.#{template_type}", File.join(VIEWS_PATH, controller_and_view_path, "_form.html.#{template_type}")
|
47
33
|
else
|
48
34
|
# Load template file, and render without saving to file
|
49
|
-
template = File.read(File.join(source_root, "
|
35
|
+
template = File.read(File.join(source_root, "_form.html.#{template_type}"))
|
50
36
|
erb = ERB.new(template, nil, '-')
|
51
37
|
generated_code = erb.result(binding).strip rescue nil
|
52
|
-
|
38
|
+
|
53
39
|
# Print the result, and copy to clipboard
|
54
40
|
puts "# ---------------------------------------------------------"
|
55
41
|
puts "# GENERATED FORMTASTIC CODE"
|
@@ -58,63 +44,68 @@ class FormGenerator < Rails::Generator::NamedBase
|
|
58
44
|
puts generated_code || " Nothing could be generated - model exists?"
|
59
45
|
puts
|
60
46
|
puts "# ---------------------------------------------------------"
|
61
|
-
puts "
|
47
|
+
puts "Copied to clipboard - just paste it!" if save_to_clipboard(generated_code)
|
62
48
|
end
|
63
49
|
end
|
64
50
|
end
|
65
|
-
|
51
|
+
|
66
52
|
protected
|
67
|
-
|
68
|
-
# Save to lipboard with multiple OS support.
|
53
|
+
|
69
54
|
def save_to_clipboard(data)
|
70
55
|
return unless data
|
56
|
+
|
71
57
|
begin
|
72
|
-
case
|
73
|
-
when
|
74
|
-
|
75
|
-
when :win
|
58
|
+
case RUBY_PLATFORM
|
59
|
+
when /win32/
|
60
|
+
require 'win32/clipboard'
|
76
61
|
::Win32::Clipboard.data = data
|
77
|
-
|
62
|
+
when /darwin/ # mac
|
63
|
+
`echo "#{data}" | pbcopy`
|
64
|
+
else # linux/unix
|
78
65
|
`echo "#{data}" | xsel --clipboard` || `echo "#{data}" | xclip`
|
79
66
|
end
|
80
|
-
rescue
|
67
|
+
rescue LoadError
|
81
68
|
false
|
82
69
|
else
|
83
70
|
true
|
84
71
|
end
|
85
72
|
end
|
86
|
-
|
73
|
+
|
87
74
|
# Add additional model attributes if specified in args - probably not that common scenario.
|
88
75
|
def attributes
|
89
76
|
# Get columns for the requested model.
|
90
77
|
existing_attributes = @class_name.constantize.content_columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }
|
91
78
|
@args = super + existing_attributes
|
92
79
|
end
|
93
|
-
|
80
|
+
|
94
81
|
def add_options!(opt)
|
95
82
|
opt.separator ''
|
96
83
|
opt.separator 'Options:'
|
97
|
-
|
84
|
+
|
98
85
|
# Allow option to generate HAML views instead of ERB.
|
99
86
|
opt.on('--haml',
|
100
87
|
"Generate HAML output instead of the default ERB.") do |v|
|
101
88
|
options[:haml] = v
|
102
89
|
end
|
103
|
-
|
90
|
+
|
104
91
|
# Allow option to generate to partial in model's views path, instead of printing out in terminal.
|
105
92
|
opt.on('--partial',
|
106
93
|
"Save generated output directly to a form partial (app/views/{resource}/_form.html.*).") do |v|
|
107
94
|
options[:partial] = v
|
108
95
|
end
|
109
|
-
|
96
|
+
|
110
97
|
opt.on('--controller CONTROLLER_PATH',
|
111
98
|
"Specify a non-standard controller for the specified model (e.g. admin/posts).") do |v|
|
112
99
|
options[:controller] = v if v.present?
|
113
100
|
end
|
114
101
|
end
|
115
|
-
|
102
|
+
|
116
103
|
def banner
|
117
104
|
"Usage: #{$0} form ExistingModelName [--haml] [--partial]"
|
118
105
|
end
|
119
106
|
|
120
|
-
|
107
|
+
def source_root
|
108
|
+
File.expand_path('../../../lib/generators/templates', __FILE__)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
@@ -1,9 +1,7 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
class FormtasticGenerator < Rails::Generator::Base
|
2
|
-
|
3
|
-
def initialize(*runtime_args)
|
4
|
-
super
|
5
|
-
end
|
6
|
-
|
4
|
+
|
7
5
|
def manifest
|
8
6
|
record do |m|
|
9
7
|
m.directory File.join('config', 'initializers')
|
@@ -14,11 +12,15 @@ class FormtasticGenerator < Rails::Generator::Base
|
|
14
12
|
m.template 'formtastic_changes.css', File.join('public', 'stylesheets', 'formtastic_changes.css')
|
15
13
|
end
|
16
14
|
end
|
17
|
-
|
15
|
+
|
18
16
|
protected
|
19
|
-
|
17
|
+
|
20
18
|
def banner
|
21
19
|
%{Usage: #{$0} #{spec.name}\nCopies formtastic.css and formtastic_changes.css to public/stylesheets/ and a config initializer to config/initializers/formtastic.rb}
|
22
20
|
end
|
23
21
|
|
24
|
-
|
22
|
+
def source_root
|
23
|
+
File.expand_path('../../../lib/generators/templates', __FILE__)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|