Search code examples
ruby-on-railsfriendly-iddecent-exposure

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


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?


Solution

  • 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.