diff --git a/app/models/calentry.rb b/app/models/calentry.rb index 9dbc178..94561f3 100644 --- a/app/models/calentry.rb +++ b/app/models/calentry.rb @@ -16,14 +16,21 @@ class Calentry < ActiveRecord::Base has_and_belongs_to_many :calendars validates :start, :presence => true validates :typ, :presence => true + before_save :get_public + belongs_to :object, polymorphic: true + validate do |entry| if entry.ende.nil? errors.add(:ende, "Es muss ein Endzeitpunkt vorhanden sein") end end - belongs_to :object, polymorphic: true + resourcify + def get_public + self.public = (self.try(:object).nil?)? (self.calendars.public.count>0) : object.try(:public) + true + end def start_time start end @@ -33,5 +40,6 @@ class Calentry < ActiveRecord::Base def name summary end + scope :public, -> { where(:public => :true) } scope :upcoming, -> { where("start >= ?" , Time.now).where("start <= ?", 8.days.from_now) } end diff --git a/db/migrate/20130823084252_add_object_to_calentries.rb b/db/migrate/20130823084252_add_object_to_calentries.rb new file mode 100644 index 0000000..4770792 --- /dev/null +++ b/db/migrate/20130823084252_add_object_to_calentries.rb @@ -0,0 +1,14 @@ +class AddObjectToCalentries < ActiveRecord::Migration + def up + add_column :calentries,:object_id, :integer + add_column :calentries,:object_type, :string + add_column :calentries, :public, :boolean + Calentry.update_all(:public => :true) + end + + def down + remove_column :calentries, :object_id + remove_column :calentries,:object_type + remove_column :calentries,:public + end +end