DEV Community

Discussion on: Design Patterns with Ruby on Rails part 2: Query Object

Collapse
 
c80609a profile image
scout

How to pass argument to the call method to use the same query class in multiple scopes?

class StatusPostsQuery
  class << self
    delegate :call, to: :new
  end

  def initialize(relation = Post)
    @relation = relation
  end

  def call(value = 'active')
    @relation.where(status: value)
  end
end
Enter fullscreen mode Exit fullscreen mode
class Post
  scope :active, StatusPostsQuery
  scope :inactive, StatusPostsQuery #...?
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
richorelse profile image
Ritchie Paul Buitre

Shameless plug: I wrote a RubyGem that can do just that. I hope that helps.