Files
fetsite/app/models/meeting.rb

171 lines
4.8 KiB
Ruby

# coding: utf-8
# This class represents a single meeting of a group or the whole Fachschaft.
#
class Meeting < ActiveRecord::Base
attr_accessible :desc, :intern, :name, :parent_id, :parent_type, :calentry,:calentry_attributes, :meetingtyp_id
# Meeting can belong to different kind of parents e.g. topics
belongs_to :parent, :polymorphic=>true, touch: true
# Each meeting should have a type, for more information see MeetingTyp
belongs_to :meetingtyp
# Belongs to a news article, that is usually the Announcement of that meeting
belongs_to :neuigkeit, touch: true
# has one protocol of type Document, which is of typ=10=protocol
#has_one :test_ol, :class_name=>'Document', -> { where(:typ => 10) }
has_one :protocol, -> { where(:typ => 10) }, :class_name=>'Document', :as=>:parent
# has one agenda of type Document
has_one :agenda, -> { where :typ=>11 } , :as=>:parent, :class_name=>'Document'
# has one calentry which contains the date and time of this meeting
has_one :calentry, as: :object, :dependent=> :destroy
# has one calendar through the meetingtyp, thus each meeting is put into a calendar
has_one :calendar, :through=>:meetingtyp
# has one rubrik, thus it is associated to a rubrik
has_one :rubrik, :through=>:meetingtyp
# scope upcomming only contains meetings in the future and 1 hour into the past
scope :upcomming, -> {includes(:calentry).where("calentries.start>?",1.hour.ago)}
default_scope -> { includes(:calentry).order("calentries.start").reverse_order}
accepts_nested_attributes_for :calentry
# Each meeting is required to have a parent otherwise it will not be shown anywhere on the website
validates :parent, :presence=>true
# Each meeting needs a calendar entry
validates :calentry, :presence=>true
# before each validation fix the calendar entry such that it is always
before_validation :fix_calentry
# title is an alias for text
def title
self.text
end
# Text is a text representation of the meeting
def text
t = self.meetingtyp.try(:name) || self.parent.try(:title) || ""
t = t + ", " unless t.empty?
t= t+ self.name.to_s
t = t +" "+ I18n.t("date.am")+" "+ self.calentry.text unless self.calentry.nil?
t
end
# Creante an anouncement with an author
# @param user is the author
def create_announcement(user)
if self.neuigkeit.nil?
n = Neuigkeit.new
n.title=self.text
n.text ="Agenda im Anhang"
n.rubrik = self.meetingtyp.rubrik
n.author=user
n.datum=Date.today
n.save
self.neuigkeit= n
end
end
# Update the title of the announcement to the text of this meeting
# @returns [true] if save was successful
def update_announcement
n=self.neuigkeit
n.title=self.text
n.save
end
# fixing the calendar entry and the calendar
# most importantly the calendar of the calentry
def fix_calentry
self.calentry.object=self unless self.calentry.nil?
self.calentry.calendar = self.meetingtyp.rubrik.calendar
end
def public?
! (self.intern)
end
# Create a Document that is a protocol for the meeting
def create_protocol
if self.protocol.nil? # if no protocol exists
d=Document.new
d.typ=10
d.name="Protokoll"
d.save
self.protocol=d
end
# what happens if a protocol already exists?
end
# Create a calender entry for this meeting
def create_calentry
if self.calentry.nil?
ce =Calentry.new
ce.typ=2
self.calentry=ce
end
end
def create_agenda
if self.agenda.nil?
d=Document.new
d.typ=11
d.name="Agenda"
d.save
self.agenda=d
end
end
def self.new_with_date_and_typ(parent,start, typ)
m= Meeting.new
m.parent=parent
m.calentry=Calentry.new
m.calentry.typ=2
m.calentry.start=start
m.calentry.dauer=4
m.meetingtyp=typ
m
end
def self.new_divid_for(parent)
"meeting_new_parent_" + parent.class.to_s + "_" + parent.id.to_s
end
def divid
"meeting_"+self.id.to_s
end
def update_time_from_protocol
st= /Beginn[\s:]*([^<>]*)/.match(self.protocol.text)[1].to_s
st= /Anfang[\s:]*([^<>]*)/.match(self.protocol.text)[1].to_s if st.nil? || st.empty?
self.calentry.start=(self.calentry.start.to_date.to_s + " " +st).to_datetime unless st.nil? || st.empty?
st= /Ende[\s:]*([^<>]*)/.match(self.protocol.text).try(:second).try(:to_s )
self.calentry.ende=(self.calentry.ende.to_date.to_s + " " +st).to_datetime unless st.nil? || st.empty?
end
def agenda_text
return self.agenda.try(:text) || ""
end
def protocol_text
return self.protocol.try(:text) || ""
end
searchable do
text :text
text :name, :boost=>4.0
text :meetingtyp do
meetingtyp.name
end
text :protocol do
self.protocol_text
end
text :agenda do
self.agenda_text
end
end
end