Skip to content

Commit

Permalink
Remove Virtus hack.
Browse files Browse the repository at this point in the history
Update this code when this is merged in Virtus.

 * Don't coerce nil for collection attributes when required is false
 * solnic/virtus#354
  • Loading branch information
forest committed Mar 18, 2016
1 parent f9dcac4 commit fdadae9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -3,5 +3,5 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in tracker_api.gemspec
gemspec

gem 'virtus', github: 'emjot/virtus', branch: 'fix-collection-nil-coercion'
gem 'coveralls', group: :test, require: false

18 changes: 9 additions & 9 deletions lib/tracker_api/resources/shared/base.rb
@@ -1,12 +1,12 @@
require 'virtus'
if Virtus::Attribute::NullifyBlank.method_defined?(:coerce)
require 'virtus/attribute/nullify_blank'
else
raise """
WARNING: The above monkey patch can't be applied as expected.
See discussion here: https://github.com/dashofcode/tracker_api/commit/27599e7e2169776c32bbff8c972a31b930452879
"""
end
# if Virtus::Attribute::NullifyBlank.method_defined?(:coerce)
# require 'virtus/attribute/nullify_blank'
# else
# raise """
# WARNING: The above monkey patch can't be applied as expected.
# See discussion here: https://github.com/dashofcode/tracker_api/commit/27599e7e2169776c32bbff8c972a31b930452879
# """
# end
require 'virtus/dirty_attribute'

module TrackerApi
Expand All @@ -15,7 +15,7 @@ module Shared
module Base
def self.included(base)
base.class_eval do
include Virtus.model(nullify_blank: true)
include Virtus.model#(nullify_blank: true)
include Virtus::DirtyAttribute
include Virtus::DirtyAttribute::InitiallyClean

Expand Down
19 changes: 10 additions & 9 deletions lib/tracker_api/resources/story.rb
Expand Up @@ -6,25 +6,25 @@ class Story
attribute :client

attribute :accepted_at, DateTime
attribute :comment_ids, Shared::Collection[Integer]
attribute :comments, Shared::Collection[Comment]
attribute :comment_ids
attribute :comments
attribute :created_at, DateTime
attribute :current_state, String # (accepted, delivered, finished, started, rejected, planned, unstarted, unscheduled)
attribute :deadline, DateTime
attribute :description, String
attribute :estimate, Float
attribute :external_id, String
attribute :follower_ids, Shared::Collection[Integer]
attribute :followers, Shared::Collection[Person]
attribute :follower_ids, Shared::Collection[Integer], required: false
attribute :followers
attribute :integration_id, Integer
attribute :kind, String
attribute :label_ids, Shared::Collection[Integer]
attribute :labels, Shared::Collection[Label]
attribute :label_ids
attribute :labels, Shared::Collection[Label], required: false
attribute :name, String
attribute :owned_by_id, Integer # deprecated!
attribute :owned_by, Person
attribute :owner_ids, Shared::Collection[Integer]
attribute :owners, Shared::Collection[Person]
attribute :owner_ids, Shared::Collection[Integer], required: false
attribute :owners
attribute :planned_iteration_number, Integer
attribute :project_id, Integer
attribute :requested_by, Person
Expand All @@ -49,7 +49,8 @@ class UpdateRepresenter < Representable::Decorator
property :deadline
property :requested_by_id
property :owner_ids, if: ->(options) { !options[:input].blank? }
collection :labels, class: Label, decorator: Label::UpdateRepresenter, render_empty: true
collection :labels, class: Label, decorator: Label::UpdateRepresenter,
render_empty: true, if: ->(options) { !options[:input].nil? }
property :integration_id
property :external_id
end
Expand Down
54 changes: 27 additions & 27 deletions lib/virtus/attribute/nullify_blank.rb
@@ -1,27 +1,27 @@
module Virtus
class Attribute

# Attribute extension which nullifies blank attributes when coercion failed
#
module NullifyBlank

# @see [Attribute#coerce]
#
# @api public
def coerce(input)
output = super

if !value_coerced?(output) && input.blank?
nil
# Added to nullify anything that is blank not just strings.
elsif output.blank?
nil
else
output
end
end

end # NullifyBlank

end # Attribute
end # Virtus
# module Virtus
# class Attribute
#
# # Attribute extension which nullifies blank attributes when coercion failed
# #
# module NullifyBlank
#
# # @see [Attribute#coerce]
# #
# # @api public
# def coerce(input)
# output = super
#
# if !value_coerced?(output) && input.blank?
# nil
# # Added to nullify anything that is blank not just strings.
# elsif output.blank?
# nil
# else
# output
# end
# end
#
# end # NullifyBlank
#
# end # Attribute
# end # Virtus
2 changes: 1 addition & 1 deletion tracker_api.gemspec
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
# spec.add_development_dependency 'minitest-byebug'

spec.add_dependency 'addressable'
spec.add_dependency 'virtus'
# spec.add_dependency 'virtus'
spec.add_dependency 'faraday', '~> 0.9.0'
spec.add_dependency 'faraday_middleware'
spec.add_dependency 'excon'
Expand Down

0 comments on commit fdadae9

Please sign in to comment.