formgen 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +130 -4
- data/VERSION +1 -1
- data/formgen.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -33,6 +33,9 @@
|
|
33
33
|
has_many :bars
|
34
34
|
belongs_to :baz
|
35
35
|
|
36
|
+
has_many :others
|
37
|
+
belongs_to :something
|
38
|
+
|
36
39
|
accepts_nested_attributes_for :bars
|
37
40
|
accepts_nested_attributes_for :baz
|
38
41
|
|
@@ -49,14 +52,137 @@
|
|
49
52
|
app/views/foos/_bar_fields.html.erb
|
50
53
|
app/views/foos/_baz_fields.html.erb
|
51
54
|
|
55
|
+
== Static Files
|
56
|
+
|
57
|
+
* public/javascripts/jquery.add_remove_links.js
|
58
|
+
* app/helpers/add_remove_links_helper.rb
|
59
|
+
|
60
|
+
|
61
|
+
== Generated Files and Examples
|
62
|
+
|
63
|
+
Based on Foo example above.
|
64
|
+
|
65
|
+
* app/helpers/foo_setup_helper.rb
|
66
|
+
* app/views/foos/new.html.erb
|
67
|
+
* app/views/foos/edit.html.erb
|
68
|
+
* app/views/foos/_form.html.erb
|
69
|
+
* app/views/foos/_bar_fields.html.erb
|
70
|
+
* app/views/foos/_baz_fields.html.erb
|
71
|
+
|
72
|
+
=== Examples:
|
73
|
+
|
74
|
+
==== app/helpers/foo_setup_helper.rb
|
75
|
+
module FooSetupHelper
|
76
|
+
def setup_foo(obj)
|
77
|
+
obj.build_baz if obj.baz.blank?
|
78
|
+
obj.bars.build if obj.bars.empty?
|
79
|
+
return obj
|
80
|
+
end
|
81
|
+
|
82
|
+
def build_select_list(assoc, options={})
|
83
|
+
name = options[:name] || "name"
|
84
|
+
scope = options[:scope] || "all"
|
85
|
+
begin
|
86
|
+
# name and id associations
|
87
|
+
assoc.to_s.classify.constantize.send(scope).map{|a| [a.send(name), a.id]}
|
88
|
+
rescue
|
89
|
+
# use id for text and value
|
90
|
+
assoc.to_s.classify.constantize.send(scope).map(&:id)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
==== app/views/foos/new.html.erb
|
96
|
+
|
97
|
+
<h1>New foo</h1>
|
98
|
+
<%= render 'form' %>
|
99
|
+
<%= link_to 'back', :back %>
|
100
|
+
|
101
|
+
==== app/views/foos/edit.html.erb
|
102
|
+
|
103
|
+
<h1>Editing foo</h1>
|
104
|
+
<%= render 'form' %>
|
105
|
+
<%= link_to 'back', :back %>
|
106
|
+
|
107
|
+
==== app/views/foos/_form.html.erb
|
108
|
+
|
109
|
+
<%= form_for setup_foo(@foo), :html => { :multipart => true } do |f| %>
|
110
|
+
<% if @foo.errors.any? %>
|
111
|
+
<div id="errorExplanation">
|
112
|
+
<h2><%= pluralize(@foo.errors.count, "error") %> prohibited this foo from being saved:</h2>
|
113
|
+
<ul>
|
114
|
+
<% @foo.errors.full_messages.each do |msg| %>
|
115
|
+
<li><%= msg %></li>
|
116
|
+
<% end %>
|
117
|
+
</ul>
|
118
|
+
</div>
|
119
|
+
<% end %>
|
120
|
+
<div class="field">
|
121
|
+
<%= f.label :name %>
|
122
|
+
<%= f.text_field :name %>
|
123
|
+
</div>
|
124
|
+
<div class="field">
|
125
|
+
<%= f.label :description %>
|
126
|
+
<%= f.text_area :description %>
|
127
|
+
</div>
|
128
|
+
<div class="field">
|
129
|
+
<%= f.label :image %>
|
130
|
+
<%= f.file_field :image %>
|
131
|
+
</div>
|
132
|
+
<div class="field">
|
133
|
+
<%= f.label :other_ids, "Others" %>
|
134
|
+
<%= f.select :other_ids, build_select_list(:others), {:prompt => "Please select"}, {:multiple => true, :size => 4} %>
|
135
|
+
</div>
|
136
|
+
<div class="field">
|
137
|
+
<%= f.label :something_id, "Something" %>
|
138
|
+
<%= f.select :something_id, build_select_list(:something), {:prompt => "Please select one"} %>
|
139
|
+
</div>
|
140
|
+
<%= f.fields_for :baz do |nf| %>
|
141
|
+
<%= render 'baz_fields', :f => nf %>
|
142
|
+
<% end %>
|
143
|
+
<%= f.fields_for :bars do |nf| %>
|
144
|
+
<%= render 'bar_fields', :f => nf %>
|
145
|
+
<% end %>
|
146
|
+
<%= link_to_add_fields "Add bar", f, :bars %>
|
147
|
+
<%= f.submit "Save" %>
|
148
|
+
<% end %>
|
149
|
+
<%= content_for :head do %>
|
150
|
+
<%= javascript_include_tag 'jquery.add_remove_links.js' %>
|
151
|
+
<% end %>
|
152
|
+
|
153
|
+
==== app/views/foos/_bar_fields.html.erb
|
154
|
+
|
155
|
+
<div class="fields">
|
156
|
+
<div class="field">
|
157
|
+
<%= f.label :name %>
|
158
|
+
<%= f.text_field :name %>
|
159
|
+
</div>
|
160
|
+
<div class="field">
|
161
|
+
<%= f.label :bar %>
|
162
|
+
<%= f.text_field :bar %>
|
163
|
+
</div>
|
164
|
+
<%= link_to_remove_fields "remove", f %>
|
165
|
+
</div>
|
166
|
+
|
167
|
+
==== app/views/foos/_baz_fields.html.erb
|
168
|
+
|
169
|
+
<div class="fields">
|
170
|
+
<div class="field">
|
171
|
+
<%= f.label :name %>
|
172
|
+
<%= f.text_field :name %>
|
173
|
+
</div>
|
174
|
+
<div class="field">
|
175
|
+
<%= f.label :image %>
|
176
|
+
<%= f.file_field :image %>
|
177
|
+
</div>
|
178
|
+
</div>
|
179
|
+
|
52
180
|
|
53
181
|
== FAQ
|
54
182
|
|
55
|
-
* I'm getting this error:
|
56
|
-
|
57
|
-
undefined method `setup_something' for #<#<Class:
|
183
|
+
* I'm getting this error: "undefined method `setup_something' for ...":
|
58
184
|
|
59
|
-
Make sure the generated helper is being loaded - restart your rails server.
|
185
|
+
Make sure the generated helper is being loaded - restart your rails server.
|
60
186
|
|
61
187
|
|
62
188
|
== Contributing to formgen
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/formgen.gemspec
CHANGED
metadata
CHANGED