Class: Spree::Report::Result

Inherits:
Object
  • Object
show all
Defined in:
app/reports/spree/report/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Result

Returns a new instance of Result.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
# File 'app/reports/spree/report/result.rb', line 7

def initialize
  yield self
  build_report_observations
end

Instance Attribute Details

#end_dateObject

Returns the value of attribute end_date.



4
5
6
# File 'app/reports/spree/report/result.rb', line 4

def end_date
  @end_date
end

#observationsObject (readonly)

Returns the value of attribute observations.



5
6
7
# File 'app/reports/spree/report/result.rb', line 5

def observations
  @observations
end

#reportObject

Returns the value of attribute report.



4
5
6
# File 'app/reports/spree/report/result.rb', line 4

def report
  @report
end

#start_dateObject

Returns the value of attribute start_date.



4
5
6
# File 'app/reports/spree/report/result.rb', line 4

def start_date
  @start_date
end

#time_scaleObject

Returns the value of attribute time_scale.



4
5
6
# File 'app/reports/spree/report/result.rb', line 4

def time_scale
  @time_scale
end

Class Method Details

.charts(*report_charts) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/reports/spree/report/result.rb', line 50

def self.charts(*report_charts)
  define_method :chart_json do
    {
      chart: true,
      charts: report_charts.collect { |report_chart| report_chart.new(self).to_h }.flatten
    }
  end
end

Instance Method Details

#build_report_observationsObject



12
13
14
15
# File 'app/reports/spree/report/result.rb', line 12

def build_report_observations
  query_results
  populate_observations
end

#chart_jsonObject



43
44
45
46
47
48
# File 'app/reports/spree/report/result.rb', line 43

def chart_json
  {
    chart: false,
    charts: []
  }
end

#headersObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/reports/spree/report/result.rb', line 73

def headers
  report.class::HEADERS.keys.collect do |header|
    header_description = {
      name: Spree.t(header.to_sym, scope: [:insight, report.name]),
      value: header,
      type: report.class::HEADERS[header],
      sortable: header.in?(report.class::SORTABLE_ATTRIBUTES)
    }
    header_description[:sorted] = report.sort_direction if report.header_sorted?(header)
    header_description
  end
end

#populate_observationsObject



21
22
23
24
25
26
27
# File 'app/reports/spree/report/result.rb', line 21

def populate_observations
  @observations = @results.collect do |result|
    _observation = self.class::Observation.new
    _observation.populate(result)
    _observation
  end
end

#query_resultsObject



17
18
19
# File 'app/reports/spree/report/result.rb', line 17

def query_results
  @results = report.get_results
end

#search_attributesObject



59
60
61
# File 'app/reports/spree/report/result.rb', line 59

def search_attributes
  report.class::SEARCH_ATTRIBUTES.transform_values { |value| value.to_s.humanize }
end

#time_dimensionObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/reports/spree/report/result.rb', line 86

def time_dimension
  case time_scale
  when :hourly
    :hour_name
  when :daily
    :day_name
  when :monthly
    :month_name
  when :yearly
    :year
  end
end

#to_hObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/reports/spree/report/result.rb', line 30

def to_h
  {
    deeplink:            report.deeplink_properties,
    total_pages:         report.total_pages,
    per_page:            report.records_per_page,
    pagination_required: report.pagination_required?,
    headers:             headers,
    search_attributes:   search_attributes,
    stats:               observations.collect(&:to_h),
    chart_json:          chart_json
  }
end

#total_pagesObject

O indexed



63
64
65
66
67
68
69
70
71
# File 'app/reports/spree/report/result.rb', line 63

def total_pages # O indexed
  if report.pagination_required?
    total_pages = report.total_records / report.records_per_page
    if report.total_records % report.records_per_page == 0
      total_pages -= 1
    end
    total_pages
  end
end