From 6a59aff4b6bb01e5b9e227cbaf13260eb7ff313d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 17 Jun 2014 21:49:41 +0200 Subject: [PATCH] Improved @_fillUp of application model (get references based on confgure_attributes). --- .../app/models/_application_model.js.coffee | 16 ++++- .../javascripts/app/models/channel.js.coffee | 7 --- .../javascripts/app/models/overview.js.coffee | 7 ++- .../app/models/postmaster_filter.js.coffee | 18 +++--- .../javascripts/app/models/role.js.coffee | 11 ++-- .../app/models/signature.js.coffee | 13 ++-- .../javascripts/app/models/sla.js.coffee | 20 ++++--- .../javascripts/app/models/ticket.js.coffee | 59 +++---------------- .../app/models/ticket_article.js.coffee | 36 ++++------- 9 files changed, 77 insertions(+), 110 deletions(-) diff --git a/app/assets/javascripts/app/models/_application_model.js.coffee b/app/assets/javascripts/app/models/_application_model.js.coffee index 8b6614a01..07e6cec81 100644 --- a/app/assets/javascripts/app/models/_application_model.js.coffee +++ b/app/assets/javascripts/app/models/_application_model.js.coffee @@ -287,7 +287,21 @@ class App.Model extends Spine.Model return true @_fillUp: (data) -> - # nothing + + # fill up via relations + if App[ @className ].configure_attributes + for attribute in App[ @className ].configure_attributes + if attribute.relation + if App[ attribute.relation ] + withoutId = attribute.name.substr( 0, attribute.name.length - 3 ) + if attribute.name.substr( attribute.name.length - 3, attribute.name.length ) is '_id' + if data[attribute.name] + if App[ attribute.relation ].exists( data[attribute.name] ) + item = App[ attribute.relation ].find( data[attribute.name] ) + item = App[ attribute.relation ]._fillUp(item) + data[ withoutId ] = item + else + console.log("ERROR, cant find App.#{ attribute.relation }.find(#{ data[attribute.name] }) for '#{ data.constructor.className }' #{ data.displayName() }") data @search: (params) -> diff --git a/app/assets/javascripts/app/models/channel.js.coffee b/app/assets/javascripts/app/models/channel.js.coffee index b58ea7c98..dc528c970 100644 --- a/app/assets/javascripts/app/models/channel.js.coffee +++ b/app/assets/javascripts/app/models/channel.js.coffee @@ -17,10 +17,3 @@ class App.Channel extends App.Model @configure_overview = [ 'adapter', 'options::host', 'options::user', 'group' ] - - @_fillUp: (data) -> - - # group - data.group = App.Group.find( data.group_id ) - - data \ No newline at end of file diff --git a/app/assets/javascripts/app/models/overview.js.coffee b/app/assets/javascripts/app/models/overview.js.coffee index ae7b2c1ae..c82c09646 100644 --- a/app/assets/javascripts/app/models/overview.js.coffee +++ b/app/assets/javascripts/app/models/overview.js.coffee @@ -131,8 +131,11 @@ class App.Overview extends App.Model owner: 'Owner' class: 'span4' }, - { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, - { name: 'active', display: 'Active', tag: 'boolean', note: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'active', display: 'Active', tag: 'boolean', note: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, + { name: 'created_at', display: 'Created', type: 'time', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, ] @configure_delete = true @configure_overview = [ diff --git a/app/assets/javascripts/app/models/postmaster_filter.js.coffee b/app/assets/javascripts/app/models/postmaster_filter.js.coffee index 3476d9b94..eea1540e7 100644 --- a/app/assets/javascripts/app/models/postmaster_filter.js.coffee +++ b/app/assets/javascripts/app/models/postmaster_filter.js.coffee @@ -4,13 +4,17 @@ class App.PostmasterFilter extends App.Model @url: @apiPath + '/postmaster_filters' @configure_attributes = [ - { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 250, 'null': false, 'class': 'span4' }, - { name: 'channel', display: 'Channel', type: 'input', readonly: 1 }, - { name: 'match', display: 'Match all of the following', tag: 'postmaster_match' }, - { name: 'perform', display: 'Perform action of the following', tag: 'postmaster_set' }, - { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, 'null': true, 'class': 'span4' }, - { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, - { name: 'active', display: 'Active', tag: 'boolean', type: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 250, 'null': false }, + { name: 'channel', display: 'Channel', type: 'input', readonly: 1 }, + { name: 'match', display: 'Match all of the following', tag: 'postmaster_match' }, + { name: 'perform', display: 'Perform action of the following', tag: 'postmaster_set' }, + { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, 'null': true }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, + { name: 'active', display: 'Active', tag: 'boolean', type: 'boolean', 'default': true, 'null': false }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, + { name: 'created_at', display: 'Created', type: 'time', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, ] @configure_delete = true @configure_overview = [ diff --git a/app/assets/javascripts/app/models/role.js.coffee b/app/assets/javascripts/app/models/role.js.coffee index 1e1830ba0..fa733bb4e 100644 --- a/app/assets/javascripts/app/models/role.js.coffee +++ b/app/assets/javascripts/app/models/role.js.coffee @@ -3,10 +3,13 @@ class App.Role extends App.Model @extend Spine.Model.Ajax @url: @apiPath + '/roles' @configure_attributes = [ - { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, 'null': false, 'class': 'span4' }, - { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, 'null': true, 'class': 'span4' }, - { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, - { name: 'active', display: 'Active', tag: 'boolean', type: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, null: false }, + { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, null: true }, + { name: 'active', display: 'Active', tag: 'boolean', type: 'boolean', 'default': true, null: false }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, + { name: 'created_at', display: 'Created', type: 'time', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, ] @configure_overview = [ 'name', diff --git a/app/assets/javascripts/app/models/signature.js.coffee b/app/assets/javascripts/app/models/signature.js.coffee index 52add5a6e..582608372 100644 --- a/app/assets/javascripts/app/models/signature.js.coffee +++ b/app/assets/javascripts/app/models/signature.js.coffee @@ -4,11 +4,14 @@ class App.Signature extends App.Model @url: @apiPath + '/signatures' @configure_attributes = [ - { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, 'null': false, 'class': 'span4' }, - { name: 'body', display: 'Text', tag: 'textarea', limit: 250, 'null': true, 'class': 'span4', rows: 10 }, - { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, 'null': true, 'class': 'span4' }, - { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, - { name: 'active', display: 'Active', tag: 'boolean', type: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, 'null': false, 'class': 'span4' }, + { name: 'body', display: 'Text', tag: 'textarea', limit: 250, 'null': true, 'class': 'span4', rows: 10 }, + { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, 'null': true, 'class': 'span4' }, + { name: 'active', display: 'Active', tag: 'boolean', type: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, + { name: 'created_at', display: 'Created', type: 'time', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, ] @configure_overview = [ 'name', diff --git a/app/assets/javascripts/app/models/sla.js.coffee b/app/assets/javascripts/app/models/sla.js.coffee index 003df8976..e85682eaf 100644 --- a/app/assets/javascripts/app/models/sla.js.coffee +++ b/app/assets/javascripts/app/models/sla.js.coffee @@ -3,12 +3,12 @@ class App.Sla extends App.Model @extend Spine.Model.Ajax @url: @apiPath + '/slas' @configure_attributes = [ - { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, null: false, 'class': 'span4' }, - { name: 'first_response_time', display: 'First Response Time', tag: 'input', type: 'text', limit: 100, null: true, 'class': 'span4', note: 'In minutes, only business times are counted.' }, - { name: 'update_time', display: 'Update Time', tag: 'input', type: 'text', limit: 100, null: true, 'class': 'span4', note: 'In minutes, only business times are counted.' }, - { name: 'close_time', display: 'Solution Time', tag: 'input', type: 'text', limit: 100, null: true, 'class': 'span4', note: 'In minutes, only business times are counted.' }, - { name: 'condition', display: 'Conditions where SLA is used', tag: 'ticket_attribute_selection', null: true, class: 'span4' }, - { name: 'timezone', display: 'Timezone', tag: 'timezone', null: true, class: 'span4' }, + { name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, null: false }, + { name: 'first_response_time', display: 'First Response Time', tag: 'input', type: 'text', limit: 100, null: true, note: 'In minutes, only business times are counted.' }, + { name: 'update_time', display: 'Update Time', tag: 'input', type: 'text', limit: 100, null: true, note: 'In minutes, only business times are counted.' }, + { name: 'close_time', display: 'Solution Time', tag: 'input', type: 'text', limit: 100, null: true, note: 'In minutes, only business times are counted.' }, + { name: 'condition', display: 'Conditions where SLA is used', tag: 'ticket_attribute_selection', null: true }, + { name: 'timezone', display: 'Timezone', tag: 'timezone', null: true }, { name: 'data' display: 'Business Times' @@ -30,10 +30,12 @@ class App.Sla extends App.Model priority: 'Priority' group: 'Group' owner: 'Owner' - class: 'span4' }, - { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, - { name: 'active', display: 'Active', tag: 'boolean', note: 'boolean', 'default': true, 'null': false, 'class': 'span4' }, + { name: 'active', display: 'Active', tag: 'boolean', note: 'boolean', 'default': true, 'null': false }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, + { name: 'created_at', display: 'Created', type: 'time', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, ] @configure_delete = true @configure_overview = [ diff --git a/app/assets/javascripts/app/models/ticket.js.coffee b/app/assets/javascripts/app/models/ticket.js.coffee index 10e9a9d56..ff3d9d030 100644 --- a/app/assets/javascripts/app/models/ticket.js.coffee +++ b/app/assets/javascripts/app/models/ticket.js.coffee @@ -4,11 +4,11 @@ class App.Ticket extends App.Model @url: @apiPath + '/tickets' @configure_attributes = [ { name: 'number', display: '#', tag: 'input', type: 'text', limit: 100, null: true, read_only: true, style: 'width: 8%' }, - { name: 'customer_id', display: 'Customer', tag: 'input', type: 'text', limit: 100, null: false, class: 'span8', autocapitalize: false, help: 'Select the customer of the Ticket or create one.', helpLink: '»' }, - { name: 'organization_id', display: 'Organization', tagreadonly: 1 }, - { name: 'group_id', display: 'Group', tag: 'select', multiple: false, limit: 100, null: false, class: 'span8', relation: 'Group', style: 'width: 10%' }, - { name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, limit: 100, null: true, class: 'span8', relation: 'User', style: 'width: 12%' }, - { name: 'title', display: 'Title', tag: 'input', type: 'text', limit: 100, null: false, class: 'span8' }, + { name: 'customer_id', display: 'Customer', tag: 'input', type: 'text', limit: 100, null: false, autocapitalize: false, relation: 'User' }, + { name: 'organization_id', display: 'Organization', relation: 'Organization', tagreadonly: 1 }, + { name: 'group_id', display: 'Group', tag: 'select', multiple: false, limit: 100, null: false, relation: 'Group', style: 'width: 10%' }, + { name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, limit: 100, null: true, relation: 'User', style: 'width: 12%' }, + { name: 'title', display: 'Title', tag: 'input', type: 'text', limit: 100, null: false }, { name: 'state_id', display: 'State', tag: 'select', multiple: false, null: false, relation: 'TicketState', default: 'new', class: 'medium', style: 'width: 12%' }, { name: 'priority_id', display: 'Priority', tag: 'select', multiple: false, null: false, relation: 'TicketPriority', default: '2 normal', class: 'medium', style: 'width: 12%' }, { name: 'last_contact', display: 'Last contact', type: 'time', null: true, style: 'width: 12%' }, @@ -18,56 +18,11 @@ class App.Ticket extends App.Model { name: 'close_time', display: 'Close time', type: 'time', null: true, style: 'width: 12%' }, { name: 'escalation_time', display: 'Escalation in', type: 'time', null: true, style: 'width: 12%', class: 'escalation' }, { name: 'article_count', display: 'Article#', style: 'width: 12%' }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, { name: 'created_at', display: 'Created', type: 'time', style: 'width: 12%', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, { name: 'updated_at', display: 'Updated', type: 'time', style: 'width: 12%', readonly: 1 }, ] uiUrl: -> '#ticket/zoom/' + @id - - @_fillUp: (data) -> - - # priority - data.priority = App.TicketPriority.find( data.priority_id ) - - # state - data.state = App.TicketState.find( data.state_id ) - - # group - data.group = App.Group.find( data.group_id ) - - # customer - if data.customer_id - if !App.User.exists( data.customer_id ) - console.error("Can't find user for data.customer_id #{data.customer_id} for ticket #{data.id}") - else - data.customer = App.User.find( data.customer_id ) - - # organization_id - if data.organization_id - if !App.Organization.exists( data.organization_id ) - console.error("Can't find user for data.organization_id #{data.organization_id} for ticket #{data.id}") - else - data.organization = App.Organization.find( data.organization_id ) - - # owner - if data.owner_id - if !App.User.exists( data.owner_id ) - console.error("Can't find user for data.owner_id #{data.owner_id} for ticket #{data.id}") - else - data.owner = App.User.find( data.owner_id ) - - # add created & updated - if data.created_by_id - if !App.User.exists( data.created_by_id ) - console.error("Can't find user for data.created_by_id #{data.created_by_id} for ticket #{data.id}") - else - data.created_by = App.User.find( data.created_by_id ) - if data.updated_by_id - if !App.User.exists( data.updated_by_id ) - console.error("Can't find user for data.updated_by_id #{data.updated_by_id} for ticket #{data.id}") - else - data.updated_by = App.User.find( data.updated_by_id ) - - data - diff --git a/app/assets/javascripts/app/models/ticket_article.js.coffee b/app/assets/javascripts/app/models/ticket_article.js.coffee index 8bb740d8a..01a7e7621 100644 --- a/app/assets/javascripts/app/models/ticket_article.js.coffee +++ b/app/assets/javascripts/app/models/ticket_article.js.coffee @@ -3,15 +3,19 @@ class App.TicketArticle extends App.Model @extend Spine.Model.Ajax @url: @apiPath + '/ticket_articles' @configure_attributes = [ - { name: 'ticket_id', display: 'TicketID', null: false, readonly: 1, }, - { name: 'from', display: 'From', tag: 'input', type: 'text', limit: 100, null: false, class: 'span8', }, - { name: 'to', display: 'To', tag: 'input', type: 'text', limit: 100, null: true, class: 'span8', }, - { name: 'cc', display: 'Cc', tag: 'input', type: 'text', limit: 100, null: true, class: 'span8', }, - { name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: true, class: 'span8', }, - { name: 'body', display: 'Text', tag: 'textarea', rows: 5, limit: 100, null: false, class: 'span8', }, - { name: 'type_id', display: 'Type', tag: 'select', multiple: false, null: false, relation: 'TicketArticleType', default: '', class: 'medium' }, - { name: 'sender_id', display: 'Sender', tag: 'select', multiple: false, null: false, relation: 'TicketArticleSender', default: '', class: 'medium' }, - { name: 'internal', display: 'Visibility', tag: 'radio', default: false, null: true, options: { true: 'internal', false: 'public' }, class: 'medium' }, + { name: 'ticket_id', display: 'TicketID', null: false, readonly: 1, }, + { name: 'from', display: 'From', tag: 'input', type: 'text', limit: 100, null: false, }, + { name: 'to', display: 'To', tag: 'input', type: 'text', limit: 100, null: true, }, + { name: 'cc', display: 'Cc', tag: 'input', type: 'text', limit: 100, null: true, }, + { name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: true, }, + { name: 'body', display: 'Text', tag: 'textarea', rows: 5, limit: 100, null: false, }, + { name: 'type_id', display: 'Type', tag: 'select', multiple: false, null: false, relation: 'TicketArticleType', default: '', class: 'medium' }, + { name: 'sender_id', display: 'Sender', tag: 'select', multiple: false, null: false, relation: 'TicketArticleSender', default: '', class: 'medium' }, + { name: 'internal', display: 'Visibility', tag: 'radio', default: false, null: true, options: { true: 'internal', false: 'public' }, class: 'medium' }, + { name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 }, + { name: 'created_at', display: 'Created', type: 'time', readonly: 1 }, + { name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 }, + { name: 'updated_at', display: 'Updated', type: 'time', readonly: 1 }, ] uiUrl: -> @@ -20,20 +24,6 @@ class App.TicketArticle extends App.Model objectDisplayName: -> 'Article' - @_fillUp: (data) -> - - # add created & updated - if data.created_by_id - data.created_by = App.User.retrieve( data.created_by_id ) - if data.updated_by_id - data.updated_by = App.User.retrieve( data.updated_by_id ) - - # add relations - data.type = App.TicketArticleType.find( data.type_id ) - data.sender = App.TicketArticleSender.find( data.sender_id ) - - data - displayName: -> if @subject return @subject