Improved code layout.

This commit is contained in:
Martin Edenhofer 2016-01-24 12:50:29 +01:00
parent aa4fffad32
commit 87e1a92e39
4 changed files with 51 additions and 51 deletions

View file

@ -168,7 +168,7 @@ returns:
# lookups # lookups
if object if object
object_lookup_id = ObjectLookup.by_name( object ) object_lookup_id = ObjectLookup.by_name(object)
end end
# get attributes in right order # get attributes in right order
@ -198,7 +198,7 @@ returns:
} }
end end
if item.data_option if item.data_option
data = data.merge( item.data_option.symbolize_keys ) data = data.merge(item.data_option.symbolize_keys)
end end
attributes.push data attributes.push data
} }

View file

@ -14,10 +14,10 @@ class Store < ApplicationModel
add an attachment to storage add an attachment to storage
result = Store.add( result = Store.add(
:object => 'Ticket::Article', object: 'Ticket::Article',
:o_id => 4711, o_id: 4711,
:data => binary_string, data: binary_string,
:preferences => { preferences: {
:content_type => 'image/png', :content_type => 'image/png',
:content_id => 234, :content_id => 234,
} }
@ -33,11 +33,11 @@ returns
data = data.stringify_keys data = data.stringify_keys
# lookup store_object.id # lookup store_object.id
store_object = Store::Object.create_if_not_exists( name: data['object'] ) store_object = Store::Object.create_if_not_exists(name: data['object'])
data['store_object_id'] = store_object.id data['store_object_id'] = store_object.id
# add to real store # add to real store
file = Store::File.add( data['data'] ) file = Store::File.add(data['data'])
data['size'] = data['data'].to_s.bytesize data['size'] = data['data'].to_s.bytesize
data['store_file_id'] = file.id data['store_file_id'] = file.id
@ -57,8 +57,8 @@ returns
get attachment of object get attachment of object
list = Store.list( list = Store.list(
:object => 'Ticket::Article', object: 'Ticket::Article',
:o_id => 4711, o_id: 4711,
) )
returns returns
@ -66,9 +66,9 @@ returns
result = [store1, store2] result = [store1, store2]
store1 = { store1 = {
:size => 94123, size: 94123,
:filename => 'image.png', filename: 'image.png',
:preferences => { preferences: {
:content_type => 'image/png', :content_type => 'image/png',
:content_id => 234, :content_id => 234,
} }
@ -79,8 +79,8 @@ returns
def self.list(data) def self.list(data)
# search # search
store_object_id = Store::Object.lookup( name: data[:object] ) store_object_id = Store::Object.lookup(name: data[:object])
stores = Store.where( store_object_id: store_object_id, o_id: data[:o_id].to_i ) stores = Store.where(store_object_id: store_object_id, o_id: data[:o_id].to_i)
.order('created_at ASC, id ASC') .order('created_at ASC, id ASC')
stores stores
end end
@ -90,8 +90,8 @@ returns
remove attachments of object from storage remove attachments of object from storage
result = Store.remove( result = Store.remove(
:object => 'Ticket::Article', object: 'Ticket::Article',
:o_id => 4711, o_id: 4711,
) )
returns returns
@ -102,14 +102,14 @@ returns
def self.remove(data) def self.remove(data)
# search # search
store_object_id = Store::Object.lookup( name: data[:object] ) store_object_id = Store::Object.lookup(name: data[:object])
stores = Store.where( store_object_id: store_object_id ) stores = Store.where(store_object_id: store_object_id)
.where( o_id: data[:o_id] ) .where(o_id: data[:o_id])
.order('created_at ASC, id ASC') .order('created_at ASC, id ASC')
stores.each do |store| stores.each do |store|
# check backend for references # check backend for references
Store.remove_item( store.id ) Store.remove_item(store.id)
end end
true true
end end
@ -130,9 +130,9 @@ returns
# check backend for references # check backend for references
store = Store.find(store_id) store = Store.find(store_id)
files = Store.where( store_file_id: store.store_file_id ) files = Store.where(store_file_id: store.store_file_id)
if files.count == 1 && files.first.id == store.id if files.count == 1 && files.first.id == store.id
Store::File.find( store.store_file_id ).destroy Store::File.find(store.store_file_id).destroy
end end
store.destroy store.destroy
@ -140,7 +140,7 @@ returns
end end
def content def content
file = Store::File.find_by( id: store_file_id ) file = Store::File.find_by(id: store_file_id)
if !file if !file
fail "No such file #{store_file_id}!" fail "No such file #{store_file_id}!"
end end
@ -148,7 +148,7 @@ returns
end end
def provider def provider
file = Store::File.find_by( id: store_file_id ) file = Store::File.find_by(id: store_file_id)
if !file if !file
fail "No such file #{store_file_id}!" fail "No such file #{store_file_id}!"
end end

View file

@ -612,7 +612,7 @@ module Import::OTRS
# find owner # find owner
if ticket_new[:owner] if ticket_new[:owner]
user = User.lookup( login: ticket_new[:owner].downcase ) user = User.lookup(login: ticket_new[:owner])
ticket_new[:owner_id] = if user ticket_new[:owner_id] = if user
user.id user.id
else else
@ -623,7 +623,7 @@ module Import::OTRS
# find customer # find customer
if ticket_new[:customer] if ticket_new[:customer]
user = User.lookup( login: ticket_new[:customer].downcase ) user = User.lookup(login: ticket_new[:customer])
ticket_new[:customer_id] = if user ticket_new[:customer_id] = if user
user.id user.id
else else
@ -922,13 +922,13 @@ module Import::OTRS
} }
# check if state already exists # check if state already exists
state_old = Ticket::State.where( id: state_new[:id] ).first state_old = Ticket::State.lookup( id: state_new[:id] )
# set state types # set state types
if state['TypeName'] == 'pending auto' if state['TypeName'] == 'pending auto'
state['TypeName'] = 'pending action' state['TypeName'] = 'pending action'
end end
state_type = Ticket::StateType.where( name: state['TypeName'] ).first state_type = Ticket::StateType.lookup( name: state['TypeName'] )
state_new[:state_type_id] = state_type.id state_new[:state_type_id] = state_type.id
if state_old if state_old
state_old.update_attributes(state_new) state_old.update_attributes(state_new)
@ -969,7 +969,7 @@ module Import::OTRS
} }
# check if state already exists # check if state already exists
priority_old = Ticket::Priority.where( id: priority_new[:id] ).first priority_old = Ticket::Priority.lookup( id: priority_new[:id] )
# set state types # set state types
if priority_old if priority_old
@ -1010,7 +1010,7 @@ module Import::OTRS
} }
# check if state already exists # check if state already exists
group_old = Group.where( id: group_new[:id] ).first group_old = Group.lookup( id: group_new[:id] )
# set state types # set state types
if group_old if group_old
@ -1070,7 +1070,7 @@ module Import::OTRS
end end
# check if agent already exists # check if agent already exists
user_old = User.where( id: user_new[:id] ).first user_old = User.lookup( id: user_new[:id] )
# check if login is already used # check if login is already used
login_in_use = User.where( "login = ? AND id != #{user_new[:id]}", user_new[:login].downcase ).count login_in_use = User.where( "login = ? AND id != #{user_new[:id]}", user_new[:login].downcase ).count
@ -1223,7 +1223,7 @@ module Import::OTRS
} }
# check if customer already exists # check if customer already exists
user_old = User.where( login: user_new[:login].downcase ).first user_old = User.lookup( login: user_new[:login] )
# create / update agent # create / update agent
if user_old if user_old
@ -1252,7 +1252,7 @@ module Import::OTRS
if user['UserCustomerID'] if user['UserCustomerID']
organizations.each {|organization| organizations.each {|organization|
next if user['UserCustomerID'] != organization['CustomerID'] next if user['UserCustomerID'] != organization['CustomerID']
organization = Organization.where(name: organization['CustomerCompanyName'] ).first organization = Organization.lookup(name: organization['CustomerCompanyName'] )
organization_id = organization.id organization_id = organization.id
} }
end end
@ -1285,7 +1285,7 @@ module Import::OTRS
} }
# check if state already exists # check if state already exists
organization_old = Organization.where( name: organization_new[:name] ).first organization_old = Organization.lookup( name: organization_new[:name] )
# set state types # set state types
if organization_old if organization_old
@ -1419,9 +1419,9 @@ module Import::OTRS
end end
end end
user = User.where( email: email.downcase ).first user = User.lookup(email: email)
if !user if !user
user = User.where( login: email.downcase ).first user = User.lookup(login: email)
end end
if !user if !user
begin begin
@ -1449,7 +1449,7 @@ module Import::OTRS
) )
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
log "User #{email} was handled by another thread, taking this." log "User #{email} was handled by another thread, taking this."
user = User.find_by( login: email.downcase ) user = User.lookup(login: email)
if !user if !user
log "User #{email} wasn't created sleep and retry." log "User #{email} wasn't created sleep and retry."
sleep rand 3 sleep rand 3

View file

@ -515,9 +515,9 @@ module Import::Zendesk
@zendesk_user_mapping = {} @zendesk_user_mapping = {}
role_admin = Role.find_by( name: 'Admin' ) role_admin = Role.lookup(name: 'Admin')
role_agent = Role.find_by( name: 'Agent' ) role_agent = Role.lookup(name: 'Agent')
role_customer = Role.find_by( name: 'Customer' ) role_customer = Role.lookup(name: 'Customer')
@client.users.all { |zendesk_user| @client.users.all { |zendesk_user|
@ -612,18 +612,18 @@ module Import::Zendesk
# https://developer.zendesk.com/rest_api/docs/core/ticket_audits # v2 # https://developer.zendesk.com/rest_api/docs/core/ticket_audits # v2
def import_tickets def import_tickets
article_sender_customer = Ticket::Article::Sender.find_by(name: 'Customer') article_sender_customer = Ticket::Article::Sender.lookup(name: 'Customer')
article_sender_agent = Ticket::Article::Sender.find_by(name: 'Agent') article_sender_agent = Ticket::Article::Sender.lookup(name: 'Agent')
article_sender_system = Ticket::Article::Sender.find_by(name: 'System') article_sender_system = Ticket::Article::Sender.lookup(name: 'System')
# TODO # TODO
article_type_web = Ticket::Article::Type.find_by(name: 'web') article_type_web = Ticket::Article::Type.lookup(name: 'web')
article_type_note = Ticket::Article::Type.find_by(name: 'note') article_type_note = Ticket::Article::Type.lookup(name: 'note')
article_type_email = Ticket::Article::Type.find_by(name: 'email') article_type_email = Ticket::Article::Type.lookup(name: 'email')
article_type_twitter_status = Ticket::Article::Type.find_by(name: 'twitter status') article_type_twitter_status = Ticket::Article::Type.lookup(name: 'twitter status')
article_type_twitter_dm = Ticket::Article::Type.find_by(name: 'twitter direct-message') article_type_twitter_dm = Ticket::Article::Type.lookup(name: 'twitter direct-message')
article_type_facebook_feed_post = Ticket::Article::Type.find_by(name: 'facebook feed post') article_type_facebook_feed_post = Ticket::Article::Type.lookup(name: 'facebook feed post')
article_type_facebook_feed_comment = Ticket::Article::Type.find_by(name: 'facebook feed comment') article_type_facebook_feed_comment = Ticket::Article::Type.lookup(name: 'facebook feed comment')
@client.tickets.all { |zendesk_ticket| @client.tickets.all { |zendesk_ticket|