Created
April 26, 2016 14:34
-
-
Save jameslafa/337e6290115479c2e7e707a8978b7f5a to your computer and use it in GitHub Desktop.
Rails: convert anything to boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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