Sha256: 66c856c5ee9c31e2b275d19879133626db4962eabaf163dd8c5237d6e11de919

Contents?: true

Size: 618 Bytes

Versions: 3

Compression:

Stored size: 618 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 value_uid.present? && value.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

3 entries across 3 versions & 1 rubygems

Version Path
cardboard_cms-0.3.1 app/models/cardboard/field/boolean.rb
cardboard_cms-0.2.2 app/models/cardboard/field/boolean.rb
cardboard_cms-0.2.1 app/models/cardboard/field/boolean.rb