Rename current_build_id sort option to current_build

This commit is contained in:
Piotr Sarnacki 2016-06-22 13:13:51 +02:00
parent cf5ea374d4
commit fcb0e7cde4
2 changed files with 18 additions and 1 deletions

View File

@ -3,7 +3,14 @@ module Travis::API::V3
params :active, :private, :starred, prefix: :repository
sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active),
:'default_branch.last_build' => 'builds.started_at',
:current_build_id => "repositories.current_build_id %{order} NULLS LAST"
:current_build => "repositories.current_build_id %{order} NULLS LAST"
# this is a hack for a bug in AR that generates invalid query when it tries
# to include `current_build` and join it at the same time. We don't actually
# need the join, but it will be automatically added, because `current_build`
# is an association. This prevents adding the join. We will probably be able
# to remove it once we move to newer AR versions
prevent_sortable_join :current_build
def for_member(user, **options)
all(user: user, **options).joins(:users).where(users: user_condition(user), invalidated_at: nil)

View File

@ -70,6 +70,15 @@ module Travis::API::V3
mapping.each { |key, value| sort_by[key.to_s] = prefix(value) }
end
def self.prevent_sortable_join(*fields)
dont_join.push(*fields.map(&:to_s))
end
@dont_join = []
def self.dont_join
@dont_join ||= superclass.dont_join.dup
end
def self.sort_condition(condition)
if condition.is_a? Hash
condition = condition.map { |e| e.map { |v| prefix(v) }.join(" = ".freeze) }.join(" and ".freeze)
@ -172,6 +181,7 @@ module Travis::API::V3
end
def sort_join?(collection, field)
return if self.class.dont_join.include?(field)
!collection.reflect_on_association(field.to_sym).nil?
end