relation inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostQuery | |
attr_accessor :relation | |
ALLOWED_SORT_ATTRS = ["title", "updated_at"] | |
# @example PostQuery.new.relation.recent.sorted("title", "DESC") | |
# @example PostQuery.new(user.posts).relation.recent.sorted("title", "DESC") | |
# | |
def initialize(relation = nil) | |
relation ||= Post.all | |
self.relation = relation.extending(Scopes) | |
end | |
module Scopes | |
include BaseQuery::Scopes | |
def recent | |
where("updated_at > ?", 1.day.ago) | |
end | |
end | |
end | |
class BaseQuery | |
module Scopes | |
def sorted(sort_param, sort_direction) | |
# This won't work | |
# How to access this const in included module | |
if ALLOWED_SORT_ATTRS.include(sort_param) | |
order("#{sort_param} #{sort_direction}") | |
else | |
order("id DESC") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work either and actually removes the const from the sub class: