liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Allow overriding blank? method in drops

Open h0jeZvgoxFepBQ2C opened this issue 1 year ago • 1 comments

Hi,

we just figured out that overriding the methods blank? or present? doesn't work to be overriden in drops, f.e. we would like to do following:


{% if user.account %}
{% else %}
{% endif %}
class AccountDrop < Liquid::Drop
  def initialize(account)
    @account = account
  end

   def blank?
    @account.blank?
  end
end

But it seems that this is not working? Also not with delegate or defs_delegators via forwardable ruby standard module?

h0jeZvgoxFepBQ2C avatar Jul 05 '24 13:07 h0jeZvgoxFepBQ2C

It seems that you can at least overwrite the invokable methods:

class AccountDrop < Liquid::Drop

  delegate :blank?, :present?, to: :@account

  def initialize(account)
    @account = account
  end

  def self.invokable_methods
      super_methods = super()
      custom_methods = Set.new(%w[blank? present?])
      super_methods + custom_methods
    end
end

So at least following works now:

{% if user.account.present? %}
{% else %}
{% endif %}

but this still not?

{% if user.account %}
{% else %}
{% endif %}

Any ideas on how to make this work?

h0jeZvgoxFepBQ2C avatar Jul 05 '24 13:07 h0jeZvgoxFepBQ2C