Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.13k stars 1.39k forks source link

Allow overriding blank? method in drops #1812

Open h0jeZvgoxFepBQ2C opened 4 months ago

h0jeZvgoxFepBQ2C commented 4 months ago

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 commented 4 months ago

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?