Class: ReportsKit::ReportBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers
Defined in:
lib/reports_kit/report_builder.rb

Constant Summary collapse

ACTION_KEYS_METHODS =
{
  'export_csv' => :export_csv_button,
  'export_xls' => :export_xls_button
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report_params:, context_params: {}, actions: %w(export_csv export_xls),, js_report_class: 'Report', properties:, view_context:, block: nil) ⇒ ReportBuilder

Returns a new instance of ReportBuilder.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/reports_kit/report_builder.rb', line 12

def initialize(report_params:, context_params: {}, actions: %w(export_csv export_xls), js_report_class: 'Report', properties:, view_context:, block: nil)
  self.report_params = report_params.is_a?(String) ? { key: report_params } : report_params
  self.context_params = context_params
  self.additional_params = { context_params: context_params, report_params: self.report_params }
  self.actions = actions
  self.js_report_class = js_report_class
  self.view_context = view_context
  self.block = block
  self.properties = properties#view_context.instance_eval(&ReportsKit.configuration.properties_method).deep_symbolize_keys
  self.form_builder = ReportsKit::FormBuilder.new(properties, additional_params: additional_params)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def actions
  @actions
end

#additional_paramsObject

Returns the value of attribute additional_params.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def additional_params
  @additional_params
end

#blockObject

Returns the value of attribute block.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def block
  @block
end

#context_paramsObject

Returns the value of attribute context_params.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def context_params
  @context_params
end

#form_builderObject

Returns the value of attribute form_builder.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def form_builder
  @form_builder
end

#js_report_classObject

Returns the value of attribute js_report_class.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def js_report_class
  @js_report_class
end

#propertiesObject

Returns the value of attribute properties.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def properties
  @properties
end

#report_paramsObject

Returns the value of attribute report_params.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def report_params
  @report_params
end

#view_contextObject

Returns the value of attribute view_context.



5
6
7
# File 'lib/reports_kit/report_builder.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#export_button(text, format, options, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reports_kit/report_builder.rb', line 50

def export_button(text, format, options, &block)
  data = {
    role: 'reports_kit_export_button',
    path: view_context.reports_kit.reports_kit_reports_path({ format: format }.merge(additional_params))
  }
  options = { class: 'btn btn-primary', data: data }.merge(options)
  if block_given?
    view_context.link_to('#', options, &block)
  else
    view_context.link_to(text, '#', options)
  end
end

#export_csv_button(text = 'Download CSV', options = {}, &block) ⇒ Object



42
43
44
# File 'lib/reports_kit/report_builder.rb', line 42

def export_csv_button(text='Download CSV', options = {}, &block)
  export_button(text, 'csv', options, &block)
end

#export_xls_button(text = 'Download Excel', options = {}, &block) ⇒ Object



46
47
48
# File 'lib/reports_kit/report_builder.rb', line 46

def export_xls_button(text='Download Excel', options = {}, &block)
  export_button(text, 'xls', options, &block)
end

#form(&block) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
# File 'lib/reports_kit/report_builder.rb', line 35

def form(&block)
  raise ArgumentError.new('No block given for ReportBuilder#form') unless block
  view_context.form_tag(reports_data_path, method: 'get', class: 'reports_kit_report_form') do
    view_context.capture(form_builder, &block)
  end
end

#renderObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/reports_kit/report_builder.rb', line 24

def render
  data = { properties: properties.slice(:format), path: reports_data_path, report_class: js_report_class }
  view_context. :div, nil, class: 'reports_kit_report form-inline', data: data do
    elements = []
    elements << view_context.capture(self, &block) if block
    elements << view_context.(:div, nil, class: 'reports_kit_visualization')
    elements << action_elements_container
    elements.compact.join.html_safe
  end
end