Merge pull request #163 from travis-ci/rkh-v3-separate-models

API v3: No longer use models from travis-core
This commit is contained in:
Konstantin Haase 2015-02-23 17:51:31 +01:00
commit f100a2b927
29 changed files with 221 additions and 19 deletions

View File

@ -50,7 +50,7 @@ GIT
GIT
remote: git://github.com/travis-ci/travis-core.git
revision: a82f25fb00a39c3c64b8c09c716c206e6f4c6fad
revision: 4cfa414b1d384e518d567c70d572b34a74cbf0bf
specs:
travis-core (0.0.1)
actionmailer (~> 3.2.19)

View File

@ -21,6 +21,7 @@ module Travis
end
extend self
load_dir("#{__dir__}/v3/extensions")
load_dir("#{__dir__}/v3")
ClientError = Error .create(status: 400)

View File

@ -52,8 +52,17 @@ module Travis::API::V3
private
def dispatch(object, method = caller_locations.first.base_label)
method = object.class.name.underscore + ?_.freeze + method
method = method_for(object.class, method)
send(method, object) if respond_to?(method, true)
end
@@method_for_cache = Tool::ThreadLocal.new
def method_for(type, method)
@@method_for_cache[[type, method]] ||= begin
prefix = type.name.sub(/^Travis::API::V3::Models::/, ''.freeze).underscore
"#{prefix}_#{method}"
end
end
end
end

View File

@ -5,6 +5,7 @@ module Travis::API::V3
attr_reader :user, :permissions
def initialize(user)
user = Models::User.find(user.id) if user.is_a? ::User
@user = user
@permissions = user.permissions.where(user_id: user.id)
super()

View File

@ -0,0 +1,49 @@
module Travis::API::V3
module Extensions
# This is a patch to ActiveRecord to allow classes for polymorphic relations to be nested in a module without the
# module name being part of the type field.
#
# Example:
#
# # Without this patch
# Repository.find(2).owner.class # => User
# Travis::API::V3::Models::Repository.find(2).owner.class # => User
#
# # With this patch
# Repository.find(2).owner.class # => User
# Travis::API::V3::Models::Repository.find(2).owner.class # => Travis::API::V3::Models::User
#
# ActiveRecord does not support this out of the box. We accomplish this feature by tracking polymorphic relations
# and then adding the namespace when calling ActiveRecord::Base#[] with the foreign type key and removing it again
# in ActiveRecord::Base#[]=, so we don't break other code by accidentially writing the prefixed version to the
# database.
module BelongsTo
module ClassMethods
def polymorfic_foreign_types
@polymorfic_foreign_types ||= []
end
def belongs_to(field, options = {})
polymorfic_foreign_types << (options[:foreign_type] || "#{field}_type") if options[:polymorphic]
super
end
end
def self.included(base)
base.extend(ClassMethods)
super
end
def [](key)
value = super
value &&= "#{self.class.parent}::#{value}" if self.class.polymorfic_foreign_types.include?(key)
value
end
def []=(key, value)
value &&= value.sub("#{self.class.parent}::", ''.freeze) if self.class.polymorfic_foreign_types.include?(key)
super(key, value)
end
end
end
end

View File

@ -0,0 +1,6 @@
module Travis::API::V3
class Model < ActiveRecord::Base
include Extensions::BelongsTo
self.abstract_class = true
end
end

View File

@ -0,0 +1,5 @@
module Travis::API::V3
module Models
extend ConstantResolver
end
end

View File

@ -0,0 +1,5 @@
module Travis::API::V3
class Models::Broadcast < Model
belongs_to :recipient, polymorphic: true
end
end

View File

@ -0,0 +1,10 @@
module Travis::API::V3
class Models::Build < Model
belongs_to :repository
belongs_to :commit
belongs_to :request
belongs_to :repository, autosave: true
belongs_to :owner, polymorphic: true
has_many :jobs, as: :source, order: :id, dependent: :destroy
end
end

View File

@ -0,0 +1,6 @@
module Travis::API::V3
class Models::Commit < Model
belongs_to :repository
has_one :request
end
end

View File

@ -0,0 +1,5 @@
module Travis::API::V3
class Models::Email < Model
belongs_to :user
end
end

View File

@ -0,0 +1,10 @@
module Travis::API::V3
class Models::Job < Model
has_one :log, dependent: :destroy
belongs_to :repository
belongs_to :commit
belongs_to :build, autosave: true, foreign_key: 'source_id'
belongs_to :owner, polymorphic: true
serialize :config
end
end

View File

@ -0,0 +1,7 @@
module Travis::API::V3
class Models::Log < Model
belongs_to :job
belongs_to :removed_by, class_name: 'User', foreign_key: :removed_by
has_many :log_parts, dependent: :destroy
end
end

View File

@ -0,0 +1,5 @@
module Travis::API::V3
class Models::LogPart < Model
belongs_to :log
end
end

View File

@ -0,0 +1,6 @@
module Travis::API::V3
class Models::Membership < Model
belongs_to :user
belongs_to :organization
end
end

View File

@ -0,0 +1,7 @@
module Travis::API::V3
class Models::Organization < Model
has_many :memberships
has_many :users, through: :memberships
has_many :repositories, as: :owner
end
end

View File

@ -0,0 +1,6 @@
module Travis::API::V3
class Models::Permission < Model
belongs_to :user
belongs_to :repository
end
end

View File

@ -0,0 +1,23 @@
module Travis::API::V3
class Models::Repository < Model
has_many :commits, dependent: :delete_all
has_many :requests, dependent: :delete_all
has_many :builds, dependent: :delete_all
has_many :permissions, dependent: :delete_all
has_many :users, through: :permissions
belongs_to :owner, polymorphic: true
has_one :last_build,
class_name: 'Travis::API::V3::Models::Build'.freeze,
order: 'id DESC'.freeze
def slug
@slug ||= "#{owner_name}/#{name}"
end
def last_build_on(branch)
builds.order('id DESC'.freeze).where(branch: branch, event_type: 'push'.freeze).first
end
end
end

View File

@ -0,0 +1,10 @@
module Travis::API::V3
class Models::Request < Model
belongs_to :commit
belongs_to :repository
belongs_to :owner, polymorphic: true
has_many :builds
serialize :config
serialize :payload
end
end

View File

@ -0,0 +1,5 @@
module Travis::API::V3
class Models::SSLKey < Model
belongs_to :repository
end
end

View File

@ -0,0 +1,9 @@
module Travis::API::V3
class Models::User < Model
has_many :memberships, dependent: :destroy
has_many :organizations, through: :memberships
has_many :permissions, dependent: :destroy
has_many :repositories, through: :permissions
has_many :emails, dependent: :destroy
end
end

View File

@ -3,8 +3,8 @@ module Travis::API::V3
params :id
def find
return ::Build.find_by_id(id) if id
raise WrongParams
return Models::Build.find_by_id(id) if id
raise WrongParams, 'missing build.id'.freeze
end
end
end

View File

@ -3,8 +3,8 @@ module Travis::API::V3
params :id
def find
return ::Organization.find_by_id(id) if id
raise WrongParams
return Models::Organization.find_by_id(id) if id
raise WrongParams, 'missing organization.id'.freeze
end
end
end

View File

@ -1,7 +1,7 @@
module Travis::API::V3
class Queries::Organizations < Query
def for_member(user)
::Organization.joins(:users).where(users: user_condition(user))
Models::Organization.joins(:users).where(users: user_condition(user))
end
end
end

View File

@ -8,7 +8,7 @@ module Travis::API::V3
def all
@all ||= begin
all = ::Repository
all = Models::Repository
all = all.where(active: bool(active)) unless active.nil?
all = all.where(private: bool(private)) unless private.nil?
all

View File

@ -3,8 +3,8 @@ module Travis::API::V3
params :id
def find
return ::Repository.find_by_id(id) if id
raise WrongParams
return Models::Repository.find_by_id(id) if id
raise WrongParams, 'missing repository.id'.freeze
end
end
end

View File

@ -48,7 +48,7 @@ module Travis::API::V3
case value
when String then { login: value }
when Integer then { id: value }
when ::User then { id: value.id }
when Models::User then { id: value.id }
else raise WrongParams
end
end

View File

@ -47,7 +47,7 @@ module Travis::API::V3
result
end
def render_model(model, type: model.class.name.to_sym, mode: :minimal, **options)
def render_model(model, type: model.class.name[/[^:]+$/].to_sym, mode: :minimal, **options)
Renderer[type].render(model, mode, script_name: script_name, **options)
end
@ -57,7 +57,7 @@ module Travis::API::V3
when Array then value.map { |v | render_value(v) }
when *PRIMITIVE then value
when Time then value.strftime('%Y-%m-%dT%H:%M:%SZ')
when Travis::Model then render_model(value)
when Model then render_model(value)
else raise ArgumentError, 'cannot render %p (%p)' % [value.class, value]
end
end

View File

@ -0,0 +1,17 @@
require 'spec_helper'
describe Travis::API::V3::Extensions::BelongsTo do
describe 'reading polymorphic relation' do
subject(:repo) { Travis::API::V3::Models::Repository.first }
example { expect(repo.owner).to be_a(Travis::API::V3::Models::User) }
end
describe 'writing polymorphic relation' do
let(:repo) { Travis::API::V3::Models::Repository.create(owner: user) }
let(:user) { Travis::API::V3::Models::User.create }
after { repo.destroy; user.destroy }
example { expect(repo.owner).to be_a(Travis::API::V3::Models::User) }
example { expect(::Repository.find(repo.id).owner).to be_a(::User) }
end
end