models.rb 2.68 KB
Newer Older
Jean-Noël Rouchon's avatar
Jean-Noël Rouchon committed
1 2
# coding: utf-8

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2015 Jean-Noël Rouchon <mail@mithrilre>
#
# This file is part of Posao.
#
# Posao is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Posao is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Posao.  If not, see <http://www.gnu.org/licenses/>.

Jean-Noël Rouchon's avatar
Jean-Noël Rouchon committed
20 21 22 23
class Technician < ActiveRecord::Base
	validates_presence_of :name, :login
	has_many :tasks
	has_many :technicians_rights
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	
	def get_types
		return Technicians_type.includes(:type).where(:technician_id => self.id)
	end
	
	def get_types_ids
		ids = []
		Technicians_type.includes(:type).where(:technician_id => self.id).each do |type|
			ids << type.type_id
		end
		return ids
	end
	
	def add_types ids
		Technicians_type.where(:technician_id => self.id).destroy_all
		ids.uniq.each do |id|
			Technicians_type.create :technician_id => self.id, :type_id => id
		end
	end
	
	def get_sites
		return Technicians_site.includes(:site).where(:technician_id => self.id)
	end
	
	def get_sites_ids
		ids = []
		Technicians_site.includes(:site).where(:technician_id => self.id).each do |site|
			ids << site.site_id
		end
		return ids
	end
	
	def add_sites ids
		Technicians_site.where(:technician_id => self.id).destroy_all
		ids.uniq.each do |id|
			Technicians_site.create :technician_id => self.id, :site_id => id
		end
	end
Jean-Noël Rouchon's avatar
Jean-Noël Rouchon committed
62 63 64 65
end

class Technicians_type < ActiveRecord::Base
	belongs_to :technician
66
	belongs_to :type
Jean-Noël Rouchon's avatar
Jean-Noël Rouchon committed
67 68 69 70
end

class Technicians_site < ActiveRecord::Base
	belongs_to :technician
71
	belongs_to :site
Jean-Noël Rouchon's avatar
Jean-Noël Rouchon committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
end

class State < ActiveRecord::Base
	validates_presence_of :name
	has_many :tasks
end

class Type < ActiveRecord::Base
	validates_presence_of :description, :icone, :icone_plan
	has_many :tasks
end

class Site < ActiveRecord::Base
	validates_presence_of :name
	has_many :places
end

class Place < ActiveRecord::Base
	validates_presence_of :name
	belongs_to :site
	has_many :tasks
end

class Task < ActiveRecord::Base
	validates_presence_of :description, :place_id, :type_id, :state_id, :date
	belongs_to :technician
	belongs_to :type
	belongs_to :place
	belongs_to :state
	has_many :actions
end

class Supplier < ActiveRecord::Base
	validates_presence_of :name
	has_many :actions
end

class Action < ActiveRecord::Base
	validates_presence_of :description
	belongs_to :supplier
	belongs_to :technician
	belongs_to :task
end