Sha256: 0a50bbafaffc3ffbef0892fa9e1276b8370eac1f8bdaa51dd67671326a9d127f

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module MethodHelper
  module Functions
    def boolean_column(factory_name, column_name, default_val = false)
      describe column_name.to_s do
        let(:record) { build(factory_name) }

        it "cannot be nil in the database" do
          stubbed = record.dup
          allow(stubbed).to receive(:set_booleans)

          stubbed.__send__"#{column_name}=", nil
          expect(stubbed.valid?).to be false
          expect(stubbed.errors[column_name]).to include("must be true or false")
          expect { stubbed.save(validate: false) }.to raise_error(ActiveRecord::NotNullViolation)
        end

        it "defaults to '#{default_val}'" do
          empty_record = record.class.new
          expect(empty_record.__send__ column_name).to be default_val
        end

        it "will parse to boolean on save" do
          record.__send__"#{column_name}=", nil
          record.save
          expect(record.__send__ column_name).to be false

          record.__send__"#{column_name}=", 0
          record.save
          expect(record.__send__ column_name).to be false

          record.__send__"#{column_name}=", 'false'
          record.save
          expect(record.__send__ column_name).to be false

          record.__send__"#{column_name}=", 1
          record.save
          expect(record.__send__ column_name).to be true

          record.__send__"#{column_name}=", 'asdf'
          record.save
          expect(record.__send__ column_name).to be true

          record.__send__"#{column_name}=", 'true'
          record.save
          expect(record.__send__ column_name).to be true

          record.destroy
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
better_record-0.2.0 spec/method_helper/functions/boolean_column.rb