Skip to content

Instantly share code, notes, and snippets.

@wycats
Created August 23, 2008 01:37
Show Gist options
  • Save wycats/6885 to your computer and use it in GitHub Desktop.
Save wycats/6885 to your computer and use it in GitHub Desktop.
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
if attrs[:checked] || (attrs[:on] && attrs[:on] == attrs[:value])
attrs[:checked] = "checked"
else
attrs.delete(:checked)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment