Moved iCal methods to own modules in lib.

This commit is contained in:
Thorsten Eckel 2015-06-22 17:15:16 +02:00
parent f345f9f47c
commit f868847826
3 changed files with 161 additions and 130 deletions

View file

@ -12,13 +12,20 @@ class IcalTicketsController < ApplicationController
# @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 200 [String] iCal file ready to import in calendar applications.
# @response_message 401 Permission denied. # @response_message 401 Permission denied.
def all def all
new_open_events_data = new_open_events_data_get new_open_events_data = ICal::Ticket.new_open(current_user)
pending_events_data = pending_events_data_get pending_events_data = ICal::Ticket.pending(current_user)
escalation_events_data = escalation_events_data_get escalation_events_data = ICal::Ticket.escalation(current_user)
events_data = new_open_events_data + pending_events_data + escalation_events_data events_data = new_open_events_data + pending_events_data + escalation_events_data
events_data_to_ical( events_data ) ical = ICal.to_ical( events_data )
send_data(
ical,
filename: 'zammad_tickets.ical',
type: 'text/plain',
disposition: 'inline'
)
end end
# @path [GET] /ical/tickets_new_open # @path [GET] /ical/tickets_new_open
@ -28,9 +35,16 @@ class IcalTicketsController < ApplicationController
# @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 200 [String] iCal file ready to import in calendar applications.
# @response_message 401 Permission denied. # @response_message 401 Permission denied.
def new_open def new_open
events_data = new_open_events_data_get events_data = ICal::Ticket.new_open(current_user)
events_data_to_ical( events_data ) ical = ICal.to_ical( events_data )
send_data(
ical,
filename: 'zammad_tickets_new_open.ical',
type: 'text/plain',
disposition: 'inline'
)
end end
# @path [GET] /ical/tickets_pending # @path [GET] /ical/tickets_pending
@ -40,9 +54,16 @@ class IcalTicketsController < ApplicationController
# @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 200 [String] iCal file ready to import in calendar applications.
# @response_message 401 Permission denied. # @response_message 401 Permission denied.
def pending def pending
events_data = pending_events_data_get events_data = ICal::Ticket.pending(current_user)
events_data_to_ical( events_data ) ical = ICal.to_ical( events_data )
send_data(
ical,
filename: 'zammad_tickets_pending.ical',
type: 'text/plain',
disposition: 'inline'
)
end end
# @path [GET] /ical/ticket_escalation # @path [GET] /ical/ticket_escalation
@ -52,131 +73,13 @@ class IcalTicketsController < ApplicationController
# @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 200 [String] iCal file ready to import in calendar applications.
# @response_message 401 Permission denied. # @response_message 401 Permission denied.
def escalation def escalation
events_data = escalation_events_data_get events_data = ICal::Ticket.escalation(current_user)
events_data_to_ical( events_data ) ical = ICal.to_ical( events_data )
end
private
def new_open_events_data_get
condition = {
'tickets.owner_id' => current_user.id,
'tickets.state_id' => Ticket::State.where(
state_type_id: Ticket::StateType.where(
name: %w(new open),
),
),
}
tickets = Ticket.search(
current_user: current_user,
condition: condition,
)
events_data = []
tickets.each do |ticket|
event_data = {}
event_data[:dtstart] = Icalendar::Values::Date.new( Time.zone.today )
event_data[:dtend] = Icalendar::Values::Date.new( Time.zone.today )
event_data[:summary] = "#{ ticket.state.name } ticket: '#{ ticket.title }'"
event_data[:description] = "T##{ ticket.number }"
events_data.push event_data
end
events_data
end
def pending_events_data_get
condition = {
'tickets.owner_id' => current_user.id,
'tickets.state_id' => Ticket::State.where(
state_type_id: Ticket::StateType.where(
name: [
'pending reminder',
'pending action',
],
),
),
}
tickets = Ticket.search(
current_user: current_user,
condition: condition,
)
events_data = []
tickets.each do |ticket|
event_data = {}
# rubocop:disable Rails/TimeZone
event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.pending_time )
event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.pending_time )
# rubocop:enable Rails/TimeZone
event_data[:summary] = "#{ ticket.state.name } ticket: '#{ ticket.title }'"
event_data[:description] = "T##{ ticket.number }"
events_data.push event_data
end
events_data
end
def escalation_events_data_get
condition = [
'tickets.escalation_time IS NOT NULL',
'tickets.owner_id = ?', current_user.id
]
tickets = Ticket.search(
current_user: current_user,
condition: condition,
)
events_data = []
tickets.each do |ticket|
event_data = {}
# rubocop:disable Rails/TimeZone
event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.escalation_time )
event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.escalation_time )
# rubocop:enable Rails/TimeZone
event_data[:summary] = "ticket escalation: '#{ ticket.title }'"
event_data[:description] = "T##{ ticket.number }"
events_data.push event_data
end
events_data
end
def events_data_to_ical(events_data)
cal = Icalendar::Calendar.new
events_data.each do |event_data|
cal.event do |e|
e.dtstart = event_data[:dtstart]
e.dtend = event_data[:dtend]
e.summary = event_data[:summary]
e.description = event_data[:description]
e.ip_class = 'PRIVATE'
end
end
send_data( send_data(
cal.to_ical, ical,
filename: 'zammad.ical', filename: 'zammad_tickets_escalation.ical',
type: 'text/plain', type: 'text/plain',
disposition: 'inline' disposition: 'inline'
) )

24
lib/i_cal.rb Normal file
View file

@ -0,0 +1,24 @@
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
module ICal
def self.to_ical(events_data)
cal = Icalendar::Calendar.new
events_data.each do |event_data|
cal.event do |e|
e.dtstart = event_data[:dtstart]
e.dtend = event_data[:dtend]
e.summary = event_data[:summary]
e.description = event_data[:description]
e.ip_class = 'PRIVATE'
end
end
cal.to_ical
end
end

104
lib/i_cal/ticket.rb Normal file
View file

@ -0,0 +1,104 @@
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
module ICal::Ticket
def self.new_open(user)
condition = {
'tickets.owner_id' => user.id,
'tickets.state_id' => Ticket::State.where(
state_type_id: Ticket::StateType.where(
name: %w(new open),
),
),
}
tickets = Ticket.search(
current_user: user,
condition: condition,
)
events_data = []
tickets.each do |ticket|
event_data = {}
event_data[:dtstart] = Icalendar::Values::Date.new( Time.zone.today )
event_data[:dtend] = Icalendar::Values::Date.new( Time.zone.today )
event_data[:summary] = "#{ticket.state.name} ticket: '#{ticket.title}'"
event_data[:description] = "T##{ticket.number}"
events_data.push event_data
end
events_data
end
def self.pending(user)
condition = {
'tickets.owner_id' => user.id,
'tickets.state_id' => Ticket::State.where(
state_type_id: Ticket::StateType.where(
name: [
'pending reminder',
'pending action',
],
),
),
}
tickets = Ticket.search(
current_user: user,
condition: condition,
)
events_data = []
tickets.each do |ticket|
event_data = {}
# rubocop:disable Rails/TimeZone
event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.pending_time )
event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.pending_time )
# rubocop:enable Rails/TimeZone
event_data[:summary] = "#{ticket.state.name} ticket: '#{ticket.title}'"
event_data[:description] = "T##{ticket.number}"
events_data.push event_data
end
events_data
end
def self.escalation(user)
condition = [
'tickets.escalation_time IS NOT NULL',
'tickets.owner_id = ?', user.id
]
tickets = Ticket.search(
current_user: user,
condition: condition,
)
events_data = []
tickets.each do |ticket|
event_data = {}
# rubocop:disable Rails/TimeZone
event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.escalation_time )
event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.escalation_time )
# rubocop:enable Rails/TimeZone
event_data[:summary] = "ticket escalation: '#{ticket.title}'"
event_data[:description] = "T##{ticket.number}"
events_data.push event_data
end
events_data
end
end