1

Several of my models have liquid methods defined, using:

class MyModel < ActiveRecord::Base
  liquid_methods :created_at, :foo, :bar, :baz
end

How can I get a list of available methods for a model (in a hash or array, I suppose)? I'd like to output the list of available methods to users who use them in things like forum posts or email templates.

MrDerp
  • 1,014
  • 2
  • 12
  • 17

3 Answers3

2

This works:

MyModel.new.to_liquid.methods - Liquid::Drop.new.methods
MrDerp
  • 1,014
  • 2
  • 12
  • 17
2

Save yourself the unnecessary instantiation:

MyModel::LiquidDropClass.public_instance_methods - Liquid::Drop.public_instance_methods
# => [:foo, :bar, :baz] (or whatever your liquid methods are)

When you call liquid_methods on a model, liquid creates the LiquidDropClass class within your model's namespace, and it holds the liquid methods.

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
-2

I don't know if it will have different behaviour concerning liquid methods, but you can do:

MyModel.methods
MyMethod.new.methods

Either way, on a class or on an instance, you can call .methods to get an array of symbols that are valid methods to use on that class or instance. You can then do things like .methods.sort or .methods.sort_by etc.

MrDanA
  • 11,489
  • 2
  • 36
  • 47