Created
August 25, 2017 15:33
-
-
Save jordpo/29e0856514b590e28d5e84920598d033 to your computer and use it in GitHub Desktop.
company bidder query
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 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 |
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
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