ensl.org/app/services/api/v1/users_collection.rb

60 lines
922 B
Ruby
Raw Permalink Normal View History

class Api::V1::UsersCollection < Api::V1::Collection
def self.as_json
self.new.data.to_json
end
def data
{ users: map_query }
end
private
def users_table
User.arel_table
end
def teams_table
Team.arel_table
end
def joins
[
users_table[:team_id].eq(teams_table[:id])
]
end
def columns
[
users_table[:username],
users_table[:steamid],
teams_table[:name],
teams_table[:tag],
2015-08-17 17:55:03 +00:00
teams_table[:logo],
users_table[:id]
]
end
def arel_query
users_table
.project(columns)
.join(teams_table, Arel::Nodes::OuterJoin)
.on(joins)
.order(users_table[:id])
end
def map_query
execute_query.map do |row|
{
2015-08-17 17:55:03 +00:00
id: row[5],
username: row[0],
steamid: row[1],
team: {
name: row[2],
tag: row[3],
logo: row[4]
}
}
end
end
2014-03-31 21:33:16 +00:00
end