Merge branch 'develop' of github.com:martini/zammad into develop

Conflicts:
	app/controllers/tickets_controller.rb
This commit is contained in:
Martin Edenhofer 2015-04-30 17:54:27 +02:00
commit a95185fcb8
24 changed files with 302 additions and 591 deletions

View file

@ -35,15 +35,17 @@ class ApplicationController < ActionController::Base
# text/plain. # text/plain.
def cors_preflight_check def cors_preflight_check
if request.method == 'OPTIONS'
headers['Access-Control-Allow-Origin'] = '*' return if request.method != 'OPTIONS'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Accept-Language' headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Max-Age'] = '1728000' headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Credentials'] = 'true' headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Accept-Language'
render text: '', content_type: 'text/plain' headers['Access-Control-Max-Age'] = '1728000'
return false headers['Access-Control-Allow-Credentials'] = 'true'
end render text: '', content_type: 'text/plain'
false
end end
private private
@ -90,9 +92,9 @@ class ApplicationController < ActionController::Base
end end
# fill user agent # fill user agent
if !session[:user_agent] return if session[:user_agent]
session[:user_agent] = request.env['HTTP_USER_AGENT']
end session[:user_agent] = request.env['HTTP_USER_AGENT']
end end
def authentication_check_only def authentication_check_only

View file

@ -159,15 +159,15 @@ class TicketArticlesController < ApplicationController
) )
# find file # find file
if list return if !list
file = Store.find(list.first)
send_data( file = Store.find(list.first)
file.content, send_data(
filename: file.filename, file.content,
type: 'message/rfc822', filename: file.filename,
disposition: 'inline' type: 'message/rfc822',
) disposition: 'inline'
end )
end end
end end

View file

@ -531,6 +531,7 @@ class TicketsController < ApplicationController
# remove attachments from upload cache # remove attachments from upload cache
return if !form_id return if !form_id
Store.remove( Store.remove(
object: 'UploadCache', object: 'UploadCache',
o_id: form_id, o_id: form_id,

View file

@ -44,11 +44,11 @@ class ApplicationModel < ActiveRecord::Base
@@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::StateType', 'Ticket::Priority', 'Group', 'User', 'Role' ] @@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::StateType', 'Ticket::Priority', 'Group', 'User', 'Role' ]
def check_attributes_protected def check_attributes_protected
if !Setting.get('system_init_done') || ( Setting.get('import_mode') && @@import_class_list.include?( self.class.to_s ) )
# do noting, use id as it is # do noting, use id as it is
else return if !Setting.get('system_init_done') || ( Setting.get('import_mode') && @@import_class_list.include?( self.class.to_s ) )
self[:id] = nil
end self[:id] = nil
end end
=begin =begin
@ -197,14 +197,15 @@ returns
self.updated_by_id = UserInfo.current_user_id self.updated_by_id = UserInfo.current_user_id
end end
end end
if self.class.column_names.include? 'created_by_id'
if UserInfo.current_user_id return if !self.class.column_names.include? 'created_by_id'
if self.created_by_id && self.created_by_id != UserInfo.current_user_id
logger.info "NOTICE create - self.created_by_id is different: #{self.created_by_id.to_s}/#{UserInfo.current_user_id.to_s}" return if !UserInfo.current_user_id
end
self.created_by_id = UserInfo.current_user_id if self.created_by_id && self.created_by_id != UserInfo.current_user_id
end logger.info "NOTICE create - self.created_by_id is different: #{self.created_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
end end
self.created_by_id = UserInfo.current_user_id
end end
=begin =begin
@ -223,9 +224,9 @@ returns
def fill_up_user_update def fill_up_user_update
return if !self.class.column_names.include? 'updated_by_id' return if !self.class.column_names.include? 'updated_by_id'
if UserInfo.current_user_id return if !UserInfo.current_user_id
self.updated_by_id = UserInfo.current_user_id
end self.updated_by_id = UserInfo.current_user_id
end end
def cache_update(o) def cache_update(o)
@ -268,12 +269,13 @@ returns
key = self.class.to_s + ':f:' + self.name.to_s key = self.class.to_s + ':f:' + self.name.to_s
Cache.delete( key.to_s ) Cache.delete( key.to_s )
end end
if self[:login]
key = self.class.to_s + '::' + self.login.to_s return if !self[:login]
Cache.delete( key.to_s )
key = self.class.to_s + ':f:' + self.login.to_s key = self.class.to_s + '::' + self.login.to_s
Cache.delete( key.to_s ) Cache.delete( key.to_s )
end key = self.class.to_s + ':f:' + self.login.to_s
Cache.delete( key.to_s )
end end
def self.cache_set(data_id, data, full = false) def self.cache_set(data_id, data, full = false)
@ -962,9 +964,9 @@ store attachments for this object
self.attachments_buffer = attachments self.attachments_buffer = attachments
# update if object already exists # update if object already exists
if self.id && self.id != 0 return if !( self.id && self.id != 0 )
attachments_buffer_check
end attachments_buffer_check
end end
=begin =begin

View file

@ -91,8 +91,9 @@ class Channel::IMAP < Channel::EmailParser
end end
def disconnect def disconnect
if @imap
@imap.disconnect() return if !@imap
end
@imap.disconnect()
end end
end end

View file

@ -72,9 +72,10 @@ class Channel::POP3 < Channel::EmailParser
end end
def disconnect def disconnect
if @pop
@pop.finish return if !@pop
end
@pop.finish
end end
end end

View file

@ -13,9 +13,10 @@ class Channel::TWITTER2
end end
def disconnect def disconnect
if @client
@client = nil return if !@client
end
@client = nil
end end
def fetch (channel) def fetch (channel)
@ -288,16 +289,15 @@ class Channel::TWITTER2
return dm return dm
end end
if attr[:type] == 'twitter status' return if attr[:type] != 'twitter status'
message = client.update(
attr[:body].to_s,
{
in_reply_to_status_id: attr[:in_reply_to]
}
)
# puts message.inspect
return message
end
message = client.update(
attr[:body].to_s,
{
in_reply_to_status_id: attr[:in_reply_to]
}
)
# puts message.inspect
return message
end end
end end

View file

@ -15,11 +15,12 @@ class Observer::Session < ActiveRecord::Observer
def check(record) def check(record)
return if !record.data return if !record.data
return if record[:request_type] return if record[:request_type]
# remember request type # remember request type
if record.data['request_type'] return if !record.data['request_type']
record[:request_type] = record.data['request_type']
record.data.delete('request_type') record[:request_type] = record.data['request_type']
end record.data.delete('request_type')
end end
end end

View file

@ -44,17 +44,18 @@ class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
recipient_list += record[key] recipient_list += record[key]
end end
} }
if recipient_list != ''
History.add( return if recipient_list == ''
o_id: record.id,
history_type: 'email', History.add(
history_object: 'Ticket::Article', o_id: record.id,
related_o_id: ticket.id, history_type: 'email',
related_history_object: 'Ticket', history_object: 'Ticket::Article',
value_from: record.subject, related_o_id: ticket.id,
value_to: recipient_list, related_history_object: 'Ticket',
created_by_id: record.created_by_id, value_from: record.subject,
) value_to: recipient_list,
end created_by_id: record.created_by_id,
)
end end
end end

View file

@ -14,9 +14,9 @@ class Observer::Ticket::Article::FillupFromGeneral < ActiveRecord::Observer
return if sender['name'] == 'Customer' return if sender['name'] == 'Customer'
# set from if not given # set from if not given
if !record.from return if record.from
user = User.find( record.created_by_id )
record.from = "#{user.firstname} #{user.lastname}" user = User.find( record.created_by_id )
end record.from = "#{user.firstname} #{user.lastname}"
end end
end end

View file

@ -31,16 +31,15 @@ class Observer::Ticket::LastContact < ActiveRecord::Observer
end end
# if sender is not agent # if sender is not agent
if sender.name == 'Agent' return if sender.name != 'Agent'
# set last_contact_agent # set last_contact_agent
record.ticket.last_contact_agent = record.created_at record.ticket.last_contact_agent = record.created_at
# set last_contact # set last_contact
record.ticket.last_contact = record.created_at record.ticket.last_contact = record.created_at
# save ticket # save ticket
record.ticket.save record.ticket.save
end
end end
end end

View file

@ -128,15 +128,15 @@ class Observer::Ticket::Notification::BackgroundJob
end end
# add history record # add history record
if recipient_list != '' return if recipient_list == ''
History.add(
o_id: ticket.id, History.add(
history_type: 'notification', o_id: ticket.id,
history_object: 'Ticket', history_type: 'notification',
value_to: recipient_list, history_object: 'Ticket',
created_by_id: ticket.updated_by_id ||  1 value_to: recipient_list,
) created_by_id: ticket.updated_by_id ||  1
end )
end end
def human_changes(user, record) def human_changes(user, record)

View file

@ -40,8 +40,8 @@ class Observer::Ticket::RefObjectTouch < ActiveRecord::Observer
end end
# touch new/current organization # touch new/current organization
if record.organization return if !record.organization
record.organization.touch
end record.organization.touch
end end
end end

View file

@ -42,9 +42,10 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
need_update = true need_update = true
customer[:preferences][:tickets_closed] = tickets_closed customer[:preferences][:tickets_closed] = tickets_closed
end end
if need_update
customer.save return if !need_update
end
customer.save
end end
end end

View file

@ -58,10 +58,11 @@ class Setting < ApplicationModel
self.state_initial = self.state self.state_initial = self.state
end end
def state_check def state_check
if self.state || self.state == false
if !self.state.respond_to?('has_key?') || !self.state.has_key?(:value) return if !(self.state || self.state == false)
self.state = { value: self.state }
end return if !( !self.state.respond_to?('has_key?') || !self.state.has_key?(:value) )
end
self.state = { value: self.state }
end end
end end

View file

@ -201,21 +201,23 @@ returns
end end
def check_title def check_title
if self.title
self.title.gsub!(/\s|\t|\r/, ' ') return if !self.title
end
self.title.gsub!(/\s|\t|\r/, ' ')
end end
def check_defaults def check_defaults
if !self.owner_id if !self.owner_id
self.owner_id = 1 self.owner_id = 1
end end
if self.customer_id
customer = User.find( self.customer_id ) return if !self.customer_id
if self.organization_id != customer.organization_id
self.organization_id = customer.organization_id customer = User.find( self.customer_id )
end return if self.organization_id == customer.organization_id
end
self.organization_id = customer.organization_id
end end
def reset_pending_time def reset_pending_time
@ -228,9 +230,9 @@ returns
current_state_type = Ticket::StateType.lookup( id: current_state.state_type_id ) current_state_type = Ticket::StateType.lookup( id: current_state.state_type_id )
# in case, set pending_time to nil # in case, set pending_time to nil
if current_state_type.name !~ /^pending/i return if current_state_type.name =~ /^pending/i
self.pending_time = nil
end self.pending_time = nil
end end
def destroy_dependencies def destroy_dependencies

View file

@ -30,9 +30,10 @@ class Ticket::Article < ApplicationModel
private private
def check_subject def check_subject
if self.subject
self.subject.gsub!(/\s|\t|\r/, ' ') return if !self.subject
end
self.subject.gsub!(/\s|\t|\r/, ' ')
end end
class Flag < ApplicationModel class Flag < ApplicationModel

View file

@ -152,10 +152,11 @@ returns
if sla_selected.close_time && self.close_time_in_min if sla_selected.close_time && self.close_time_in_min
self.close_time_diff_in_min = sla_selected.close_time - self.close_time_in_min self.close_time_diff_in_min = sla_selected.close_time - self.close_time_in_min
end end
if self.changed?
self.callback_loop = true return if !self.changed?
self.save
end self.callback_loop = true
self.save
end end
=begin =begin

View file

@ -168,9 +168,9 @@ translate strings in ruby context, e. g. for notifications
private private
def set_initial def set_initial
if !target_initial
self.target_initial = self.target return if target_initial
end self.target_initial = self.target
end end
def cache_clear def cache_clear
Cache.delete( 'Translation::' + self.locale.downcase ) Cache.delete( 'Translation::' + self.locale.downcase )

View file

@ -449,9 +449,10 @@ returns
end end
def check_email def check_email
if self.email
self.email = self.email.downcase return if !self.email
end
self.email = self.email.downcase
end end
def check_login def check_login
@ -469,16 +470,16 @@ returns
end end
# check if login already exists # check if login already exists
if self.login return if !self.login
self.login = self.login.downcase
check = true self.login = self.login.downcase
while check check = true
exists = User.where( login: self.login ).first while check
if exists && exists.id != self.id exists = User.where( login: self.login ).first
self.login = self.login + rand(999).to_s if exists && exists.id != self.id
else self.login = self.login + rand(999).to_s
check = false else
end check = false
end end
end end
end end
@ -499,10 +500,10 @@ returns
) )
# update user link # update user link
if avatar return if !avatar
self.update_column( :image, avatar.store_hash )
self.cache_delete self.update_column( :image, avatar.store_hash )
end self.cache_delete
end end
def avatar_destroy def avatar_destroy
@ -524,9 +525,9 @@ returns
end end
# crypt password if not already crypted # crypt password if not already crypted
if self.password && self.password !~ /^\{sha2\}/ return if !( self.password && self.password !~ /^\{sha2\}/ )
crypted = Digest::SHA2.hexdigest( self.password )
self.password = "{sha2}#{crypted}" crypted = Digest::SHA2.hexdigest( self.password )
end self.password = "{sha2}#{crypted}"
end end
end end

View file

@ -5,121 +5,122 @@ class UpdateOverviewAndTicketState < ActiveRecord::Migration
overview_role = Role.where( name: 'Agent' ).first overview_role = Role.where( name: 'Agent' ).first
add_column :ticket_states, :next_state_id, :integer, null: true add_column :ticket_states, :next_state_id, :integer, null: true
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
if overview_role
Overview.create_or_update(
name: 'My pending reached Tickets',
link: 'my_pending_reached',
prio: 1010,
role_id: overview_role.id,
condition: {
'tickets.state_id' => [3],
'tickets.owner_id' => 'current_user.id',
'tickets.pending_time' => { 'direction' => 'before', 'count' => 1, 'area' => 'minute' },
},
order: {
by: 'created_at',
direction: 'ASC',
},
view: {
d: [ 'title', 'customer', 'group', 'created_at' ],
s: [ 'title', 'customer', 'group', 'created_at' ],
m: [ 'number', 'title', 'customer', 'group', 'created_at' ],
view_mode_default: 's',
},
)
Ticket::State.create_or_update( id: 3, name: 'pending reminder', state_type_id: Ticket::StateType.where(name: 'pending reminder').first.id )
Ticket::State.create_or_update( id: 6, name: 'removed', state_type_id: Ticket::StateType.where(name: 'removed').first.id, active: false )
Ticket::State.create_or_update( id: 7, name: 'pending close', state_type_id: Ticket::StateType.where(name: 'pending action').first.id )
ObjectManager::Attribute.add( return true if !overview_role
object: 'Ticket',
name: 'state_id',
display: 'State',
data_type: 'select',
data_option: {
relation: 'TicketState',
nulloption: true,
multiple: false,
null: false,
default: 2,
translate: true,
filter: [1, 2, 3, 4, 7],
},
editable: false,
active: true,
screens: {
create_middle: {
Agent: {
null: false,
item_class: 'column',
},
Customer: {
item_class: 'column',
nulloption: false,
null: true,
filter: [1, 4],
default: 1,
},
},
edit: {
Agent: {
nulloption: false,
null: false,
filter: [2, 3, 4, 7],
},
Customer: {
nulloption: false,
null: true,
filter: [2, 4],
default: 2,
},
},
},
pending_migration: false,
position: 40,
created_by_id: 1,
updated_by_id: 1,
)
ObjectManager::Attribute.add( Overview.create_or_update(
object: 'Ticket', name: 'My pending reached Tickets',
name: 'pending_time', link: 'my_pending_reached',
display: 'Pending till', prio: 1010,
data_type: 'datetime', role_id: overview_role.id,
data_option: { condition: {
future: true, 'tickets.state_id' => [3],
past: false, 'tickets.owner_id' => 'current_user.id',
diff: 24, 'tickets.pending_time' => { 'direction' => 'before', 'count' => 1, 'area' => 'minute' },
null: true, },
translate: true, order: {
required_if: { by: 'created_at',
state_id: [3, 7] direction: 'ASC',
},
view: {
d: [ 'title', 'customer', 'group', 'created_at' ],
s: [ 'title', 'customer', 'group', 'created_at' ],
m: [ 'number', 'title', 'customer', 'group', 'created_at' ],
view_mode_default: 's',
},
)
Ticket::State.create_or_update( id: 3, name: 'pending reminder', state_type_id: Ticket::StateType.where(name: 'pending reminder').first.id )
Ticket::State.create_or_update( id: 6, name: 'removed', state_type_id: Ticket::StateType.where(name: 'removed').first.id, active: false )
Ticket::State.create_or_update( id: 7, name: 'pending close', state_type_id: Ticket::StateType.where(name: 'pending action').first.id )
ObjectManager::Attribute.add(
object: 'Ticket',
name: 'state_id',
display: 'State',
data_type: 'select',
data_option: {
relation: 'TicketState',
nulloption: true,
multiple: false,
null: false,
default: 2,
translate: true,
filter: [1, 2, 3, 4, 7],
},
editable: false,
active: true,
screens: {
create_middle: {
Agent: {
null: false,
item_class: 'column',
}, },
shown_if: { Customer: {
state_id: [3, 7] item_class: 'column',
nulloption: false,
null: true,
filter: [1, 4],
default: 1,
}, },
}, },
editable: false, edit: {
active: true, Agent: {
screens: { nulloption: false,
create_middle: { null: false,
'-all-' => { filter: [2, 3, 4, 7],
null: false,
item_class: 'column',
},
}, },
edit: { Customer: {
Agent: { nulloption: false,
null: false, null: true,
}, filter: [2, 4],
default: 2,
}, },
}, },
pending_migration: false, },
position: 41, pending_migration: false,
created_by_id: 1, position: 40,
updated_by_id: 1, created_by_id: 1,
) updated_by_id: 1,
end )
ObjectManager::Attribute.add(
object: 'Ticket',
name: 'pending_time',
display: 'Pending till',
data_type: 'datetime',
data_option: {
future: true,
past: false,
diff: 24,
null: true,
translate: true,
required_if: {
state_id: [3, 7]
},
shown_if: {
state_id: [3, 7]
},
},
editable: false,
active: true,
screens: {
create_middle: {
'-all-' => {
null: false,
item_class: 'column',
},
},
edit: {
Agent: {
null: false,
},
},
},
pending_migration: false,
position: 41,
created_by_id: 1,
updated_by_id: 1,
)
end end
def down def down

View file

@ -2,29 +2,30 @@ class UpdateOverview2 < ActiveRecord::Migration
def up def up
overview_role = Role.where( name: 'Agent' ).first overview_role = Role.where( name: 'Agent' ).first
if overview_role
UserInfo.current_user_id = 1 return true if !overview_role
Overview.create_or_update(
name: 'My assigned Tickets', UserInfo.current_user_id = 1
link: 'my_assigned', Overview.create_or_update(
prio: 1000, name: 'My assigned Tickets',
role_id: overview_role.id, link: 'my_assigned',
condition: { prio: 1000,
'tickets.state_id' => [ 1, 2, 3, 7 ], role_id: overview_role.id,
'tickets.owner_id' => 'current_user.id', condition: {
}, 'tickets.state_id' => [ 1, 2, 3, 7 ],
order: { 'tickets.owner_id' => 'current_user.id',
by: 'created_at', },
direction: 'ASC', order: {
}, by: 'created_at',
view: { direction: 'ASC',
d: [ 'title', 'customer', 'group', 'created_at' ], },
s: [ 'title', 'customer', 'group', 'created_at' ], view: {
m: [ 'number', 'title', 'customer', 'group', 'created_at' ], d: [ 'title', 'customer', 'group', 'created_at' ],
view_mode_default: 's', s: [ 'title', 'customer', 'group', 'created_at' ],
}, m: [ 'number', 'title', 'customer', 'group', 'created_at' ],
) view_mode_default: 's',
end },
)
end end
def down def down

View file

@ -1,224 +0,0 @@
#!/usr/bin/ruby -w
=begin
/***************************************************************************
* Copyright (C) 2008, Paul Lutus *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
=end
PVERSION = 'Version 2.9, 10/24/2008'
module RBeautify
# user-customizable values
RBeautify::TabStr = ' '
RBeautify::TabSize = 2
# indent regexp tests
IndentExp = [
/^module\b/,
/^class\b/,
/^if\b/,
/[A-z]\($/,
/(=\s*|^)until\b/,
/(=\s*|^)for\b/,
/^unless\b/,
/(=\s*|^)while\b/,
/(=\s*|^)begin\b/,
/(^| )case\b/,
/\bthen\b/,
/^rescue\b/,
/^def\b/,
/\bdo\b/,
/^else\b/,
/^elsif\b/,
/^ensure\b/,
/\bwhen\b/,
/\{[^\}]*$/,
/\[[^\]]*$/
]
# outdent regexp tests
OutdentExp = [
/^rescue\b/,
/^ensure\b/,
/^elsif\b/,
/^\)$/,
/^\);$/,
/^\)\./,
/^end\b/,
/^else\b/,
/\bwhen\b/,
/^[^\{]*\}/,
/^[^\[]*\]/
]
def RBeautify.rb_make_tab(tab)
return (tab < 0)?'':TabStr * TabSize * tab
end
def RBeautify.rb_add_line(line,tab)
line.strip!
line = rb_make_tab(tab) + line if line.length > 0
return line
end
def RBeautify.beautify_string(source, path = '')
comment_block = false
in_here_doc = false
here_doc_term = ''
program_end = false
multiLine_array = []
multiLine_str = ''
tab = 0
output = []
source.each do |line|
line.chomp!
if(!program_end)
# detect program end mark
if(line =~ /^__END__$/)
program_end = true
else
# combine continuing lines
if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
multiLine_array.push line
multiLine_str += line.sub(/^(.*)\\\s*$/,'\\1')
next
end
# add final line
if(multiLine_str.length > 0)
multiLine_array.push line
multiLine_str += line.sub(/^(.*)\\\s*$/,'\\1')
end
tline = ((multiLine_str.length > 0)?multiLine_str:line).strip
if(tline =~ /^=begin/)
comment_block = true
end
if(in_here_doc)
in_here_doc = false if tline =~ %r{\s*#{here_doc_term}\s*}
else # not in here_doc
if tline =~ %r{=\s*<<}
here_doc_term = tline.sub(%r{.*=\s*<<-?\s*([_|\w]+).*},'\\1')
in_here_doc = here_doc_term.size > 0
end
end
end
end
if(comment_block || program_end || in_here_doc)
# add the line unchanged
output << line
else
comment_line = (tline =~ /^#/)
if(!comment_line)
# throw out sequences that will
# only sow confusion
while tline.gsub!(/\{[^\{]*?\}/,'')
end
while tline.gsub!(/\[[^\[]*?\]/,'')
end
while tline.gsub!(/'.*?'/,'')
end
while tline.gsub!(/".*?"/,'')
end
while tline.gsub!(/\`.*?\`/,'')
end
while tline.gsub!(/\([^\(]*?\)/,'')
end
while tline.gsub!(/\/.*?\//,'')
end
while tline.gsub!(/%r(.).*?\1/,'')
end
# delete end-of-line comments
tline.sub!(/#[^\"]+$/,'')
# convert quotes
tline.gsub!(/\\\"/,"'")
OutdentExp.each do |re|
if(tline =~ re)
tab -= 1
break
end
end
end
if (multiLine_array.length > 0)
multiLine_array.each do |ml|
output << rb_add_line(ml,tab)
end
multiLine_array.clear
multiLine_str = ''
else
output << rb_add_line(line,tab)
end
if(!comment_line)
IndentExp.each do |re|
if(tline =~ re && !(tline =~ /\s+end\s*$/))
tab += 1
break
end
end
end
end
if(tline =~ /^=end/)
comment_block = false
end
end
error = (tab != 0)
STDERR.puts "Error: indent/outdent mismatch: #{tab}." if error
return output.join("\n") + "\n",error
end # beautify_string
def RBeautify.beautify_file(path)
error = false
if(path == '-') # stdin source
source = STDIN.read
dest,error = beautify_string(source,'stdin')
print dest
else # named file source
source = File.read(path)
dest,error = beautify_string(source,path)
if(source != dest)
# make a backup copy
#File.open(path + "~","w") { |f| f.write(source) }
# overwrite the original
File.open(path,'w') { |f| f.write(dest) }
end
end
return error
end # beautify_file
def RBeautify.main
error = false
if(!ARGV[0])
STDERR.puts "usage: Ruby filenames or \"-\" for stdin."
exit 0
end
ARGV.each do |path|
error = (beautify_file(path))?true:error
end
error = (error)?1:0
exit error
end # main
end # module RBeautify
# if launched as a standalone program, not loaded as a module
if __FILE__ == $0
RBeautify.main
end

View file

@ -1,82 +0,0 @@
#!/usr/bin/ruby
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
require 'tempfile'
require 'code_beauty_ruby.rb'
def checkForHeader(fileName)
foundHeader = false;
foundSheBang = false;
isCoffee = false;
header = "# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/\n"
if File.extname(fileName) == '.coffee'
isCoffee = true
end
# read file
file = File.open(fileName)
t_file = Tempfile.new('Temp')
allLines = IO.readlines(fileName)
# detect file type
if allLines[0] =~ /(^# Copyright)/ || allLines[1] =~ /(^# Copyright)/
foundHeader = true
elsif allLines[1] =~ /^=begin/ #assume external script
foundHeader = true
end
if allLines[0] =~ /(^#!\/)/
foundSheBang = true
end
file.each do |line|
# replace old header in script
if file.lineno == 1 && foundSheBang && foundHeader
t_file.puts header
# insert new header
elsif file.lineno == 1 && !foundSheBang && foundHeader
t_file.puts header
# insert new header
elsif file.lineno == 1 && !foundSheBang && !foundHeader
t_file.puts header
t_file.puts line + "\n"
# insert new header into script
elsif file.lineno == 1 && foundSheBang && !foundHeader
t_file.puts line
t_file.puts header + "\n"
# strip lines
else
t_file.puts line.rstrip
end
end
# rename file
t_file.rewind
t_file.close
FileUtils.cp(t_file.path, fileName)
t_file.unlink
# beautify ruby file
if !isCoffee
t_file = RBeautify.beautify_file(fileName)
end
end
#folder array
folder = ['app/assets/javascripts/app', 'app/controllers/', 'app/models/', 'app/helpers/', 'app/mailers/' ]
folder.each do |folder|
puts 'Working on folder' + folder.to_s
rbfiles = File.join("../#{folder}**", '*.{rb,coffee}')
d = Dir.glob(rbfiles)
d.each {|fileName|
puts "Working on #{fileName}"
#check if file header is present
checkForHeader(fileName)
}
end