How do I send a two chained methods as a symbol?

112 views Asked by At

In order to properly search my Posts using friendly_id, I use this query :

Post.friendly.find(params[:id])

But I need to wrap this method up and send it to my presenter class gem, decent_exposure. Which gives me a fancy place to include a method that would fetch my object.

By default, this method is :find . As written in their code :

def finder
  options[:finder] || :find
end

scope.send(finder, id) # scope here would mean Post

How can I send via options, this two method query as a symbol?

2

There are 2 answers

1
j-dexx On BEST ANSWER

Looking at their documentation quickly if you define a class method on Post you should then be able to use that.

class Post
  def self.find_with_friendly(id)
    friendly.find(id)
  end
end

Then you can do

expose(:post, finder: :find_with_friendly)

Pretty sure this should work.

0
Rajeev Sharma On

Something like

scope.send(:finder).try(:id) ??