Prepare models for ES6 modules

This commit is contained in:
Piotr Sarnacki 2015-01-20 12:13:05 +01:00
parent 45615e71ee
commit 71633f1a00
10 changed files with 129 additions and 95 deletions

View File

@ -1,6 +1,6 @@
require 'travis/model'
@Travis.Account = Travis.Model.extend
Travis.Account = Travis.Model.extend
login: Ember.attr('string')
name: Ember.attr('string')
type: Ember.attr('string')
@ -8,10 +8,6 @@ require 'travis/model'
subscribed: Ember.attr(Boolean)
education: Ember.attr(Boolean)
urlGithub: (->
"#{Travis.config.source_endpoint}/#{@get('login')}"
).property()
# TODO: maybe it would be good to add a "default" value for Ember.attr
reposCount: (->
@get('_reposCount') || 0

View File

@ -1,5 +1,7 @@
require 'travis/model'
Repo = Travis.Repo
@Travis.Branch = Travis.Model.extend
repoId: Ember.attr('number', key: 'repository_id')
commitId: Ember.attr('number')
@ -15,7 +17,7 @@ require 'travis/model'
commit: Ember.belongsTo('Travis.Commit')
repo: (->
Travis.Repo.find @get('repoId') if @get('repoId')
Repo.find @get('repoId') if @get('repoId')
).property('repoId')
updateTimes: ->

View File

@ -1,7 +1,14 @@
require 'travis/model'
require 'models/extensions'
require 'utils/duration_calculations'
@Travis.Build = Travis.Model.extend Travis.DurationCalculations,
compact = Travis.Helpers.compact
configKeys = Travis.Helpers.configKeys
Ajax = Travis.ajax
config_keys_map = Travis.CONFIG_KEYS_MAP
DurationCalculations = Travis.DurationCalculations
@Travis.Build = Travis.Model.extend DurationCalculations,
repositoryId: Ember.attr('number')
commitId: Ember.attr('number')
@ -26,7 +33,7 @@ require 'models/extensions'
config: (->
console.log('config')
if config = @get('_config')
Travis.Helpers.compact(config)
compact(config)
else
return if @get('isFetchingConfig')
@set 'isFetchingConfig', true
@ -77,7 +84,7 @@ require 'models/extensions'
keys = []
@get('jobs').forEach (job) ->
Travis.Helpers.configKeys(job.get('config')).forEach (key) ->
configKeys(job.get('config')).forEach (key) ->
keys.pushObject key unless keys.contains key
keys
@ -86,7 +93,7 @@ require 'models/extensions'
configKeys: (->
keys = @get('rawConfigKeys')
headers = ['Job', 'Duration', 'Finished']
$.map(headers.concat(keys), (key) -> if Travis.CONFIG_KEYS_MAP.hasOwnProperty(key) then Travis.CONFIG_KEYS_MAP[key] else key)
$.map(headers.concat(keys), (key) -> if config_keys_map.hasOwnProperty(key) then config_keys_map[key] else key)
).property('rawConfigKeys.length')
canCancel: (->
@ -94,11 +101,11 @@ require 'models/extensions'
).property('isFinished', 'jobs.@each.canCancel')
cancel: (->
Travis.ajax.post "/builds/#{@get('id')}/cancel"
Ajax.post "/builds/#{@get('id')}/cancel"
)
requeue: ->
Travis.ajax.post "/builds/#{@get('id')}/restart"
Ajax.post "/builds/#{@get('id')}/restart"
formattedFinishedAt: (->
if finishedAt = @get('finishedAt')

View File

@ -1,14 +1 @@
Travis.DurationCalculations = Ember.Mixin.create
duration: (->
if @get('notStarted')
null
else if duration = @get('_duration')
duration
else
Travis.Helpers.durationFrom(@get('startedAt'), @get('finishedAt'))
).property('_duration', 'finishedAt', 'startedAt', 'notStarted', '_finishedAt', '_startedAt')
updateTimes: ->
unless @get('isFinished')
@notifyPropertyChange '_duration'
@notifyPropertyChange 'finished_at'

View File

@ -1,5 +1,8 @@
require 'travis/model'
loadOrMerge = Travis.loadOrMerge
Repo = Travis.Repo
@Travis.Hook = Travis.Model.extend
name: Ember.attr('string')
ownerName: Ember.attr('string')
@ -34,6 +37,6 @@ require 'travis/model'
# I add an info which we have here to the store - this will allow to display
# a link to the repo and if more info is needed, it will be requested when the
# link is used
Travis.loadOrMerge(Travis.Repo, @getProperties('id', 'slug', 'name', 'ownerName'), skipIfExists: true)
Travis.Repo.find(@get('id'))
loadOrMerge(Repo, @getProperties('id', 'slug', 'name', 'ownerName'), skipIfExists: true)
Repo.find(@get('id'))
).property('id')

View File

@ -1,7 +1,12 @@
require 'travis/model'
require 'models/extensions'
require 'models/log'
@Travis.Job = Travis.Model.extend Travis.DurationCalculations,
DurationCalculations = Travis.DurationCalculations
Log = Travis.Log
compact = Travis.Helpers.compact
@Travis.Job = Travis.Model.extend DurationCalculations,
repoId: Ember.attr('string', key: 'repository_id')
buildId: Ember.attr('string')
commitId: Ember.attr('string')
@ -25,7 +30,7 @@ require 'models/extensions'
log: ( ->
@set('isLogAccessed', true)
Travis.Log.create(job: this)
Log.create(job: this)
).property()
startedAt: (->
@ -44,7 +49,7 @@ require 'models/extensions'
config: (->
if config = @get('_config')
Travis.Helpers.compact(config)
compact(config)
else
return if @get('isFetchingConfig')
@set 'isFetchingConfig', true
@ -87,11 +92,11 @@ require 'models/extensions'
).property('state')
cancel: (->
Travis.ajax.post "/jobs/#{@get('id')}/cancel"
Ajax.post "/jobs/#{@get('id')}/cancel"
)
removeLog: ->
Travis.ajax.patch("/jobs/#{@get('id')}/log").then =>
Ajax.patch("/jobs/#{@get('id')}/log").then =>
@reloadLog()
reloadLog: ->
@ -99,7 +104,7 @@ require 'models/extensions'
@get('log').fetch()
requeue: ->
Travis.ajax.post "/jobs/#{@get('id')}/restart"
Ajax.post "/jobs/#{@get('id')}/restart"
appendLog: (part) ->
@get('log').append part

View File

@ -1,6 +1,41 @@
require 'travis/model'
require 'travis/log_chunks'
Ajax = Travis.ajax
Job = Travis.Job
Request = Em.Object.extend
HEADERS:
accept: 'application/json; chunked=true; version=2, text/plain; version=2'
run: ->
Ajax.ajax "/jobs/#{@id}/log?cors_hax=true", 'GET',
dataType: 'text'
headers: @HEADERS
success: (body, status, xhr) => Ember.run(this, -> @handle(body, status, xhr))
handle: (body, status, xhr) ->
if Travis.config.pro
Job.find(@get('id')).get('log').set('token', xhr.getResponseHeader('X-Log-Access-Token'))
if xhr.status == 204
$.ajax(url: @redirectTo(xhr), type: 'GET', success: @handlers.text)
else if @isJson(xhr, body)
@handlers.json(body)
else
@handlers.text(body)
redirectTo: (xhr) ->
# Firefox can't see the Location header on the xhr response due to the wrong
# status code 204. Should be some redirect code but that doesn't work with CORS.
xhr.getResponseHeader('Location')
isJson: (xhr, body) ->
# Firefox can't see the Content-Type header on the xhr response due to the wrong
# status code 204. Should be some redirect code but that doesn't work with CORS.
type = xhr.getResponseHeader('Content-Type') || ''
type.indexOf('json') > -1
@Travis.Log = Em.Object.extend
version: 0 # used to refresh log on requeue
isLoaded: false
@ -13,7 +48,7 @@ require 'travis/log_chunks'
data['part_numbers'] = partNumbers if partNumbers
data['after'] = after if after
Travis.ajax.ajax "/jobs/#{@get('job.id')}/log", 'GET',
Ajax.ajax "/jobs/#{@get('job.id')}/log", 'GET',
dataType: 'json'
headers:
accept: 'application/json; chunked=true; version=2'
@ -44,7 +79,7 @@ require 'travis/log_chunks'
@set('removed', true)
@loadParts(json['log']['parts'])
text: (text) => @loadText(text)
Travis.Log.Request.create(id: id, handlers: handlers).run() if id = @get('job.id')
Request.create(id: id, handlers: handlers).run() if id = @get('job.id')
clear: ->
@clearParts()
@ -63,35 +98,3 @@ require 'travis/log_chunks'
console.log 'log model: load text' if Log.DEBUG
@append(number: 1, content: text, final: true)
@set('isLoaded', true)
Travis.Log.Request = Em.Object.extend
HEADERS:
accept: 'application/json; chunked=true; version=2, text/plain; version=2'
run: ->
Travis.ajax.ajax "/jobs/#{@id}/log?cors_hax=true", 'GET',
dataType: 'text'
headers: @HEADERS
success: (body, status, xhr) => Ember.run(this, -> @handle(body, status, xhr))
handle: (body, status, xhr) ->
if Travis.config.pro
Travis.Job.find(@get('id')).get('log').set('token', xhr.getResponseHeader('X-Log-Access-Token'))
if xhr.status == 204
$.ajax(url: @redirectTo(xhr), type: 'GET', success: @handlers.text)
else if @isJson(xhr, body)
@handlers.json(body)
else
@handlers.text(body)
redirectTo: (xhr) ->
# Firefox can't see the Location header on the xhr response due to the wrong
# status code 204. Should be some redirect code but that doesn't work with CORS.
xhr.getResponseHeader('Location')
isJson: (xhr, body) ->
# Firefox can't see the Content-Type header on the xhr response due to the wrong
# status code 204. Should be some redirect code but that doesn't work with CORS.
type = xhr.getResponseHeader('Content-Type') || ''
type.indexOf('json') > -1

View File

@ -1,5 +1,14 @@
require 'travis/expandable_record_array'
require 'travis/model'
require 'helpers/helpers'
EnvVar = Travis.EnvVar
Build = Travis.Build
SshKey = Travis.SshKey
ExpandableRecordArray = Travis.ExpandableRecordArray
Event = Travis.Event
durationFrom = Travis.Helpers.durationFrom
Ajax = Travis.ajax
@Travis.Repo = Travis.Model.extend
id: Ember.attr('string')
@ -28,22 +37,22 @@ require 'travis/model'
@filter( (repo) -> repo.get('lastBuildId') )
sshKey: (->
Travis.SshKey.find(@get('id'))
SshKey.find(@get('id'))
)
envVars: (->
id = @get('id')
envVars = Travis.EnvVar.find repository_id: id
envVars = EnvVar.find repository_id: id
# TODO: move to controller
array = Travis.ExpandableRecordArray.create
type: Travis.EnvVar
array = ExpandableRecordArray.create
type: EnvVar
content: Ember.A([])
array.load(envVars)
globalEnvVars = Ember.RecordArray.create({ modelClass: Travis.EnvVar, content: Ember.A([]) })
Travis.EnvVar.registerRecordArray(globalEnvVars)
globalEnvVars = Ember.RecordArray.create({ modelClass: EnvVar, content: Ember.A([]) })
EnvVar.registerRecordArray(globalEnvVars)
array.observe(globalEnvVars, (envVar) -> envVar.get('isLoaded') && envVar.get('repo.id') == id )
@ -51,18 +60,18 @@ require 'travis/model'
).property()
allBuilds: (->
recordArray = Ember.RecordArray.create({ modelClass: Travis.Build, content: Ember.A([]) })
Travis.Build.registerRecordArray(recordArray)
recordArray = Ember.RecordArray.create({ modelClass: Build, content: Ember.A([]) })
Build.registerRecordArray(recordArray)
recordArray
).property()
builds: (->
id = @get('id')
builds = Travis.Build.byRepoId id, event_type: 'push'
builds = Build.byRepoId id, event_type: 'push'
# TODO: move to controller
array = Travis.ExpandableRecordArray.create
type: Travis.Build
array = ExpandableRecordArray.create
type: Build
content: Ember.A([])
array.load(builds)
@ -75,9 +84,9 @@ require 'travis/model'
pullRequests: (->
id = @get('id')
builds = Travis.Build.byRepoId id, event_type: 'pull_request'
array = Travis.ExpandableRecordArray.create
type: Travis.Build
builds = Build.byRepoId id, event_type: 'pull_request'
array = ExpandableRecordArray.create
type: Build
content: Ember.A([])
array.load(builds)
@ -89,11 +98,11 @@ require 'travis/model'
).property()
branches: (->
Travis.Build.branches repoId: @get('id')
Build.branches repoId: @get('id')
).property()
events: (->
Travis.Event.byRepoId @get('id')
Event.byRepoId @get('id')
).property()
owner: (->
@ -106,7 +115,7 @@ require 'travis/model'
lastBuildDuration: (->
duration = @get('_lastBuildDuration')
duration = Travis.Helpers.durationFrom(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration
duration = durationFrom(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration
duration
).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt')
@ -130,16 +139,16 @@ require 'travis/model'
@notifyPropertyChange 'lastBuildDuration'
regenerateKey: (options) ->
Travis.ajax.ajax '/repos/' + @get('id') + '/key', 'post', options
Ajax.ajax '/repos/' + @get('id') + '/key', 'post', options
fetchSettings: ->
Travis.ajax.ajax('/repos/' + @get('id') + '/settings', 'get', forceAuth: true).then (data) ->
Ajax.ajax('/repos/' + @get('id') + '/settings', 'get', forceAuth: true).then (data) ->
data['settings']
saveSettings: (settings) ->
Travis.ajax.ajax('/repos/' + @get('id') + '/settings', 'patch', data: { settings: settings })
Ajax.ajax('/repos/' + @get('id') + '/settings', 'patch', data: { settings: settings })
@Travis.Repo.reopenClass
Travis.Repo.reopenClass
recent: ->
@find()
@ -154,12 +163,12 @@ require 'travis/model'
withLastBuild: ->
filtered = Ember.FilteredRecordArray.create(
modelClass: Travis.Repo
modelClass: this
filterFunction: (repo) -> repo.get('lastBuildId')
filterProperties: ['lastBuildId']
)
Travis.Repo.fetch().then (array) ->
@fetch().then (array) ->
filtered.updateFilter()
filtered.set('isLoaded', true)

View File

@ -1,6 +1,10 @@
require 'travis/ajax'
require 'travis/model'
Ajax = Travis.ajax
trigger = Travis.trigger
Account = Travis.Account
@Travis.User = Travis.Model.extend
_name: Ember.attr('string', key: 'name')
email: Ember.attr('string')
@ -30,7 +34,7 @@ require 'travis/model'
).property()
_rawPermissions: (->
Travis.ajax.get('/users/permissions')
Ajax.get('/users/permissions')
).property()
permissions: (->
@ -63,12 +67,12 @@ require 'travis/model'
sync: ->
self = this
Travis.ajax.post('/users/sync', {}, ->
Ajax.post('/users/sync', {}, ->
self.setWithSession('isSyncing', true)
)
poll: ->
Travis.ajax.get '/users', (data) =>
Ajax.get '/users', (data) =>
if data.user.is_syncing
self = this
setTimeout ->
@ -77,10 +81,10 @@ require 'travis/model'
else
@set('isSyncing', false)
@setWithSession('syncedAt', data.user.synced_at)
Travis.trigger('user:synced', data.user)
trigger('user:synced', data.user)
# need to pass any param to trigger findQuery
Travis.Account.find(foo: '')
Account.find(foo: '')
setWithSession: (name, value) ->
@set(name, value)

View File

@ -0,0 +1,18 @@
require 'helpers/helpers'
durationFrom = Travis.Helpers.durationFrom
Travis.DurationCalculations = Ember.Mixin.create
duration: (->
if @get('notStarted')
null
else if duration = @get('_duration')
duration
else
durationFrom(@get('startedAt'), @get('finishedAt'))
).property('_duration', 'finishedAt', 'startedAt', 'notStarted', '_finishedAt', '_startedAt')
updateTimes: ->
unless @get('isFinished')
@notifyPropertyChange '_duration'
@notifyPropertyChange 'finished_at'