Skip to content

Instantly share code, notes, and snippets.

@jordpo
Created August 25, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordpo/29e0856514b590e28d5e84920598d033 to your computer and use it in GitHub Desktop.
Save jordpo/29e0856514b590e28d5e84920598d033 to your computer and use it in GitHub Desktop.
company bidder query
class Companies::CompanyUsersQuery
def initialize()
@scope = CompanyUser
end
def add_includes
scope = scope.include([:credentials, :company])
self
end
def merge_with_companies_scope(companies_scope)
scope = scope.merge(companies_scope)
self
end
def with_last_active_time
scope = scope.merge(Credential.with_last)
self
end
def sort_by_last_active_time
scope = scope.order(last_active_time: :asc)
self
end
private
attr_accessor :scope
end
def last_active_users
group_company_users = Companies::CompanyUsersQuery.new(CompanyUser)
.add_includes
.filter_by_companies(companies_scope)
.with_last_active_time
.sort_by_last_active_time
.group_by(:company)
group_company_users.map(&:last)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment