Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed determining Boolean from false #378

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/virtus/attribute/boolean.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
module Virtus
class Attribute

# An "ancestor" for both TrueClass and FalseClass
# Used to correctly build Attribute::Boolean from
# true or false
#
# Needs to be a descendant of TrueClass in order
# to allow Axiom::Type.infer to infer
# Axiom::Types::Boolean from BooleanPrimitive
#
# @private
class BooleanPrimitive < TrueClass
def self.>=(klass)
TrueClass >= klass || FalseClass >= klass
end
end

# Boolean attribute allows true or false values to be set
# Additionally it adds boolean reader method, like "admin?"
#
Expand All @@ -15,7 +30,7 @@ class Attribute
# post.published? # => false
#
class Boolean < Attribute
primitive TrueClass
primitive BooleanPrimitive

# @api private
def self.build_type(*)
Expand Down
2 changes: 1 addition & 1 deletion lib/virtus/support/type_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def determine_type_from_primitive(primitive)
type = nil
descendants.select(&:primitive).reverse_each do |descendant|
descendant_primitive = descendant.primitive
next unless primitive <= descendant_primitive
next unless descendant_primitive >= primitive
type = descendant if type.nil? or type.primitive > descendant_primitive
end
type
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/virtus/attribute/class_methods/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,18 @@
it { is_expected.to be < Axiom::Types::Collection }
end
end

context 'when building from Boolean values' do
context 'when building from true' do
let(:type) { true }

it { is_expected.to be_instance_of(Virtus::Attribute::Boolean) }
end

context 'when building from false' do
let(:type) { false }

it { is_expected.to be_instance_of(Virtus::Attribute::Boolean) }
end
end
end