Module: Rtt::ReportGenerator

Included in:
Rtt
Defined in:
lib/rtt/report_generator.rb

Constant Summary collapse

DEFAULT_FILENAME =
'rtt_report'
FORMATS_ACCEPTED =
[ :csv, :pdf ]
REPORT_FIELDS =
%w(Client Project Name Date Rate Duration)
FIXED_FIELDS =
%w(Client Project)
REPORT_FIELD_OUTPUT =
{
  'Client' => Proc.new { |task| (task.client.name) if task.client.present? },
  'Project' => Proc.new { |task| (task.project.name) if task.project.present? },
  'Name' => Proc.new { |task| task.name },
  'Date' => Proc.new { |task| task.date.strftime('%m-%d-%y') if task.date.present? },
  'Rate' => Proc.new { |task| task.rate },
  'Duration' => Proc.new { |task| task.duration }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/rtt/report_generator.rb', line 5

def data
  @data
end

#different_fixedObject

Returns the value of attribute different_fixed.



5
6
7
# File 'lib/rtt/report_generator.rb', line 5

def different_fixed
  @different_fixed
end

Instance Method Details

#column_widths(fixed_fields) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rtt/report_generator.rb', line 20

def column_widths(fixed_fields)
  if fixed_fields.same(['Date', 'Client', 'Project'])
      { 0 => 430, 1 => 50, 2 => 60 }
  elsif fixed_fields.same(['Client', 'Project']) 
      { 0 => 360, 1 => 60, 2 => 60, 3 => 60 }
  elsif fixed_fields.same(['Client', 'Date']) || fixed_fields.same(['Project', 'Date'])
      { 0 => 60, 1 => 360, 2 => 60, 3 => 60 }
  elsif fixed_fields.same(['Project']) || fixed_fields.same(['Client'])
      { 0 => 80, 1 => 290, 2 => 60, 3 => 50, 4 => 60 }
  else
      { 0 => 80, 1 => 80, 2 => 210, 3 => 60, 4 => 50, 5 => 60 }
  end
end

#custom_user_is_defined?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rtt/report_generator.rb', line 34

def custom_user_is_defined?
  current_user.present? && current_user.nickname != User::DEFAULT_NICK
end

#fill_user_information(pdf) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rtt/report_generator.rb', line 38

def fill_user_information(pdf)
  pdf.cell [330, 790],
      :text => current_user.full_name_and_nickname,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
  pdf.cell [330, 770],
      :text => current_user.company,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
  pdf.cell [330, 750],
      :text => current_user.location,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
  pdf.cell [330, 730],
      :text => current_user.address,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
  pdf.cell [330, 710],
      :text => current_user.phone,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
  pdf.cell [330, 690],
      :text => current_user.email,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
  pdf.cell [330, 670],
      :text => current_user.site,
      :width => 225, :padding => 10, :border_width => 0, :align => :right
end

#fixed_fields_for_current_dataObject



62
63
64
65
66
67
68
69
70
# File 'lib/rtt/report_generator.rb', line 62

def fixed_fields_for_current_data
  @fixed_fields_for_current_data ||= begin
    calculate_fixed_fields_based_on_data
    @different_fixed.keys.reject { |key| @different_fixed[key].length > 1 }.inject(@data[:fixed_fields].keys) do |keys, new_key_based_on_data|
      keys<<(new_key_based_on_data) unless keys.include?(new_key_based_on_data)
      keys
    end
  end
end

#fixed_value(field) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rtt/report_generator.rb', line 72

def fixed_value(field)
  if @data[:fixed_fields].include? field
      @data[:fixed_fields][field]
  else
      @different_fixed[field].first
  end
end

#full_path(output_path = nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/rtt/report_generator.rb', line 80

def full_path(output_path = nil)
  entered_filename = output_path || DEFAULT_FILENAME
  filename, directory, extension = File.basename(entered_filename), File.dirname(entered_filename), File.extname(entered_filename)
  path = directory.present? && directory != '.' && File.exists?(directory) ? directory : ENV['HOME']
  ext = extension.present? ? '' : '.pdf'
  "#{File.join(path, filename)}#{ext}"
end

#has_default_value?(field) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/rtt/report_generator.rb', line 88

def has_default_value?(field)
  task = self.data[:rows].first
  return true if task.nil?
  return false if !(field == 'Client' || field == 'Project' || field == 'User')
  (REPORT_FIELD_OUTPUT[field].call(task) if task.present?) == eval("#{field}::DEFAULT_NAME")
end

#report(options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rtt/report_generator.rb', line 97

def report options = {}
  raise 'Argument must be a valid Hash. Checkout: rtt usage' unless options.is_a?(Hash) || options.keys.empty?
  @different_fixed ||= FIXED_FIELDS.inject({}) { |result, key| result[key] = []; result }
  extension = options.keys.select { |key| FORMATS_ACCEPTED.include?(key) }.first
  path = options.delete(extension)
  fixed_fields = extract_fixed_fields(options)
  fixed_fields_and_values = fixed_fields.inject({}) { |hash, key| hash[key] = options[key.downcase.to_sym]; hash }
  @data = { :fixed_fields => fixed_fields_and_values, :rows => query(options).order('date desc') }
  filename_path = full_path(path)
  case extension
    when :pdf
      report_to_pdf filename_path
    when :csv
      raise 'CSV format report not implemented yet'
      report_to_csv path
    else
      raise 'Format not supported. Only csv and pdf are available for the moment.'
  end

end