no longer use models from travis-core in queries
This commit is contained in:
parent
a6ff2b5422
commit
5f2dc47e07
|
@ -21,6 +21,7 @@ module Travis
|
||||||
end
|
end
|
||||||
|
|
||||||
extend self
|
extend self
|
||||||
|
load_dir("#{__dir__}/v3/extensions")
|
||||||
load_dir("#{__dir__}/v3")
|
load_dir("#{__dir__}/v3")
|
||||||
|
|
||||||
ClientError = Error .create(status: 400)
|
ClientError = Error .create(status: 400)
|
||||||
|
|
|
@ -52,8 +52,17 @@ module Travis::API::V3
|
||||||
private
|
private
|
||||||
|
|
||||||
def dispatch(object, method = caller_locations.first.base_label)
|
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)
|
send(method, object) if respond_to?(method, true)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
32
lib/travis/api/v3/extensions/belongs_to.rb
Normal file
32
lib/travis/api/v3/extensions/belongs_to.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
module Extensions
|
||||||
|
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}::") if self.class.polymorfic_foreign_types.include?(key)
|
||||||
|
super(key, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
6
lib/travis/api/v3/model.rb
Normal file
6
lib/travis/api/v3/model.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Model < ActiveRecord::Base
|
||||||
|
include Extensions::BelongsTo
|
||||||
|
self.abstract_class = true
|
||||||
|
end
|
||||||
|
end
|
5
lib/travis/api/v3/models.rb
Normal file
5
lib/travis/api/v3/models.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
module Models
|
||||||
|
extend ConstantResolver
|
||||||
|
end
|
||||||
|
end
|
5
lib/travis/api/v3/models/broadcast.rb
Normal file
5
lib/travis/api/v3/models/broadcast.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::Broadcast < Model
|
||||||
|
belongs_to :recipient, polymorphic: true
|
||||||
|
end
|
||||||
|
end
|
10
lib/travis/api/v3/models/build.rb
Normal file
10
lib/travis/api/v3/models/build.rb
Normal 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
|
6
lib/travis/api/v3/models/commit.rb
Normal file
6
lib/travis/api/v3/models/commit.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::Commit < Model
|
||||||
|
belongs_to :repository
|
||||||
|
has_one :request
|
||||||
|
end
|
||||||
|
end
|
5
lib/travis/api/v3/models/email.rb
Normal file
5
lib/travis/api/v3/models/email.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::Email < Model
|
||||||
|
belongs_to :user
|
||||||
|
end
|
||||||
|
end
|
10
lib/travis/api/v3/models/job.rb
Normal file
10
lib/travis/api/v3/models/job.rb
Normal 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
|
7
lib/travis/api/v3/models/log.rb
Normal file
7
lib/travis/api/v3/models/log.rb
Normal 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
|
5
lib/travis/api/v3/models/log_part.rb
Normal file
5
lib/travis/api/v3/models/log_part.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::LogPart < Model
|
||||||
|
belongs_to :log
|
||||||
|
end
|
||||||
|
end
|
6
lib/travis/api/v3/models/membership.rb
Normal file
6
lib/travis/api/v3/models/membership.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::Membership < Model
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :organization
|
||||||
|
end
|
||||||
|
end
|
7
lib/travis/api/v3/models/organization.rb
Normal file
7
lib/travis/api/v3/models/organization.rb
Normal 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
|
6
lib/travis/api/v3/models/permission.rb
Normal file
6
lib/travis/api/v3/models/permission.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::Permission < Model
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :repository
|
||||||
|
end
|
||||||
|
end
|
23
lib/travis/api/v3/models/repository.rb
Normal file
23
lib/travis/api/v3/models/repository.rb
Normal 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
|
10
lib/travis/api/v3/models/request.rb
Normal file
10
lib/travis/api/v3/models/request.rb
Normal 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
|
5
lib/travis/api/v3/models/ssl_key.rb
Normal file
5
lib/travis/api/v3/models/ssl_key.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::SSLKey < Model
|
||||||
|
belongs_to :repository
|
||||||
|
end
|
||||||
|
end
|
5
lib/travis/api/v3/models/user.rb
Normal file
5
lib/travis/api/v3/models/user.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Travis::API::V3
|
||||||
|
class Models::User < Model
|
||||||
|
has_many :emails
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,7 +3,7 @@ module Travis::API::V3
|
||||||
params :id
|
params :id
|
||||||
|
|
||||||
def find
|
def find
|
||||||
return ::Build.find_by_id(id) if id
|
return Models::Build.find_by_id(id) if id
|
||||||
raise WrongParams
|
raise WrongParams
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Travis::API::V3
|
||||||
params :id
|
params :id
|
||||||
|
|
||||||
def find
|
def find
|
||||||
return ::Organization.find_by_id(id) if id
|
return Models::Organization.find_by_id(id) if id
|
||||||
raise WrongParams
|
raise WrongParams
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Travis::API::V3
|
module Travis::API::V3
|
||||||
class Queries::Organizations < Query
|
class Queries::Organizations < Query
|
||||||
def for_member(user)
|
def for_member(user)
|
||||||
::Organization.joins(:users).where(users: user_condition(user))
|
Models::Organization.joins(:users).where(users: user_condition(user))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ module Travis::API::V3
|
||||||
|
|
||||||
def all
|
def all
|
||||||
@all ||= begin
|
@all ||= begin
|
||||||
all = ::Repository
|
all = Models::Repository
|
||||||
all = all.where(active: bool(active)) unless active.nil?
|
all = all.where(active: bool(active)) unless active.nil?
|
||||||
all = all.where(private: bool(private)) unless private.nil?
|
all = all.where(private: bool(private)) unless private.nil?
|
||||||
all
|
all
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Travis::API::V3
|
||||||
params :id
|
params :id
|
||||||
|
|
||||||
def find
|
def find
|
||||||
return ::Repository.find_by_id(id) if id
|
return Models::Repository.find_by_id(id) if id
|
||||||
raise WrongParams
|
raise WrongParams
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -47,17 +47,17 @@ module Travis::API::V3
|
||||||
result
|
result
|
||||||
end
|
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)
|
Renderer[type].render(model, mode, script_name: script_name, **options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_value(value)
|
def render_value(value)
|
||||||
case value
|
case value
|
||||||
when Hash then value.map { |k, v| [k, render_value(v)] }.to_h
|
when Hash then value.map { |k, v| [k, render_value(v)] }.to_h
|
||||||
when Array then value.map { |v | render_value(v) }
|
when Array then value.map { |v | render_value(v) }
|
||||||
when *PRIMITIVE then value
|
when *PRIMITIVE then value
|
||||||
when Time then value.strftime('%Y-%m-%dT%H:%M:%SZ')
|
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]
|
else raise ArgumentError, 'cannot render %p (%p)' % [value.class, value]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user