Class: ActsAsEventOwner::EventSpecification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/acts_as_event_owner/event_specification.rb

Constant Summary collapse

ON_THE =
{ :first => '1', :second => '2', :third => '3', :fourth => '4', :last => '-1' }
BYDAYS =
{ :day => 'SU,MO,TU,WE,TH,FR,SA', :wkday => 'MO,TU,WE,TH,FR', :wkend => 'SU,SA'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generateObject

Returns the value of attribute generate.



25
26
27
# File 'lib/acts_as_event_owner/event_specification.rb', line 25

def generate
  @generate
end

Class Method Details

.generate_events(options = {}) ⇒ Object



136
137
138
139
140
# File 'lib/acts_as_event_owner/event_specification.rb', line 136

def self.generate_events options={}
  self.all(:conditions => "until IS NULL OR until >= '#{Time.now.utc.to_s(:db)}'").each {|spec|
    spec.generate_events(options)
  }
end

Instance Method Details

#generate_events(options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/acts_as_event_owner/event_specification.rb', line 98

def generate_events options={}
  raise ActsAsEventOwner::Exception.new("Invalid Event Specification") if !valid?

  opts = options.clone
  opts[:from] ||= self.start_at
  opts[:to] ||= (opts[:from] + 30.days) if opts[:from]
  opts[:from] -= 1.second
  opts[:to] -= 1.second
  opts[:from] = opts[:to] = nil if opts[:count]
  attribute_overrides = opts[:attributes] || {}

  # puts "generate #{self.attributes.inspect} from #{opts[:from]} to #{opts[:to]}, extended_attributes = #{extended_attributes.inspect}"

  cal = RiCal.Calendar do |cal|
    cal.event do |event|
      event.description self.description
      event.dtstart(self.start_at) if self.start_at
      event.dtend(self.end_at) if self.end_at
      event.rrule = self.to_rrule if self.to_rrule
    end
  end
  event = cal.events.first
  occurrences = event.occurrences(:starting => opts[:from], :before => opts[:to], :count => opts[:count])
  occurrences.collect do |occurrence|
    @@OCCURRENCE_COLUMNS ||= (EventOccurrence.columns.collect(&:name) - EXCLUDED_COLUMNS)
    @@SPECIFICATION_COLUMNS ||= (EventSpecification.columns.collect(&:name) - EXCLUDED_COLUMNS)
    additional_columns = (@@SPECIFICATION_COLUMNS).inject({}) do |additional, column|
      additional[column] = self.attributes[column] if @@OCCURRENCE_COLUMNS.include?(column)
      additional
    end

    EventOccurrence.find_or_create_by_owner_id_and_owner_type_and_event_specification_id_and_start_at_and_end_at({
      :owner_id => self.owner_id, :owner_type => self.owner_type, :event_specification_id => self.id,
      :description => occurrence.description, :start_at => occurrence.start_time,
      :end_at => occurrence.finish_time}.merge(additional_columns).merge(attribute_overrides.stringify_keys))
  end
end

#repeatObject



142
143
144
# File 'lib/acts_as_event_owner/event_specification.rb', line 142

def repeat
  self.attributes["repeat"].try(:to_sym)
end

#to_rruleObject



62
63
64
65
66
67
68
69
70
71
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
# File 'lib/acts_as_event_owner/event_specification.rb', line 62

def to_rrule
  return nil if !self.valid? || self.repeat.nil?

  components = []
  repeat = self.repeat
  frequency = self.frequency

  case self.repeat
    when :by_hour
      repeat = "DAILY"
      components << "BYHOUR=#{self.target.join(',')}"
      frequency = nil

    when :daily

    when :weekly
      components << "BYDAY=#{self.on.join(',').upcase}" if self.on

    when :monthly
      if self.on_the
        components << "BYSETPOS=#{ON_THE[self.on_the]}"
        components << "BYDAY=#{byday}"
      end
      components << "BYMONTHDAY=#{self.on.join(',').upcase}" if self.on

    when :yearly
      components << "BYMONTH=#{self.on.join(',').upcase}" if self.on
      components << "BYSETPOS=#{ON_THE[self.on_the]};BYDAY=#{byday}" if self.on_the
  end

  components.unshift "INTERVAL=#{frequency}" if frequency
  components.unshift "FREQ=#{repeat.to_s.upcase}"
  components << "UNTIL=#{self.until.strftime("%Y%m%dT%H%M%SZ")}" if self.until
  components.join(';')
end

#validate_recurrence_rulesObject



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
# File 'lib/acts_as_event_owner/event_specification.rb', line 28

def validate_recurrence_rules
  case self.repeat
    when :by_hour
      errors.add(:target, "must be an array") if !self.target.present? || !self.target.is_a?(Array)
      [:on, :on_the].each {|v| errors.add(v, :present) if self.send(v)}

    when :daily
      [:on, :on_the, :target].each {|v| errors.add(v, :present) if self.send(v)}

    when :weekly
      errors.add(:on, "must be an array") if self.on.present? && !self.on.is_a?(Array)
      [:on_the, :target].each {|v| errors.add(v, :present) if self.send(v)}

    when :monthly
      if self.on_the
        errors.add(:target, "must be an array, :day, :wkday, or :wkend") if self.target.nil? || !(self.target.is_a?(Array) || BYDAYS.keys.include?(self.target))
        errors.add(:on, :present) if self.on.present?
      elsif self.on
        errors.add(:on, "must be an array") if !self.on.is_a?(Array)
        [:on_the, :target].each {|v| errors.add(v, :present) if self.send(v)}
      end

    when :yearly
      if self.on_the
        errors.add(:on, "must be an array") if !self.on.present? || !self.on.is_a?(Array)
        errors.add(:target, "must be an array, :day, :wkday, or :wkend") if self.target.nil? || !(self.target.is_a?(Array) || BYDAYS.keys.include?(self.target))
      elsif self.on
        errors.add(:on, "must be an array") if !self.on.present? || !self.on.is_a?(Array)
      else
        errors.add(:on, :present)
      end
  end
end