Skip to content

Instantly share code, notes, and snippets.

@jameslafa
Created April 26, 2016 14:34
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 jameslafa/337e6290115479c2e7e707a8978b7f5a to your computer and use it in GitHub Desktop.
Save jameslafa/337e6290115479c2e7e707a8978b7f5a to your computer and use it in GitHub Desktop.
Rails: convert anything to boolean
# config/initializers/to_boolean.rb
module ToBoolean
def to_bool
return true if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(self)
return false
end
end
class NilClass; include ToBoolean; end
class TrueClass; include ToBoolean; end
class FalseClass; include ToBoolean; end
class Numeric; include ToBoolean; end
class String; include ToBoolean; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment