Skip to content

Commit

Permalink
Add patch to allow using our custom setter in case of nil values
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Mar 15, 2021
1 parent 689a5c6 commit da33f4f
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions lib/open_project/patches/representable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,55 @@
#++

require 'representable'
require 'open_project/patches'

module OpenProject::Patches::Representable
def self.included(base)
base.class_eval do
def self.as_strategy=(strategy)
raise 'The :as_strategy option should respond to #call?' unless strategy.respond_to?(:call)
module DecoratorPatch
def self.included(base)
base.class_eval do
def self.as_strategy=(strategy)
raise 'The :as_strategy option should respond to #call?' unless strategy.respond_to?(:call)

@as_strategy = strategy
end
@as_strategy = strategy
end

def self.as_strategy
@as_strategy
end

def self.property(name, options = {}, &block)
options = { as: as_strategy.call(name.to_s) }.merge(options) if as_strategy

def self.as_strategy
@as_strategy
super
end
end
end
end

def self.property(name, options = {}, &block)
options = { as: as_strategy.call(name.to_s) }.merge(options) if as_strategy
module OverwriteOnNilPatch
def self.included(base)
base.class_eval do
unless const_defined?(:OverwriteOnNil)
raise "Missing OverwriteOnNil on Representable gem, check if this patch still applies"
end

super
remove_const :OverwriteOnNil

##
# Redefine OverwriteOnNil to ensure we use the correct setter
# https://github.com/trailblazer/representable/issues/234
const_set(:OverwriteOnNil, ->(input, *) { input })
end
end
end
end

unless Representable::Decorator.included_modules.include?(OpenProject::Patches::Representable)
Representable::Decorator.include OpenProject::Patches::Representable
OpenProject::Patches.patch_gem_version 'representable', '3.0.4' do
unless Representable::Decorator.included_modules.include?(OpenProject::Patches::Representable::DecoratorPatch)
Representable::Decorator.include OpenProject::Patches::Representable::DecoratorPatch
end

unless Representable::Decorator.included_modules.include?(OpenProject::Patches::Representable::OverwriteOnNilPatch)
Representable.include OpenProject::Patches::Representable::OverwriteOnNilPatch
end
end

0 comments on commit da33f4f

Please sign in to comment.