Changeset 1027
- Timestamp:
- 11/19/07 09:57:34 (11 months ago)
- Files:
-
- plugins/merb_helpers/Rakefile (modified) (2 diffs)
- plugins/merb_helpers/lib/form_helpers.rb (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/merb_helpers/Rakefile
r1024 r1027 1 1 require 'rubygems' 2 2 require 'rake/gempackagetask' 3 require 'spec' 4 require 'spec/rake/spectask' 3 5 4 6 PLUGIN = "merb_helpers" … … 35 37 sh %{sudo gem install pkg/#{NAME}-#{GEM_VERSION}} 36 38 end 39 40 Spec::Rake::SpecTask.new do |t| 41 t.warning = true 42 t.spec_opts = ["--format", "specdoc", "--colour"] 43 t.spec_files = Dir['specs/**/*_spec.rb'].sort 44 end plugins/merb_helpers/lib/form_helpers.rb
r1009 r1027 21 21 # 22 22 # <form action="/people/create" method="post"> 23 # <label for="person_first_name">First Name</label> 23 24 # <input id="person_first_name" name="person[first_name]" size="30" type="text" /> 25 # <label for="person_last_name">Last Name</label> 24 26 # <input id="person_last_name" name="person[last_name]" size="30" type="text" /> 25 # < input name="commit" type="submit" value="Create" />27 # <button type="submit">Create</button> 26 28 # </form> 27 29 # … … 36 38 # <form action="/foo/bar/1" method="post"> 37 39 # <label for="first_name">First Name</label><input id="first_name" name="first_name" size="30" type="text" /> 38 # < input name="commit" type="submit" value="Create" />40 # <button type="submit">Create</button> 39 41 # </form> 40 42 module Form … … 45 47 # 46 48 # ==== Example 47 #48 49 # <%= error_messages_for :person %> 49 50 def error_messages_for(obj, error_li = nil, html_class='submittal_failed') … … 120 121 # 121 122 # ==== Examples 122 #123 123 # <% form_for :person, :action => url(:people) do %> 124 124 # <%= text_control :first_name, :label => 'First Name' %> … … 130 130 # <% end %> 131 131 def fields_for(obj, attrs=nil, &block) 132 @_obj ||= nil 133 @_block ||= nil 132 134 obj = obj_from_ivar_or_sym(obj) 133 135 old_obj, @_obj = @_obj, obj … … 167 169 end 168 170 169 # Provides a generic HTML text input tag 171 # Provides a generic HTML text input tag. 170 172 # Provides a HTML text input tag based on a resource attribute. 171 173 # 172 174 # ==== Example 173 # <%= text_field :fav_color, :label => 'Your Favorite Color' %>174 # => <label for="fav_color">Your Favorite Color</label><input type="text" name="fav_color" id="fav_color"/>175 # <%= text_field :fav_color, :label => 'Your Favorite Color' %> 176 # # => <label for="fav_color">Your Favorite Color</label><input type="text" name="fav_color" id="fav_color"/> 175 177 def text_field(attrs = {}) 176 178 attrs.merge!(:type => "text") … … 179 181 180 182 # Provides a HTML password input based on a resource attribute. 181 # This is generally used within a resource block such as +form_for+ 182 # ==== Example 183 # <%= password_control :password, :label => 'New Password' %> 183 # This is generally used within a resource block such as +form_for+. 184 # 185 # ==== Example 186 # <%= password_control :password, :label => 'New Password' %> 184 187 def password_control(col, attrs = {}) 185 188 attrs.merge!(:name => control_name(col)) … … 188 191 end 189 192 193 # Provides a generic HTML password input tag. 194 # 195 # ==== Example 196 # <%= password_field :password, :label => "Password" %> 197 # # => <label for="password">Password</label><input type="password" name="password" id="password"/> 190 198 def password_field(attrs = {}) 191 199 attrs.delete(:value) … … 201 209 private :col_val_to_bool 202 210 211 # Provides a HTML checkbox input based on a resource attribute. 212 # This is generally used within a resource block such as +form_for+. 213 # 214 # ==== Example 215 # <%= checkbox_control :is_activated, :label => "Activated?" %> 203 216 def checkbox_control(col, attrs = {}) 204 217 errorify_field(attrs, col) … … 207 220 end 208 221 222 # Provides a generic HTML checkbox input tag. 223 # 224 # ==== Example 225 # <% checkbox_field :name => "is_activated", :value => "1" %> 209 226 def checkbox_field(attrs = {}) 210 227 on = attrs.delete(:on) || 1 … … 221 238 # shown. 222 239 # 223 # ==== Examples 224 # 225 # <%= hidden_control :identifier %> 226 # # => <input id="person_identifier" name="person[identifier]" type="hidden" value="#{@person.identifier}" /> 227 # 240 # ==== Example 241 # <%= hidden_control :identifier %> 242 # # => <input id="person_identifier" name="person[identifier]" type="hidden" value="#{@person.identifier}" /> 228 243 def hidden_control(col, attrs = {}) 229 244 attrs.delete(:label) … … 232 247 end 233 248 249 # Provides a generic HTML hidden input field. 250 # 251 # ==== Example 252 # <%= hidden_field :name => "secret", :value => "some secret value" %> 234 253 def hidden_field(attrs = {}) 235 254 attrs.delete(:label) … … 238 257 end 239 258 259 # Provides a radio group based on a resource attribute. 260 # This is generally used within a resource block such as +form_for+. 261 # 262 # ==== Examples 263 # <!-- the labels are the options --> 264 # <%= radio_group_control :my_choice, [5,6,7] %> 265 # 266 # <!-- custom labels --> 267 # <%= radio_group_control :my_choice, [{:value => 5, :label => "five"}] %> 240 268 def radio_group_control(col, options = [], attrs = {}) 241 269 errorify_field(attrs, col) … … 251 279 end 252 280 281 # Provides a generic HTML radio input tag. 282 # Normally, you would use multipe +radio_field+. 283 # 284 # ==== Example 285 # <%= radio_field :name => "radio_options", :value => "1", :label => "One" %> 286 # <%= radio_field :name => "radio_options", :value => "2", :label => "Two" %> 253 287 def radio_field(attrs = {}) 254 288 attrs.merge!(:type => "radio") … … 257 291 end 258 292 293 # Provides a HTML textarea based on a resource attribute 294 # This is generally used within a resource block such as +form_for+ 295 # 296 # ==== Example 297 # <% text_area_control :comments, :label => "Comments" 259 298 def text_area_control(col, attrs = {}) 260 299 attrs ||= {} … … 263 302 end 264 303 304 # Provides a generic HTML textarea tag. 305 # 306 # ==== Example 307 # <% text_area_field "my comments", :name => "comments", :label => "Comments" %> 265 308 def text_area_field(val, attrs = {}) 266 309 val ||="" … … 272 315 end 273 316 317 # Provides a generic HTML submit button. 318 # 319 # ==== Example 320 # <% submit_button "Process" %> 274 321 def submit_button(contents, attrs = {}) 322 contents ||= "Submit" 275 323 attrs.merge!(:type => "submit") 276 324 tag("button", contents, attrs) 277 325 end 278 326 327 # Provides a generic HTML label. 328 # 329 # ==== Example 330 # <% label "Name", "", :for => "name" %> 331 # # => <label for="name">Name</label> 279 332 def label(name, contents = "", attrs = {}) 280 333 tag("label", name.to_s + contents, attrs) 281 334 end 282 335 336 # Provides a generic HTML select. 337 # 338 # ==== Options 339 # +prompt+:: Adds an additional option tag with the provided string with no value. 340 # +selected+:: The value of a selected object, which may be either a string or an array. 341 # +include_blank+:: Adds an additional blank option tag with no value. 342 # +collection+:: The collection for the select options 343 # +text_method+:: Method to determine text of an option (as a symbol). 344 # +value_method+:: Method to determine value of an option (as a symbol). 283 345 def select_field(attrs = {}) 284 346 collection = attrs.delete(:collection) … … 293 355 end 294 356 357 # Provides a HTML select based on a resource attribute. 358 # This is generally used within a resource block such as +form_for+. 359 # 360 # ==== Example 361 # <% select_control :name, :collection => %w(one two three four) %> 295 362 def select_control(col, attrs = {}) 296 363 attrs.merge!(:name => attrs[:name] || control_name(col)) … … 315 382 # 316 383 # ==== Options 317 # +selected+:: The value of a selected object, may be either a string or an array.384 # +selected+:: The value of a selected object, which may be either a string or an array. 318 385 # +prompt+:: Adds an addtional option tag with the provided string with no value. 319 386 # +include_blank+:: Adds an additional blank option tag with no value. … … 384 451 # 385 452 # ==== Example 386 # <% fieldset :legend => 'Customer Options' do -%>387 # ...your form elements388 # <% end -%>389 # 390 # => <fieldset><legend>Customer Options</legend>...your form elements</fieldset>453 # <% fieldset :legend => 'Customer Options' do -%> 454 # ...your form elements 455 # <% end -%> 456 # 457 # => <fieldset><legend>Customer Options</legend>...your form elements</fieldset> 391 458 # 392 459 # ==== Options … … 400 467 end 401 468 402 ## file input control 469 # Provides a HTML file input for a resource attribute. 470 # This is generally used within a resource block such as +form_for+. 471 # 472 # ==== Example 473 # <% file_control :file, :label => "File" %> 403 474 def file_control(col, attrs = {}) 404 475 errorify_field(attrs, col) … … 406 477 end 407 478 479 # Provides a HTML file input 480 # 481 # ==== Example 482 # <% file_field :name => "file", :label => "File" %> 408 483 def file_field(attrs = {}) 409 484 attrs.merge!(:type => "file")
