Class: ClarkKent::ReportResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/clark_kent/report_result.rb

Instance Method Summary collapse

Constructor Details

#initialize(arel_query, params) ⇒ ReportResult

Returns a new instance of ReportResult.



5
6
7
8
# File 'app/models/clark_kent/report_result.rb', line 5

def initialize(arel_query, params)
	@arel_query = arel_query
	@params = params
end

Instance Method Details

#current_pageObject



19
20
21
# File 'app/models/clark_kent/report_result.rb', line 19

def current_page
	@params[:page]
end

#each(&block) ⇒ Object



44
45
46
# File 'app/models/clark_kent/report_result.rb', line 44

def each(&block)
	rows.each(&block)
end

#paginated_queryObject



10
11
12
13
14
15
16
17
# File 'app/models/clark_kent/report_result.rb', line 10

def paginated_query
	if @params.has_key? :page and @params.has_key? :per
		page = @params[:page] || 1
		@arel_query.offset((page.to_i - 1) * @params[:per]).limit(@params[:per])
	else
		@arel_query
	end
end

#per_pageObject



23
24
25
# File 'app/models/clark_kent/report_result.rb', line 23

def per_page
	@params[:per]
end

#queryObject



27
28
29
# File 'app/models/clark_kent/report_result.rb', line 27

def query
	@arel_query
end

#rowsObject



40
41
42
# File 'app/models/clark_kent/report_result.rb', line 40

def rows
	results_for(paginated_query.to_sql)
end

#total_countObject



31
32
33
34
35
36
37
38
# File 'app/models/clark_kent/report_result.rb', line 31

def total_count
	unless defined?(@total_count)
		results = results_for(@arel_query.to_sql)
		@total_count = results.num_tuples
	end

	@total_count
end