diff --git a/lib/travis/api/v3/queries/repositories.rb b/lib/travis/api/v3/queries/repositories.rb index 3994c384..1f28db5c 100644 --- a/lib/travis/api/v3/queries/repositories.rb +++ b/lib/travis/api/v3/queries/repositories.rb @@ -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) diff --git a/lib/travis/api/v3/query.rb b/lib/travis/api/v3/query.rb index af4e5006..4769485e 100644 --- a/lib/travis/api/v3/query.rb +++ b/lib/travis/api/v3/query.rb @@ -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