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

View File

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

View File

@ -1,7 +1,14 @@
require 'travis/model' require 'travis/model'
require 'models/extensions' 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') repositoryId: Ember.attr('number')
commitId: Ember.attr('number') commitId: Ember.attr('number')
@ -26,7 +33,7 @@ require 'models/extensions'
config: (-> config: (->
console.log('config') console.log('config')
if config = @get('_config') if config = @get('_config')
Travis.Helpers.compact(config) compact(config)
else else
return if @get('isFetchingConfig') return if @get('isFetchingConfig')
@set 'isFetchingConfig', true @set 'isFetchingConfig', true
@ -77,7 +84,7 @@ require 'models/extensions'
keys = [] keys = []
@get('jobs').forEach (job) -> @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.pushObject key unless keys.contains key
keys keys
@ -86,7 +93,7 @@ require 'models/extensions'
configKeys: (-> configKeys: (->
keys = @get('rawConfigKeys') keys = @get('rawConfigKeys')
headers = ['Job', 'Duration', 'Finished'] 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') ).property('rawConfigKeys.length')
canCancel: (-> canCancel: (->
@ -94,11 +101,11 @@ require 'models/extensions'
).property('isFinished', 'jobs.@each.canCancel') ).property('isFinished', 'jobs.@each.canCancel')
cancel: (-> cancel: (->
Travis.ajax.post "/builds/#{@get('id')}/cancel" Ajax.post "/builds/#{@get('id')}/cancel"
) )
requeue: -> requeue: ->
Travis.ajax.post "/builds/#{@get('id')}/restart" Ajax.post "/builds/#{@get('id')}/restart"
formattedFinishedAt: (-> formattedFinishedAt: (->
if finishedAt = @get('finishedAt') 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' require 'travis/model'
loadOrMerge = Travis.loadOrMerge
Repo = Travis.Repo
@Travis.Hook = Travis.Model.extend @Travis.Hook = Travis.Model.extend
name: Ember.attr('string') name: Ember.attr('string')
ownerName: 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 # 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 # a link to the repo and if more info is needed, it will be requested when the
# link is used # link is used
Travis.loadOrMerge(Travis.Repo, @getProperties('id', 'slug', 'name', 'ownerName'), skipIfExists: true) loadOrMerge(Repo, @getProperties('id', 'slug', 'name', 'ownerName'), skipIfExists: true)
Travis.Repo.find(@get('id')) Repo.find(@get('id'))
).property('id') ).property('id')

View File

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

View File

@ -1,6 +1,41 @@
require 'travis/model' require 'travis/model'
require 'travis/log_chunks' 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 @Travis.Log = Em.Object.extend
version: 0 # used to refresh log on requeue version: 0 # used to refresh log on requeue
isLoaded: false isLoaded: false
@ -13,7 +48,7 @@ require 'travis/log_chunks'
data['part_numbers'] = partNumbers if partNumbers data['part_numbers'] = partNumbers if partNumbers
data['after'] = after if after data['after'] = after if after
Travis.ajax.ajax "/jobs/#{@get('job.id')}/log", 'GET', Ajax.ajax "/jobs/#{@get('job.id')}/log", 'GET',
dataType: 'json' dataType: 'json'
headers: headers:
accept: 'application/json; chunked=true; version=2' accept: 'application/json; chunked=true; version=2'
@ -44,7 +79,7 @@ require 'travis/log_chunks'
@set('removed', true) @set('removed', true)
@loadParts(json['log']['parts']) @loadParts(json['log']['parts'])
text: (text) => @loadText(text) 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: -> clear: ->
@clearParts() @clearParts()
@ -63,35 +98,3 @@ require 'travis/log_chunks'
console.log 'log model: load text' if Log.DEBUG console.log 'log model: load text' if Log.DEBUG
@append(number: 1, content: text, final: true) @append(number: 1, content: text, final: true)
@set('isLoaded', 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/expandable_record_array'
require 'travis/model' 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 @Travis.Repo = Travis.Model.extend
id: Ember.attr('string') id: Ember.attr('string')
@ -28,22 +37,22 @@ require 'travis/model'
@filter( (repo) -> repo.get('lastBuildId') ) @filter( (repo) -> repo.get('lastBuildId') )
sshKey: (-> sshKey: (->
Travis.SshKey.find(@get('id')) SshKey.find(@get('id'))
) )
envVars: (-> envVars: (->
id = @get('id') id = @get('id')
envVars = Travis.EnvVar.find repository_id: id envVars = EnvVar.find repository_id: id
# TODO: move to controller # TODO: move to controller
array = Travis.ExpandableRecordArray.create array = ExpandableRecordArray.create
type: Travis.EnvVar type: EnvVar
content: Ember.A([]) content: Ember.A([])
array.load(envVars) array.load(envVars)
globalEnvVars = Ember.RecordArray.create({ modelClass: Travis.EnvVar, content: Ember.A([]) }) globalEnvVars = Ember.RecordArray.create({ modelClass: EnvVar, content: Ember.A([]) })
Travis.EnvVar.registerRecordArray(globalEnvVars) EnvVar.registerRecordArray(globalEnvVars)
array.observe(globalEnvVars, (envVar) -> envVar.get('isLoaded') && envVar.get('repo.id') == id ) array.observe(globalEnvVars, (envVar) -> envVar.get('isLoaded') && envVar.get('repo.id') == id )
@ -51,18 +60,18 @@ require 'travis/model'
).property() ).property()
allBuilds: (-> allBuilds: (->
recordArray = Ember.RecordArray.create({ modelClass: Travis.Build, content: Ember.A([]) }) recordArray = Ember.RecordArray.create({ modelClass: Build, content: Ember.A([]) })
Travis.Build.registerRecordArray(recordArray) Build.registerRecordArray(recordArray)
recordArray recordArray
).property() ).property()
builds: (-> builds: (->
id = @get('id') id = @get('id')
builds = Travis.Build.byRepoId id, event_type: 'push' builds = Build.byRepoId id, event_type: 'push'
# TODO: move to controller # TODO: move to controller
array = Travis.ExpandableRecordArray.create array = ExpandableRecordArray.create
type: Travis.Build type: Build
content: Ember.A([]) content: Ember.A([])
array.load(builds) array.load(builds)
@ -75,9 +84,9 @@ require 'travis/model'
pullRequests: (-> pullRequests: (->
id = @get('id') id = @get('id')
builds = Travis.Build.byRepoId id, event_type: 'pull_request' builds = Build.byRepoId id, event_type: 'pull_request'
array = Travis.ExpandableRecordArray.create array = ExpandableRecordArray.create
type: Travis.Build type: Build
content: Ember.A([]) content: Ember.A([])
array.load(builds) array.load(builds)
@ -89,11 +98,11 @@ require 'travis/model'
).property() ).property()
branches: (-> branches: (->
Travis.Build.branches repoId: @get('id') Build.branches repoId: @get('id')
).property() ).property()
events: (-> events: (->
Travis.Event.byRepoId @get('id') Event.byRepoId @get('id')
).property() ).property()
owner: (-> owner: (->
@ -106,7 +115,7 @@ require 'travis/model'
lastBuildDuration: (-> lastBuildDuration: (->
duration = @get('_lastBuildDuration') duration = @get('_lastBuildDuration')
duration = Travis.Helpers.durationFrom(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration duration = durationFrom(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration
duration duration
).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt') ).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt')
@ -130,16 +139,16 @@ require 'travis/model'
@notifyPropertyChange 'lastBuildDuration' @notifyPropertyChange 'lastBuildDuration'
regenerateKey: (options) -> regenerateKey: (options) ->
Travis.ajax.ajax '/repos/' + @get('id') + '/key', 'post', options Ajax.ajax '/repos/' + @get('id') + '/key', 'post', options
fetchSettings: -> 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'] data['settings']
saveSettings: (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: -> recent: ->
@find() @find()
@ -154,12 +163,12 @@ require 'travis/model'
withLastBuild: -> withLastBuild: ->
filtered = Ember.FilteredRecordArray.create( filtered = Ember.FilteredRecordArray.create(
modelClass: Travis.Repo modelClass: this
filterFunction: (repo) -> repo.get('lastBuildId') filterFunction: (repo) -> repo.get('lastBuildId')
filterProperties: ['lastBuildId'] filterProperties: ['lastBuildId']
) )
Travis.Repo.fetch().then (array) -> @fetch().then (array) ->
filtered.updateFilter() filtered.updateFilter()
filtered.set('isLoaded', true) filtered.set('isLoaded', true)

View File

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