prawn_report 1.9.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/app/controllers/ac_filter_defs_controller.rb +17 -0
- data/app/controllers/custom_generate_report_controller.rb +18 -0
- data/app/controllers/generate_report_controller.rb +24 -0
- data/app/controllers/report_templates_controller.rb +40 -0
- data/app/models/ac_filter.rb +15 -0
- data/app/models/ac_filter_def.rb +38 -0
- data/app/models/ac_filter_option.rb +5 -0
- data/app/models/report_template.rb +22 -0
- data/lib/ac_filters_utils.rb +187 -0
- data/lib/active_record_helpers.rb +218 -0
- data/lib/bands/band.rb +23 -0
- data/lib/bands/footer_band.rb +7 -0
- data/lib/bands/header_band.rb +9 -0
- data/lib/bands/summary_band.rb +7 -0
- data/lib/custom_report_controller.rb +48 -0
- data/lib/generators/prawn_report/install/install_generator.rb +16 -0
- data/lib/generators/prawn_report/install/templates/20120108210141_create_prawn_report_catalogs.rb +14 -0
- data/lib/generators/prawn_report/install/templates/20120124012653_add_filter_defs.rb +35 -0
- data/lib/generators/prawn_report/install/templates/20120209231821_add_filters_to_reports.rb +13 -0
- data/lib/generators/prawn_report/install/templates/20120222021437_add_order_to_filter_def.rb +9 -0
- data/lib/generators/prawn_report/install/templates/20120222025632_add_select_sql.rb +9 -0
- data/lib/generators/prawn_report/install/templates/20120222030526_create_ac_filter_joins.rb +12 -0
- data/lib/generators/prawn_report/install/templates/20120222122507_add_serialization_params_to_report.rb +9 -0
- data/lib/generators/prawn_report/install/templates/20120222134101_create_ac_filter_includes.rb +12 -0
- data/lib/generators/prawn_report/install/templates/20120222150029_change_includes_and_joins_to_hash.rb +21 -0
- data/lib/generators/prawn_report/install/templates/20120229134810_add_group_to_filter_def.rb +9 -0
- data/lib/generators/prawn_report/install/templates/20120229194434_add_system_criteria_to_ac_filter.rb +9 -0
- data/lib/generators/prawn_report/install/templates/20120301063031_change_select_sql.rb +11 -0
- data/lib/generators/prawn_report/install/templates/20120303104431_change_filled_criteria_unfilled_criteria.rb +15 -0
- data/lib/generators/prawn_report/install/templates/20120316162712_add_filled_criteria_to_options.rb +9 -0
- data/lib/generators/prawn_report/install/templates/20120323124446_add_fields_to_filter.rb +17 -0
- data/lib/generators/prawn_report/install/templates/20130122101313_add_sql_query_to_ac_filter_def.rb +5 -0
- data/lib/generators/prawn_report/install/templates/20131107172133_add_excluir_to_report_template.rb +9 -0
- data/lib/prawn_report.rb +21 -0
- data/lib/prawn_report/engine.rb +9 -0
- data/lib/prawn_report/version.rb +3 -0
- data/lib/prawn_report_seeds.rb +46 -0
- data/lib/report.rb +190 -0
- data/lib/report_helpers.rb +82 -0
- data/lib/report_info.rb +27 -0
- data/repo/bands/footers/footer_001.rb +20 -0
- data/repo/bands/headers/header_001.rb +61 -0
- data/repo/bands/headers/header_002.rb +31 -0
- data/repo/reports/column_group.rb +58 -0
- data/repo/reports/listing.rb +144 -0
- data/repo/reports/simple_listing.rb +130 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 242507f2c7f5d85e05fe01212b988cb15610b95d
|
4
|
+
data.tar.gz: 9476a352688d8adeb402050c9708475184ba751b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 511e79d5163022e0d3f592402cf6b642941f8583c6f63d3af3d9db77ef9a643e370e10e5000abc6ee3168672fd71322e283a5f07df4d2c72fe918034ee4e5da1
|
7
|
+
data.tar.gz: 9a95c4394ff843c53ae3af12365deccd932e9c3991e69303d6b6f6fc6b62faad605f17581ef8a4882caccbb7c289c81ab39e0f0cbd7b4dfc3fa480e13ebde67b
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class AcFilterDefsController < ApplicationController
|
2
|
+
|
3
|
+
unloadable
|
4
|
+
|
5
|
+
def index
|
6
|
+
@ac_filter_defs = AcFilterDef.find(:all)
|
7
|
+
|
8
|
+
respond_to do |format|
|
9
|
+
format.html # index.html.erb
|
10
|
+
format.xml { render :xml => @ac_filter_defs }
|
11
|
+
format.fxml { render :fxml => @ac_filter_defs.to_fxml(
|
12
|
+
{:include => {:ac_filters => {:include => { :ac_filter_options => {}}}}} )}
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "custom_report_controller"
|
2
|
+
|
3
|
+
class CustomGenerateReportController < ApplicationController
|
4
|
+
|
5
|
+
unloadable
|
6
|
+
|
7
|
+
include PrawnReportController
|
8
|
+
|
9
|
+
def get_system_criteria
|
10
|
+
{}
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_pr_suggested_filename
|
14
|
+
@report_template = ReportTemplate.find(params["report_template_id"])
|
15
|
+
@report_template.report_class.underscore + '_' + Date.today.to_s + '.pdf'
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class GenerateReportController < PrawnReport.parent_controller.constantize
|
2
|
+
|
3
|
+
unloadable
|
4
|
+
|
5
|
+
def get_pr_report_class
|
6
|
+
@report_template = ReportTemplate.find(params["report_template_id"])
|
7
|
+
Kernel.const_get(@report_template.report_class)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_pr_report_params; end
|
11
|
+
|
12
|
+
def get_pr_report_data
|
13
|
+
@report_template = ReportTemplate.find(params["report_template_id"])
|
14
|
+
mc=Kernel
|
15
|
+
@report_template.ac_filter_def.model_class.split('::').each {|c| mc=mc.const_get(c)}
|
16
|
+
mc.apply_ac_filter(parse_ac_filters(params), get_system_criteria)
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_pr_filters
|
20
|
+
@report_template = ReportTemplate.find(params["report_template_id"])
|
21
|
+
get_ac_filters_applied(params, @report_template.ac_filter_def)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class ReportTemplatesController < ApplicationController
|
2
|
+
|
3
|
+
unloadable
|
4
|
+
|
5
|
+
def index
|
6
|
+
conditions = []
|
7
|
+
conditions = ['report_type in (?)', parse_array(params['report_type'])] unless params['report_type'].blank?
|
8
|
+
|
9
|
+
@templates = ReportTemplate.find(:all,
|
10
|
+
:conditions => conditions)
|
11
|
+
|
12
|
+
respond_to do |format|
|
13
|
+
format.html # index.html.erb
|
14
|
+
format.xml { render :xml => @templates }
|
15
|
+
format.fxml do
|
16
|
+
render :fxml => @templates.to_fxml(
|
17
|
+
{ :include => {
|
18
|
+
:ac_filter_def => { :include => {
|
19
|
+
:ac_filters => { :include => :ac_filter_options}
|
20
|
+
}}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def parse_array(s_array)
|
33
|
+
retorno = []
|
34
|
+
s_array.split(',').each{|s|
|
35
|
+
retorno << s
|
36
|
+
}
|
37
|
+
retorno
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class AcFilter < ActiveRecord::Base
|
2
|
+
belongs_to :ac_filter_def
|
3
|
+
|
4
|
+
has_many :ac_filter_options, :dependent => :destroy
|
5
|
+
accepts_nested_attributes_for :ac_filter_options
|
6
|
+
|
7
|
+
def has_filled_criteria?
|
8
|
+
!self.filled_criteria.nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_unfilled_criteria?
|
12
|
+
!self.unfilled_criteria.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class AcFilterDef < ActiveRecord::Base
|
2
|
+
|
3
|
+
has_many :ac_filters, :dependent => :destroy
|
4
|
+
accepts_nested_attributes_for :ac_filters
|
5
|
+
|
6
|
+
def joins_param=(value)
|
7
|
+
write_marshal_attribute(:joins_param, value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def joins_param
|
11
|
+
read_marshal_attribute(:joins_param)
|
12
|
+
end
|
13
|
+
|
14
|
+
def include_param=(value)
|
15
|
+
write_marshal_attribute(:include_param, value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def include_param
|
19
|
+
read_marshal_attribute(:include_param)
|
20
|
+
end
|
21
|
+
|
22
|
+
def has_sql_query?
|
23
|
+
sql_query.to_s != ''
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def write_marshal_attribute(name, value)
|
29
|
+
write_attribute name, Marshal.dump(value)
|
30
|
+
end
|
31
|
+
|
32
|
+
def read_marshal_attribute(name)
|
33
|
+
value = read_attribute name
|
34
|
+
value.nil? ? nil : Marshal.load(value)
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
class ReportTemplate < ActiveRecord::Base
|
4
|
+
belongs_to :ac_filter_def
|
5
|
+
|
6
|
+
def serialization_params=(value)
|
7
|
+
if value.is_a? Hash
|
8
|
+
value = Marshal.dump(value)
|
9
|
+
end
|
10
|
+
write_attribute :serialization_params, value
|
11
|
+
end
|
12
|
+
|
13
|
+
def serialization_params
|
14
|
+
value = read_attribute :serialization_params
|
15
|
+
begin
|
16
|
+
value.nil? ? {} : Marshal.load(value)
|
17
|
+
rescue TypeError
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
def parse_ac_filters(params)
|
4
|
+
parsed_filters = {}
|
5
|
+
parsed_filters['filter_def_id'] = params['filter_def_id'].to_i
|
6
|
+
params.each_pair do |k,v|
|
7
|
+
md = /^ac_filter_(\d+)_(.+)/.match(k)
|
8
|
+
if md
|
9
|
+
filter_id = md[1]
|
10
|
+
filter_result_name = md[2].underscore
|
11
|
+
filter_value = v
|
12
|
+
parsed_filters[filter_id.to_i] = {} unless parsed_filters[filter_id.to_i]
|
13
|
+
parsed_filters[filter_id.to_i][filter_result_name] = filter_value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
parsed_filters
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_ac_filters_applied(params, ac_filter_def)
|
20
|
+
parsed_filter = parse_ac_filters(params)
|
21
|
+
r = []
|
22
|
+
ac_filter_def.ac_filters.each do |f|
|
23
|
+
if f.query_user? && (parsed_filter[f.id]['is_filled'] == 'true')
|
24
|
+
pf = parsed_filter[f.id]
|
25
|
+
val = ''
|
26
|
+
if ['text', 'options'].include? f.data_type
|
27
|
+
val = pf['filter_value']
|
28
|
+
elsif ['period', 'timezone_period'].include?(f.data_type)
|
29
|
+
if (!pf['from_date'].empty?) && (!pf['to_date'].empty?)
|
30
|
+
val = "#{Date.parse(pf['from_date']).strftime('%d/%m/%Y')} até #{Date.parse(pf['to_date']).strftime('%d/%m/%Y')}"
|
31
|
+
elsif !pf['from_date'].empty?
|
32
|
+
val = "Desde #{Date.parse(pf['from_date']).strftime('%d/%m/%Y')}"
|
33
|
+
elsif
|
34
|
+
val = "Até #{Date.parse(pf['to_date']).strftime('%d/%m/%Y')}"
|
35
|
+
end
|
36
|
+
elsif f.data_type == 'autocomplete'
|
37
|
+
val = Kernel.const_get(f.target_model).find(pf['filter_value']).send(f.target_field)
|
38
|
+
end
|
39
|
+
r << [f.label, val]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
r
|
43
|
+
end
|
44
|
+
|
45
|
+
def is_filled?(parsed_params, filter)
|
46
|
+
!parsed_params[filter.id].nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
def fill_params(f, fillings)
|
50
|
+
if ['period', 'timezone_period'].include?(f.data_type)
|
51
|
+
[fillings['from_date'], fillings['to_date']]
|
52
|
+
elsif f.data_type == 'text'
|
53
|
+
'%' + fillings['filter_value'] + '%'
|
54
|
+
elsif f.data_type == 'options'
|
55
|
+
fillings['filter_value']
|
56
|
+
else
|
57
|
+
fillings['filter_value']
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse_conditions(parsed_filter, system_params)
|
62
|
+
system_params.symbolize_keys!
|
63
|
+
filter_def = AcFilterDef.find(parsed_filter['filter_def_id'])
|
64
|
+
conditions = []
|
65
|
+
conditions[0] = ['1=1']
|
66
|
+
|
67
|
+
filter_def.ac_filters.each do |f|
|
68
|
+
if f.query_user?
|
69
|
+
parse_condition(conditions, parsed_filter, f)
|
70
|
+
elsif f.system_criteria.to_s != ''
|
71
|
+
conditions[0] << f.filled_criteria
|
72
|
+
conditions << system_params[f.system_criteria.to_sym]
|
73
|
+
else
|
74
|
+
conditions[0] << f.filled_criteria
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
conditions[0] = conditions[0].join(' and ')
|
79
|
+
conditions
|
80
|
+
end
|
81
|
+
|
82
|
+
def parse_condition(conditions, parsed_filter, filter)
|
83
|
+
filled = parsed_filter[filter.id]['is_filled'] == 'true'
|
84
|
+
|
85
|
+
if filter.data_type == 'checkbox'
|
86
|
+
fill_with_checkbox(conditions, filled, parsed_filter, filter)
|
87
|
+
elsif filter.data_type == 'options'
|
88
|
+
fill_with_options(conditions, filled, parsed_filter, filter)
|
89
|
+
elsif filter.data_type == 'period'
|
90
|
+
fill_with_period(conditions, filled, parsed_filter, filter)
|
91
|
+
elsif filter.data_type == 'timezone_period'
|
92
|
+
fill_with_timezone_period(conditions, filled, parsed_filter, filter)
|
93
|
+
else
|
94
|
+
fill_with_others(conditions, filled, parsed_filter, filter)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def fill_with_checkbox(conditions, filled, parsed_filter, filter)
|
99
|
+
if filled && filter.has_filled_criteria?
|
100
|
+
conditions[0] << filter.filled_criteria
|
101
|
+
elsif filter.has_unfilled_criteria?
|
102
|
+
conditions[0] << filter.unfilled_criteria
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def fill_with_options(conditions, filled, parsed_filter, filter)
|
107
|
+
if !filled && filter.has_unfilled_criteria?
|
108
|
+
conditions[0] << c.unfilled_criteria
|
109
|
+
elsif filled
|
110
|
+
fo = AcFilterOption.find_by_ac_filter_id_and_value(filter.id, parsed_filter[filter.id]['filter_value'])
|
111
|
+
if fo && fo.filled_criteria.to_s != ''
|
112
|
+
conditions[0] << fo.filled_criteria
|
113
|
+
conditions << fill_params(filter, parsed_filter[filter.id])
|
114
|
+
elsif filter.has_filled_criteria?
|
115
|
+
conditions[0] << filter.filled_criteria
|
116
|
+
conditions << fill_params(filter, parsed_filter[filter.id])
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def fill_with_period(conditions, filled, parsed_filter, filter)
|
122
|
+
if !filled && filter.has_unfilled_criteria?
|
123
|
+
conditions[0] << c.unfilled_criteria
|
124
|
+
elsif filled
|
125
|
+
fp = fill_params(filter, parsed_filter[filter.id])
|
126
|
+
if fp[0].to_s != ''
|
127
|
+
conditions[0] << filter.filled_criteria_from
|
128
|
+
conditions << fp[0]
|
129
|
+
end
|
130
|
+
if fp[1].to_s != ''
|
131
|
+
conditions[0] << filter.filled_criteria_to
|
132
|
+
conditions << fp[1]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def fill_with_timezone_period(conditions, filled, parsed_filter, filter)
|
138
|
+
if !filled && filter.has_unfilled_criteria?
|
139
|
+
conditions[0] << c.unfilled_criteria
|
140
|
+
elsif filled
|
141
|
+
fp = fill_params(filter, parsed_filter[filter.id])
|
142
|
+
if fp[0].to_s != ''
|
143
|
+
conditions[0] << filter.filled_criteria_from
|
144
|
+
conditions << Time.zone.parse(fp[0])
|
145
|
+
end
|
146
|
+
if fp[1].to_s != ''
|
147
|
+
conditions[0] << filter.filled_criteria_to
|
148
|
+
conditions << Time.zone.parse(fp[1])+1.day
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def fill_with_others(conditions, filled, parsed_filter, filter)
|
154
|
+
if filled && filter.has_filled_criteria?
|
155
|
+
fp = fill_params(filter, parsed_filter[filter.id])
|
156
|
+
conditions[0] << filter.filled_criteria
|
157
|
+
conditions << fp
|
158
|
+
elsif filter.has_unfilled_criteria?
|
159
|
+
conditions[0] << filter.unfilled_criteria
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def get_param_by_label(params, label)
|
164
|
+
filter = AcFilter.find(:first, :conditions => ['ac_filter_def_id = ? and label = ?',params['filter_def_id'], label])
|
165
|
+
'ac_filter_'+filter.id.to_s
|
166
|
+
end
|
167
|
+
|
168
|
+
module AcFilters
|
169
|
+
def apply_ac_filter(parsed_filter, system_params)
|
170
|
+
conditions = parse_conditions(parsed_filter, system_params)
|
171
|
+
find_params = {:conditions => conditions}
|
172
|
+
filter_def = AcFilterDef.find(parsed_filter['filter_def_id'])
|
173
|
+
if filter_def.has_sql_query?
|
174
|
+
sql_to_execute = ActiveRecord::Base.send(:sanitize_sql_array, conditions)
|
175
|
+
r = []
|
176
|
+
ActiveRecord::Base.connection.instance_exec(sql_to_execute).each(as :hash) {|i| r << i}
|
177
|
+
else
|
178
|
+
find_params[:select] = filter_def.select_sql.to_s unless filter_def.select_sql.nil?
|
179
|
+
find_params[:order] = filter_def.order_sql.to_s unless filter_def.order_sql.nil?
|
180
|
+
find_params[:joins] = filter_def.joins_param unless filter_def.joins_param.nil?
|
181
|
+
find_params[:include] = filter_def.include_param unless filter_def.include_param.nil?
|
182
|
+
find_params[:group] = filter_def.group_param unless filter_def.group_param.nil?
|
183
|
+
r = find(:all, find_params)
|
184
|
+
end
|
185
|
+
r
|
186
|
+
end
|
187
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
class ActiveRecordYAMLSerializer
|
6
|
+
|
7
|
+
def initialize(obj, params)
|
8
|
+
@obj = obj
|
9
|
+
@params = params.symbolize_keys!
|
10
|
+
@contents = ''
|
11
|
+
@indent_level = 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialize
|
15
|
+
@contents = ''
|
16
|
+
@contents += serialize_root_values(@params)
|
17
|
+
|
18
|
+
if @obj.is_a? ActiveRecord::Base
|
19
|
+
@contents += serialize_record(@obj, false, @params) #asnotarray
|
20
|
+
elsif @obj.is_a? Array
|
21
|
+
@contents += "items:\n"
|
22
|
+
@obj.each do |item|
|
23
|
+
reset_indent
|
24
|
+
indent
|
25
|
+
@contents += serialize_record(item, true, @params) #asarray
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_yaml
|
33
|
+
YAML::load( @contents )
|
34
|
+
end
|
35
|
+
|
36
|
+
def save_as(file_name)
|
37
|
+
File.open(file_name, 'w') do |f|
|
38
|
+
f.write @contents
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def indent
|
45
|
+
@indent_level += 1
|
46
|
+
end
|
47
|
+
|
48
|
+
def unindent
|
49
|
+
@indent_level -= 1
|
50
|
+
end
|
51
|
+
|
52
|
+
def reset_indent(level = 0)
|
53
|
+
@indent_level = level
|
54
|
+
end
|
55
|
+
|
56
|
+
def serialize_root_values(params)
|
57
|
+
r = ''
|
58
|
+
params[:root_values] ||= {}
|
59
|
+
params[:root_values].each_pair { |k,v| r += serialize_key_value(k,v) }
|
60
|
+
r
|
61
|
+
end
|
62
|
+
|
63
|
+
def serialize_record(rec, as_array, params = {})
|
64
|
+
r = ''
|
65
|
+
first_line = true
|
66
|
+
to_serialize = (rec.is_a? ActiveRecord::Base) ? rec.attributes : rec
|
67
|
+
|
68
|
+
to_serialize.each_pair do |k, v|
|
69
|
+
r += render_indent_first_line(as_array) if first_line
|
70
|
+
r += ' ' * @indent_level unless first_line
|
71
|
+
r += serialize_key_value(k,v)
|
72
|
+
first_line = false
|
73
|
+
end
|
74
|
+
|
75
|
+
if rec.is_a? ActiveRecord::Base
|
76
|
+
r += serialize_belongs_tos(rec, first_line, params)
|
77
|
+
r += serialize_has_manys(rec, first_line, params)
|
78
|
+
r += serialize_methods(rec, first_line, params)
|
79
|
+
end
|
80
|
+
|
81
|
+
r
|
82
|
+
end
|
83
|
+
|
84
|
+
def serialize_key_value(k, v)
|
85
|
+
k.to_s + ': ' + serialize_value(v)
|
86
|
+
end
|
87
|
+
|
88
|
+
def serialize_value(v)
|
89
|
+
if v.is_a? String
|
90
|
+
serialize_string(v)
|
91
|
+
else
|
92
|
+
v.to_s + "\n"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def serialize_string(s)
|
97
|
+
s.strip!
|
98
|
+
r = ''
|
99
|
+
if s.lines.count > 1
|
100
|
+
r += "|-\n"
|
101
|
+
s.lines.each do |l|
|
102
|
+
r += ' ' * (@indent_level + 1)
|
103
|
+
r += l
|
104
|
+
end
|
105
|
+
else
|
106
|
+
r += '"' + s.to_s.gsub('"', '\"') + '"'
|
107
|
+
end
|
108
|
+
r + "\n"
|
109
|
+
end
|
110
|
+
|
111
|
+
def render_indent_first_line(as_array)
|
112
|
+
r = ''
|
113
|
+
if @indent_level != 0
|
114
|
+
r = ' ' * (@indent_level - 1)
|
115
|
+
if (as_array)
|
116
|
+
r += '- '
|
117
|
+
else
|
118
|
+
r += ' '
|
119
|
+
end
|
120
|
+
end
|
121
|
+
r
|
122
|
+
end
|
123
|
+
|
124
|
+
def render_indent(first_line)
|
125
|
+
r = ''
|
126
|
+
#r += render_indent_first_line(@indent_level) if first_line
|
127
|
+
r += ' ' * @indent_level unless first_line
|
128
|
+
end
|
129
|
+
|
130
|
+
def serialize_belongs_tos(rec, first_line, params)
|
131
|
+
r = ''
|
132
|
+
if params[:include_all_belongs_to] == true
|
133
|
+
rec.class.reflect_on_all_associations(:belongs_to).collect do |a|
|
134
|
+
r += render_indent(first_line)
|
135
|
+
r += a.name.to_s + ":\n"
|
136
|
+
indent
|
137
|
+
r += serialize_record(rec.send(a.name), false,
|
138
|
+
{:include_all_belongs_to => params[:include_all_belongs_to]}) if rec.send(a.name)
|
139
|
+
unindent
|
140
|
+
end
|
141
|
+
else
|
142
|
+
params[:included_belongs_to] ||= {}
|
143
|
+
params[:included_belongs_to].each_pair do |k,v|
|
144
|
+
v.symbolize_keys!
|
145
|
+
serialization_params = v
|
146
|
+
type = v[:type].to_sym || :fields
|
147
|
+
master_rec = rec.send(k)
|
148
|
+
if type == :fields
|
149
|
+
v[:fields].each do |f|
|
150
|
+
r += render_indent(first_line)
|
151
|
+
val = master_rec ? master_rec.send(f) : nil
|
152
|
+
r += serialize_key_value(k.to_s + '_' + f.to_s, val)
|
153
|
+
first_line = false
|
154
|
+
end
|
155
|
+
elsif type == :record
|
156
|
+
r += render_indent(first_line)
|
157
|
+
r += k.to_s + ":\n"
|
158
|
+
indent
|
159
|
+
r += serialize_record(master_rec, false, v[:params]) if master_rec #as_not_array
|
160
|
+
unindent
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
r
|
165
|
+
end
|
166
|
+
|
167
|
+
def serialize_has_manys(rec, first_line, params)
|
168
|
+
r = ''
|
169
|
+
params[:included_has_many] ||= {}
|
170
|
+
params[:included_has_many].each_pair do |k,v|
|
171
|
+
r += render_indent(first_line)
|
172
|
+
r += k.to_s + ":"
|
173
|
+
r += serialize_has_many(rec.send(k), v)
|
174
|
+
first_line = false
|
175
|
+
end
|
176
|
+
r
|
177
|
+
end
|
178
|
+
|
179
|
+
def serialize_methods(rec, first_line, params)
|
180
|
+
r = ''
|
181
|
+
params[:included_methods] ||= {}
|
182
|
+
params[:included_methods].each do |v|
|
183
|
+
r += render_indent(first_line)
|
184
|
+
val = rec.send(v)
|
185
|
+
r += serialize_key_value(v, val)
|
186
|
+
first_line = false
|
187
|
+
end
|
188
|
+
r
|
189
|
+
end
|
190
|
+
|
191
|
+
def serialize_has_many(hm, params)
|
192
|
+
original_indent = @indent_level
|
193
|
+
r = ''
|
194
|
+
if hm.count == 0
|
195
|
+
r += " []\n"
|
196
|
+
else
|
197
|
+
r += "\n"
|
198
|
+
hm.each do |det|
|
199
|
+
reset_indent(original_indent)
|
200
|
+
indent
|
201
|
+
r += serialize_record(det, true, params || {}) #asarray
|
202
|
+
end
|
203
|
+
end
|
204
|
+
r
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
module PrawnReportActiveRecord
|
210
|
+
|
211
|
+
def pr_serialize(params = {})
|
212
|
+
a = ActiveRecordYAMLSerializer.new(self, params)
|
213
|
+
a.serialize
|
214
|
+
a
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|