Sha256: cfab383892526edf3d5384ae2456dc6b6da1fe730e4ae7f1ea0c0bb959982d0a

Contents?: true

Size: 605 Bytes

Versions: 1

Compression:

Stored size: 605 Bytes

Contents

module Cardboard
  class Field::Boolean < Field
    validate :is_boolean

    def value
      to_boolean(value_uid) 
    end

    def value=(val)
      self.value_uid = to_boolean(val) #saved as a consistent string 't'
    end

    def default
      [true, false].sample
    end

    private

    def is_boolean
      errors.add(:value, "is not a valid boolean") if self.value_uid.nil?
    end

    def to_boolean(val)
      return true if val == true || !!(val =~ /(true|t|yes|y|1)$/i)
      return false if val == false || val.blank? || !!(val =~ /(false|f|no|n|0)$/i)
      return nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cardboard_cms-0.1.8 app/models/cardboard/field/boolean.rb