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



143
144
145
146
147
# File 'lib/acts_as_event_owner/event_specification.rb', line 143

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

Instance Method Details

#generate_events(options = {}) ⇒ Object



96
97
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
135
136
137
138
139
140
141
# File 'lib/acts_as_event_owner/event_specification.rb', line 96

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

  opts = options.clone
  opts[:from] ||= Time.now
  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]} with #{attribute_overrides.inspect}"

  start_at = self.start_at
  end_at = self.end_at
  cal = RiCal.Calendar do |cal|
    cal.event do |event|
      event.description self.description
      event.dtstart(start_at.in_time_zone) if start_at
      event.dtend(end_at.in_time_zone) if end_at
      event.rrule = self.to_rrule if self.to_rrule
    end
  end
  event = cal.events.first
  # puts "event is #{event.inspect}"
  occurrences = event.occurrences(:starting => opts[:from], :before => opts[:to], :count => opts[:count])
  # puts "got #{occurrences.length} occurrences"
  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

    # puts "*********** #{occurrence.start_time} : #{occurrence.start_time.zone}"
    # puts "*********** #{Time.zone.at(occurrence.start_time.to_i)}"

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

    EventOccurrence.find_or_create_by_owner_id_and_owner_type_and_event_specification_id_and_start_at_and_end_at(hash)
  end
end

#repeatObject



149
150
151
# File 'lib/acts_as_event_owner/event_specification.rb', line 149

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

#to_rruleObject



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

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%S")}" 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
# 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)
      end
  end
end