service_pack_presenter.rb 1.03 KB
Newer Older
Tam Le's avatar
Tam Le committed
1
class ServicePackPresenter
Tam Le's avatar
Tam Le committed
2
	attr_reader :service_pack
Tam Le's avatar
Tam Le committed
3
4
5
6
7
8
9
10
11
	def initialize(service_pack)
		# we don't take NIL as an option
		if service_pack && service_pack.id
			@service_pack = service_pack
		else
			raise "This is NIL cannot print"
		end
	end
	def json_full_header
Tam Le's avatar
Tam Le committed
12
		# not recommended in production
Tam Le's avatar
Tam Le committed
13
14
15
		@service_pack.to_json
	end
	def hash_lite_header
Tam Le's avatar
Tam Le committed
16
		@service_pack.as_json(except: [:id, :threshold1, :threshold2, :updated_at, :created_at])
Tam Le's avatar
Tam Le committed
17
18
19
20
21
	end
	def json_lite_header
		hash_lite_header.to_json
	end
	def hash_rate_only
Tam Le's avatar
Tam Le committed
22
23
24
25
26
27
28
29
30
		# ActiveRecord join returns an array!
		q = <<-SQL
			SELECT name, units_per_hour AS upt
			FROM mapping_rates t1
			INNER JOIN #{TimeEntryActivity.table_name} t2
			ON t1.activity_id = t2.id
			WHERE t1.service_pack_id = #{@service_pack.id}
			SQL
		ActiveRecord::Base.connection.exec_query(q).to_hash
Tam Le's avatar
Tam Le committed
31
32
33
34
35
36
37
	end
	def json_rate_only
		hash_rate_only.to_json
	end
	def json_export(sym=:header)
		err = { :error => 422, :name => "Unsupported format"}
		sym == :header ? json_lite_header : (sym == :rate ? json_rate_only : err.to_json)
Tam Le's avatar
Tam Le committed
38
39
	end
end