makandra / active_type

Make any Ruby object quack like ActiveRecord
MIT License
1.09k stars 74 forks source link

Boolean typecast is #46

Closed firedev closed 8 years ago

firedev commented 9 years ago

ActiveRecord converts string values of "false" and "true" to false and true for boolean fields in models.

ActiveType on the other hand assigns 0 to boolean field, in any case, which is not correct either, after all strings are considered truthy in Ruby.

I believe booleans should contain true or false instead of 0 and 1.

Type-casting behaviour is something I can live with, but I would like to hear you comments what do you think is the best course of action. I am parsing these values from XML so they are coming up as strings. As of now I am probably going to change ActiveType attribute to string and then pass to ActiveRecord as is.

triskweline commented 9 years ago

I believe ActiveType is casting "true" to true, not 1 (see specs

Can you please post a code example showing the issue?

firedev commented 9 years ago
class Test < ActiveType::Object; attribute :test, :boolean; end

t = Test.new

t.test = "false"
t.test
=> 0

t.test = "true"
t.test
=> 0
cisolarix commented 9 years ago
require 'rails'
require 'active_type'

ActiveRecord::Base.establish_connection(
  adapter:  "mysql",
  host:     "localhost",
  username: "root",
  password: "",
  database: "caida_development"
)

class Foo < ActiveType::Object
  attribute :accept_terms, :boolean, default: proc { true }
end

f = Foo.new accept_terms: '1'  # => #<Foo >
f.accept_terms                 # => 1
f.accept_terms.class           # => Fixnum
f.accept_terms == true         # => false
firedev commented 9 years ago

Thanks, but in my case this comes from an external source as "true" and "false".

triskweline commented 8 years ago

Hopefully fixed in 0.4.3.