Skip to content

Instantly share code, notes, and snippets.

@phatmann
Created January 29, 2009 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phatmann/54341 to your computer and use it in GitHub Desktop.
Save phatmann/54341 to your computer and use it in GitHub Desktop.
######## broken version #########
# update_unbound_check_box handles :checked properly, but radio does not get the same treatment
def update_unbound_controls(attrs, type)
case type
when "checkbox"
update_unbound_check_box(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
#################### Fixed version
# A new update_unbound_radio_button that handles :checked
# Changes to update_unbound_controls to call update_unbound_radio_button and
# make :disabled handling consistent with :checked handling
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] = "disabled" if attrs.delete(:disabled)
end
def update_unbound_radio_button(attrs)
attrs[:checked] = "checked" if attrs.delete(:checked)
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
##########################
# Alt fix
def update_unbound_controls(attrs, type)
case type
when "checkbox"
update_unbound_check_box(attrs)
when "file"
@multipart = true
end
attrs[:disabled] = "disabled" if attrs.delete(:disabled)
attrs[:checked] = "checked" if attrs.delete(:checked)
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment