API v3: remove n+1 query from owner page endpoint

This commit is contained in:
Konstantin Haase 2015-05-05 16:43:52 +02:00
parent cc82721565
commit 59835c80f6
5 changed files with 7 additions and 5 deletions

View File

@ -37,3 +37,4 @@ module Travis
end end
end end
end end

View File

@ -19,7 +19,7 @@ module Travis::API::V3
list = list.where(private: bool(private)) unless private.nil? list = list.where(private: bool(private)) unless private.nil?
list = list.includes(:owner) if includes? 'repository.owner'.freeze list = list.includes(:owner) if includes? 'repository.owner'.freeze
if includes? 'repository.last_build'.freeze if includes? 'repository.last_build'.freeze or includes? 'build'.freeze
list = list.includes(:last_build) list = list.includes(:last_build)
list = list.includes(last_build: :commit) if includes? 'build.commit'.freeze list = list.includes(last_build: :commit) if includes? 'build.commit'.freeze
end end

View File

@ -26,7 +26,7 @@ module Travis::API::V3
attr_reader :params, :main_type attr_reader :params, :main_type
def initialize(params, main_type, includes = nil) def initialize(params, main_type, includes: nil)
@params = params @params = params
@main_type = main_type.to_s @main_type = main_type.to_s
@includes = includes @includes = includes
@ -43,7 +43,7 @@ module Travis::API::V3
def includes?(key) def includes?(key)
@includes ||= @params['include'.freeze].to_s.split(?,.freeze) @includes ||= @params['include'.freeze].to_s.split(?,.freeze)
@includes.include? key key.include?(?.) ? @includes.include?(key) : @includes.any? { |k| k.start_with? key }
end end
def bool(value) def bool(value)

View File

@ -3,7 +3,7 @@ require 'travis/api/v3/renderer/model_renderer'
module Travis::API::V3 module Travis::API::V3
class Renderer::Commit < Renderer::ModelRenderer class Renderer::Commit < Renderer::ModelRenderer
representation(:minimal, :id, :sha, :ref, :message, :compare_url, :committed_at) representation(:minimal, :id, :sha, :ref, :message, :compare_url, :committed_at)
representation(:standard, *representations[:minimal], :repository, :branch, :committer, :author) representation(:standard, *representations[:minimal], :committer, :author)
def sha def sha
model.commit model.commit

View File

@ -12,8 +12,9 @@ module Travis::API::V3
end end
def self.representation(name, *fields) def self.representation(name, *fields)
location = caller_locations.first
fields.each do |field| fields.each do |field|
class_eval "def #{field}; @model.#{field}; end" unless method_defined?(field) class_eval "def #{field}; @model.#{field}; end", location.path, location.lineno unless method_defined?(field)
available_attributes << field.to_s available_attributes << field.to_s
end end
representations[name] ||= [] representations[name] ||= []