Class: ServicePackPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/service_pack_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_pack) ⇒ ServicePackPresenter

Returns a new instance of ServicePackPresenter.



3
4
5
6
7
8
9
10
# File 'app/helpers/service_pack_presenter.rb', line 3

def initialize(service_pack)
	# we don't take NIL as an option
	if service_pack&.id
		@service_pack = service_pack
	else
		raise 'This is NIL cannot print'
	end
end

Instance Attribute Details

#service_packObject (readonly)

Returns the value of attribute service_pack.



2
3
4
# File 'app/helpers/service_pack_presenter.rb', line 2

def service_pack
  @service_pack
end

Instance Method Details

#hash_lite_headerObject



15
16
17
# File 'app/helpers/service_pack_presenter.rb', line 15

def hash_lite_header
	@service_pack.as_json(except: [:id, :threshold1, :threshold2, :updated_at, :created_at])
end

#hash_rate_onlyObject



21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/service_pack_presenter.rb', line 21

def hash_rate_only
	# 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
end

#json_export(sym = :header) ⇒ Object



35
36
37
38
# File 'app/helpers/service_pack_presenter.rb', line 35

def json_export(sym=:header)
	err = { :error => 422, :name => "Unsupported format"}
	sym == :header ? json_lite_header : (sym == :rate ? json_rate_only : err.to_json)
end

#json_full_headerObject



11
12
13
14
# File 'app/helpers/service_pack_presenter.rb', line 11

def json_full_header
	# not recommended in production
	@service_pack.to_json
end

#json_lite_headerObject



18
19
20
# File 'app/helpers/service_pack_presenter.rb', line 18

def json_lite_header
	hash_lite_header.to_json
end

#json_rate_onlyObject



32
33
34
# File 'app/helpers/service_pack_presenter.rb', line 32

def json_rate_only
	hash_rate_only.to_json
end