resource.rb 1.17 KB
Newer Older
Tyler Lemburg's avatar
Tyler Lemburg committed
1
2
3
require 'active_record'
require 'models/resource_approver'
require 'models/resource_authorization'
4
5
6
require 'models/resource_class'
require 'models/resource_field'
require 'models/resource_field_data'
Tyler Lemburg's avatar
Tyler Lemburg committed
7
8

class Resource < ActiveRecord::Base
9
	belongs_to :service_space
Tyler Lemburg's avatar
Tyler Lemburg committed
10
11
12
	has_many :reservations, dependent: :destroy
	has_many :resource_approvers, dependent: :destroy
	has_many :resource_authorizations, dependent: :destroy
13
14
	belongs_to :resource_class
	has_many :resource_field_datas, dependent: :destroy
Tyler Lemburg's avatar
Tyler Lemburg committed
15
	alias_method :approvers, :resource_approvers
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

	def method_missing(meth_sym)
		# check if this symbol can be associated with a field for this class
		if !self.resource_class.nil? && (field = ResourceField.find_by(:resource_class_id => self.resource_class.id, :field_name => meth_sym.to_s.downcase))
			# use this field id to retrieve the data from the resoruce field data table
			data = ResourceFieldData.find_by(:resource_id => self.id, :resource_field_id => field.id)
			unless data.nil?
				return data.data
			end	
		end
		super
	end

	def get_field_data
		{}.tap do |hash|
			self.resource_field_datas.each do |data|
				hash[data.resource_field.field_name.downcase] = data.data
			end
		end
	end
Tyler Lemburg's avatar
Tyler Lemburg committed
36
end