Class: Spree::Report

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

Defined Under Namespace

Modules: Chart, DateSlicer, QueryFragments, QueryTimeScale Classes: Configuration, Observation, Result, TimedObservation, TimedResult

Constant Summary collapse

TIME_SCALES =
[:hourly, :daily, :monthly, :yearly]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Report

Returns a new instance of Report.



42
43
44
45
46
47
48
49
50
51
52
# File 'app/reports/spree/report.rb', line 42

def initialize(options)
  self.search = options.fetch(:search, {})
  self.records_per_page = options[:records_per_page]
  self.current_page = options[:offset]
  self.paginate = options[:paginate]
  extract_reporting_period
  determine_report_time_scale
  if self.class::SORTABLE_ATTRIBUTES.present?
    set_sortable_attributes(options, self.class::DEFAULT_SORTABLE_ATTRIBUTE)
  end
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



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

def current_page
  @current_page
end

#paginateObject Also known as: paginate?

Returns the value of attribute paginate.



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

def paginate
  @paginate
end

#records_per_pageObject

Returns the value of attribute records_per_page.



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

def records_per_page
  @records_per_page
end

#reporting_periodObject

Returns the value of attribute reporting_period.



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

def reporting_period
  @reporting_period
end

#searchObject

Returns the value of attribute search.



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

def search
  @search
end

#sortable_attributeObject

Returns the value of attribute sortable_attribute.



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

def sortable_attribute
  @sortable_attribute
end

#sortable_typeObject Also known as: sort_direction

Returns the value of attribute sortable_type.



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

def sortable_type
  @sortable_type
end

#total_recordsObject

Returns the value of attribute total_records.



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

def total_records
  @total_records
end

Class Method Details



26
27
28
29
30
# File 'app/reports/spree/report.rb', line 26

def self.deeplink(template_for_headers = {})
  define_method :deeplink_properties do
    { deeplinked: true }.merge(template_for_headers)
  end
end

Instance Method Details

#active_record_sortObject



76
77
78
# File 'app/reports/spree/report.rb', line 76

def active_record_sort
  "#{ sortable_attribute } #{ sortable_type }"
end


20
21
22
23
24
# File 'app/reports/spree/report.rb', line 20

def deeplink_properties
  {
    deeplinked: false
  }
end

#generate(options = {}) ⇒ Object



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

def generate(options = {})
  self.class::Result.new do |report|
    report.start_date = @start_date
    report.end_date   = @end_date
    report.time_scale = @time_scale
    report.report = self
  end
end

#get_resultsObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/reports/spree/report.rb', line 58

def get_results
  query =
    if pagination_required?
      paginated_report_query
    else
      report_query
    end

  query = query.order(active_record_sort) if sortable_attribute.present?
  query_sql = query.to_sql
  r = ActiveRecord::Base.connection.exec_query(query_sql)
end

#header_sorted?(header) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/reports/spree/report.rb', line 54

def header_sorted?(header)
  sortable_attribute.present? && sortable_attribute.eql?(header)
end

#nameObject



104
105
106
# File 'app/reports/spree/report.rb', line 104

def name
  @_report_name ||= self.class.to_s.demodulize.underscore.gsub("_report", "")
end

#paginated?Boolean

Returns:

  • (Boolean)


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

def paginated?
  false
end

#pagination_required?Boolean

Returns:

  • (Boolean)


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

def pagination_required?
  paginated? && paginate?
end

#set_sortable_attributes(options, default_sortable_attribute) ⇒ Object



71
72
73
74
# File 'app/reports/spree/report.rb', line 71

def set_sortable_attributes(options, default_sortable_attribute)
  self.sortable_type ||= (options[:sort] && options[:sort][:type].eql?('desc')) ? :desc : :asc
  self.sortable_attribute = options[:sort] ? options[:sort][:attribute].to_sym : default_sortable_attribute
end

#time_scale_columnsObject



96
97
98
# File 'app/reports/spree/report.rb', line 96

def time_scale_columns
  @_time_scale_columns ||= QueryTimeScale.time_scale_columns(@time_scale)
end

#time_scale_columns_to_sObject



100
101
102
# File 'app/reports/spree/report.rb', line 100

def time_scale_columns_to_s
  @_time_scale_columns_to_s ||= time_scale_columns.collect(&:to_s)
end

#time_scale_selects(time_scale_on = nil) ⇒ Object



92
93
94
# File 'app/reports/spree/report.rb', line 92

def time_scale_selects(time_scale_on = nil)
  QueryTimeScale.select(@time_scale, time_scale_on)
end

#total_pagesObject



84
85
86
87
88
89
90
# File 'app/reports/spree/report.rb', line 84

def total_pages
  if pagination_required?
    total_pages = total_records / records_per_page
    total_pages -= 1 if total_records % records_per_page == 0
    total_pages
  end
end