/usr/lib/ruby/vendor_ruby/merb-helpers/form/builder.rb is in ruby-merb-helpers 1.1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | #load File.dirname(__FILE__) / ".." / "tag_helpers.rb"
require 'merb-helpers/tag_helpers'
module Merb::Helpers::Form::Builder
class Base
include Merb::Helpers::Tag
def initialize(obj, name, origin)
@obj, @origin = obj, origin
@name = name || @obj.class.name.snake_case.split("/").last
end
def form(attrs = {}, &blk)
captured = @origin.capture(&blk)
fake_method_tag = process_form_attrs(attrs)
tag(:form, fake_method_tag + captured, attrs)
end
def fieldset(attrs, &blk)
legend = (l_attr = attrs.delete(:legend)) ? tag(:legend, l_attr) : ""
tag(:fieldset, legend + @origin.capture(&blk), attrs)
# @origin.concat(contents, blk.binding)
end
%w(text password hidden file).each do |kind|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def bound_#{kind}_field(method, attrs = {})
name = control_name(method)
update_bound_controls(method, attrs, "#{kind}")
unbound_#{kind}_field({
:name => name,
:value => control_value(method)
}.merge(attrs))
end
def unbound_#{kind}_field(attrs)
update_unbound_controls(attrs, "#{kind}")
self_closing_tag(:input, {:type => "#{kind}"}.merge(attrs))
end
RUBY
end
def bound_check_box(method, attrs = {})
name = control_name(method)
update_bound_controls(method, attrs, "checkbox")
unbound_check_box({:name => name}.merge(attrs))
end
def unbound_check_box(attrs)
update_unbound_controls(attrs, "checkbox")
if attrs.delete(:boolean)
on, off = attrs.delete(:on), attrs.delete(:off)
unbound_hidden_field(:name => attrs[:name], :value => off) <<
self_closing_tag(:input, {:type => "checkbox", :value => on}.merge(attrs))
else
self_closing_tag(:input, {:type => "checkbox"}.merge(attrs))
end
end
def bound_radio_button(method, attrs = {})
name = control_name(method)
update_bound_controls(method, attrs, "radio")
unbound_radio_button({:name => name, :value => control_value(method)}.merge(attrs))
end
def unbound_radio_button(attrs)
update_unbound_controls(attrs, "radio")
self_closing_tag(:input, {:type => "radio"}.merge(attrs))
end
def bound_radio_group(method, arr)
val = control_value(method)
arr.map do |attrs|
attrs = {:value => attrs} unless attrs.is_a?(Hash)
attrs[:checked] = true if (val == attrs[:value])
radio_group_item(method, attrs)
end.join
end
def unbound_radio_group(arr, attrs = {})
arr.map do |ind_attrs|
ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash)
joined = attrs.merge(ind_attrs)
joined.merge!(:label => joined[:label] || joined[:value])
unbound_radio_button(joined)
end.join
end
def bound_select(method, attrs = {})
name = control_name(method)
update_bound_controls(method, attrs, "select")
unbound_select({:name => name}.merge(attrs))
end
def unbound_select(attrs = {})
update_unbound_controls(attrs, "select")
attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/)
tag(:select, options_for(attrs), attrs)
end
def bound_text_area(method, attrs = {})
name = "#{@name}[#{method}]"
update_bound_controls(method, attrs, "text_area")
unbound_text_area(control_value(method), {:name => name}.merge(attrs))
end
def unbound_text_area(contents, attrs)
update_unbound_controls(attrs, "text_area")
tag(:textarea, contents, attrs)
end
def button(contents, attrs)
update_unbound_controls(attrs, "button")
tag(:button, contents, attrs)
end
def submit(value, attrs)
attrs[:type] ||= "submit"
attrs[:name] ||= "form_submit"
attrs[:value] ||= value
update_unbound_controls(attrs, "submit")
self_closing_tag(:input, attrs)
end
private
def process_form_attrs(attrs)
method = attrs[:method]
# Unless the method is :get, fake out the method using :post
attrs[:method] = :post unless attrs[:method] == :get
# Use a fake PUT if the object is not new, otherwise use the method
# passed in. Defaults to :post if no method is set.
method ||= (@obj.respond_to?(:new_record?) && !@obj.new_record?) ? :put : :post
attrs[:enctype] = "multipart/form-data" if attrs.delete(:multipart) || @multipart
method == :post || method == :get ? "" : fake_out_method(attrs, method)
end
# This can be overridden to use another method to fake out methods
def fake_out_method(attrs, method)
unbound_hidden_field(:name => "_method", :value => method)
end
def update_bound_controls(method, attrs, type)
case type
when "checkbox"
update_bound_check_box(method, attrs)
when "select"
update_bound_select(method, attrs)
end
end
def update_bound_check_box(method, attrs)
raise ArgumentError, ":value can't be used with a bound_check_box" if attrs.has_key?(:value)
attrs[:boolean] = attrs.fetch(:boolean, true)
val = control_value(method)
attrs[:checked] = attrs.key?(:on) ? val == attrs[:on] : considered_true?(val)
end
def update_bound_select(method, attrs)
attrs[:value_method] ||= method
attrs[:text_method] ||= attrs[:value_method] || :to_s
attrs[:selected] ||= control_value(method)
end
def update_unbound_controls(attrs, type)
case type
when "checkbox"
update_unbound_check_box(attrs)
when "radio"
update_unbound_radio_button(attrs)
when "file"
@multipart = true
end
attrs[:disabled] ? attrs[:disabled] = "disabled" : attrs.delete(:disabled)
end
def update_unbound_check_box(attrs)
boolean = attrs[:boolean] || (attrs[:on] && attrs[:off]) ? true : false
case
when attrs.key?(:on) ^ attrs.key?(:off)
raise ArgumentError, ":on and :off must be specified together"
when (attrs[:boolean] == false) && (attrs.key?(:on))
raise ArgumentError, ":boolean => false cannot be used with :on and :off"
when boolean && attrs.key?(:value)
raise ArgumentError, ":value can't be used with a boolean checkbox"
end
if attrs[:boolean] = boolean
attrs[:on] ||= "1"; attrs[:off] ||= "0"
end
attrs[:checked] = "checked" if attrs.delete(:checked)
end
def update_unbound_radio_button(attrs)
attrs[:checked] = "checked" if attrs.delete(:checked)
end
# Accepts a collection (hash, array, enumerable, your type) and returns a string of option tags.
# Given a collection where the elements respond to first and last (such as a two-element array),
# the "lasts" serve as option values and the "firsts" as option text. Hashes are turned into
# this form automatically, so the keys become "firsts" and values become lasts. If selected is
# specified, the matching "last" or element will get the selected option-tag. Selected may also
# be an array of values to be selected when using a multiple select.
#
# ==== Parameters
# attrs<Hash>:: HTML attributes and options
#
# ==== Options
# +selected+:: The value of a selected object, which may be either a string or an array.
# +prompt+:: Adds an addtional option tag with the provided string with no value.
# +include_blank+:: Adds an additional blank option tag with no value.
#
# ==== Returns
# String:: HTML
#
# ==== Examples
# <%= options_for [["apple", "Apple Pie"], ["orange", "Orange Juice"]], :selected => "orange"
# => <option value="apple">Apple Pie</option><option value="orange" selected="selected">Orange Juice</option>
#
# <%= options_for [["apple", "Apple Pie"], ["orange", "Orange Juice"]], :selected => ["orange", "apple"], :prompt => "Select One"
# => <option value="">Select One</option><option value="apple" selected="selected">Apple Pie</option><option value="orange" selected="selected">Orange Juice</option>
def options_for(attrs)
blank, prompt = attrs.delete(:include_blank), attrs.delete(:prompt)
b = blank || prompt ? tag(:option, prompt || "", :value => "") : ""
# yank out the options attrs
collection, selected, text_method, value_method =
attrs.extract!(:collection, :selected, :text_method, :value_method)
# if the collection is a Hash, optgroups are a-coming
if collection.is_a?(Hash)
([b] + collection.map do |g,col|
tag(:optgroup, options(col, text_method, value_method, selected), :label => g)
end).join
else
options(collection || [], text_method, value_method, selected, b)
end
end
def options(col, text_meth, value_meth, sel, b = nil)
([b] + col.map do |item|
text_meth = text_meth && item.respond_to?(text_meth) ? text_meth : :last
value_meth = value_meth && item.respond_to?(value_meth) ? value_meth : :first
text = item.is_a?(String) ? item : item.send(text_meth)
value = item.is_a?(String) ? item : item.send(value_meth)
unless Merb.disabled?(:merb_helper_escaping)
text = Merb::Parse.escape_xml(text)
value = Merb::Parse.escape_xml(value)
end
option_attrs = {:value => value}
if sel.is_a?(Array)
option_attrs.merge!(:selected => "selected") if value.in? sel.map {|e| e.to_s }
else
option_attrs.merge!(:selected => "selected") if value == sel.to_s
end
tag(:option, text, option_attrs)
end).join
end
def radio_group_item(method, attrs)
attrs.merge!(:checked => "checked") if attrs[:checked]
bound_radio_button(method, attrs)
end
def considered_true?(value)
value && value != "false" && value != "0" && value != 0
end
def control_name(method)
@obj ? "#{@name}[#{method}]" : method
end
def control_value(method)
value = @obj ? @obj.send(method) : @origin.params[method]
if Merb.disabled?(:merb_helper_escaping)
value.to_s
else
Merb::Parse.escape_xml(value.to_s)
end
end
def add_css_class(attrs, new_class)
attrs[:class] = attrs[:class] ? "#{attrs[:class]} #{new_class}" : new_class
end
end
class Form < Base
def label(contents, attrs = {})
if contents
if contents.is_a?(Hash)
label_attrs = contents
contents = label_attrs.delete(:title)
else
label_attrs = attrs
end
tag(:label, contents, label_attrs)
else
""
end
end
%w(text password file).each do |kind|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def unbound_#{kind}_field(attrs = {})
unbound_label(attrs) + super
end
RUBY
end
def unbound_label(attrs = {})
if attrs[:id]
label_attrs = {:for => attrs[:id]}
elsif attrs[:name]
label_attrs = {:for => attrs[:name]}
else
label_attrs = {}
end
label_option = attrs.delete(:label)
if label_option.is_a? Hash
label(label_attrs.merge(label_option))
else
label(label_option, label_attrs)
end
end
def unbound_check_box(attrs = {})
label_text = unbound_label(attrs)
super + label_text
end
def unbound_hidden_field(attrs = {})
attrs.delete(:label)
super
end
def unbound_radio_button(attrs = {})
label_text = unbound_label(attrs)
super + label_text
end
def unbound_select(attrs = {})
unbound_label(attrs) + super
end
def unbound_text_area(contents, attrs = {})
unbound_label(attrs) + super
end
def button(contents, attrs = {})
unbound_label(attrs) + super
end
def submit(value, attrs = {})
unbound_label(attrs) + super
end
private
def update_bound_controls(method, attrs, type)
attrs.merge!(:id => "#{@name}_#{method}") unless attrs[:id]
super
end
def update_unbound_controls(attrs, type)
if attrs[:name] && !attrs[:id]
attrs.merge!(:id => valid_xhtml_id(attrs[:name]))
end
case type
when "text", "radio", "password", "hidden", "checkbox", "file"
add_css_class(attrs, type)
end
super
end
def valid_xhtml_id(candidate)
candidate.to_s.gsub(/(\[|\])/, '_')
end
def radio_group_item(method, attrs)
unless attrs[:id]
attrs.merge!(:id => "#{@name}_#{method}_#{attrs[:value]}")
end
attrs.merge!(:label => attrs[:label] || attrs[:value])
super
end
end
module Errorifier
def error_messages_for(obj, error_class, build_li, header, before)
obj ||= @obj
return "" unless obj.respond_to?(:errors)
errors = obj.errors
return "" if errors.empty?
header_message = header % [errors.size, errors.size == 1 ? "" : "s"]
markup = %Q{<div class='#{error_class}'>#{header_message}<ul>}
errors.each {|err| markup << (build_li % err.join(" "))}
markup << %Q{</ul></div>}
end
private
def update_bound_controls(method, attrs, type)
if @obj && !@obj.errors[method.to_sym].blank?
add_css_class(attrs, "error")
end
super
end
end
class FormWithErrors < Form
include Errorifier
end
module Resourceful
private
def process_form_attrs(attrs)
attrs[:action] ||= @origin.url(@name, @obj) if @origin
super
end
end
class ResourcefulForm < Form
include Resourceful
end
class ResourcefulFormWithErrors < FormWithErrors
include Errorifier
include Resourceful
end
end
|