<commit_before>require 'location'

class Event < ActiveRecord::Base
  include Location
  geocoded_by :address

  after_validation :reset_geocode
  before_save :schedule_to_yaml
  after_save :generate_single_events

  has_many :single_events
  has_many :comments, as: :commentable, dependent: :destroy

  attr_writer :schedule

  # Provide tagging
  acts_as_taggable

  def generate_single_events
    self.future_single_events_cleanup
    self.future_single_event_creation
  end

  # Delete SingleEvents that don't match the pattern
  def future_single_events_cleanup
    rule_based_events = self.single_events.in_future.where based_on_rule: true

    rule_based_events.each do |single_event|
      single_event.delete unless schedule.occurs_at? single_event.occurrence
    end
  end

  # Add SingleEvents that are in the pattern, but haven't been created so far
  def future_single_event_creation
    self.schedule.next_occurrences(12).each do |time|
      # remove milliseconds from occurrence, this causes a bug
      # in sqlite since it uses milliseconds. Sadly the milliseconds
      # the ice_cube generates are different each time. It's the
      # current millisecond when the method is called
      # (See https://github.com/seejohnrun/ice_cube/issues/84)
      occurrence = Time.new time.year,
        time.month,
        time.day,
        time.hour,
        time.min,
        time.sec

      # ToDo: Hot-Fix for Bug #83
      if !self.schedule.extimes.map(&:to_i).include? occurrence.to_i
        SingleEvent.find_or_create event_id: self.id,
          occurrence: occurrence,
          based_on_rule: true
      end
    end
  end

  def schedule
    if @schedule.nil?
      if self.schedule_yaml.present?
        begin
          @schedule = IceCube::Schedule.from_yaml self.schedule_yaml
        rescue => e
          # Shit, parsing went wrong
        end
      end

      if @schedule.nil?
        @schedule = IceCube::Schedule.new Time.now, duration: 1.hour
      end
    end
    @schedule
  end

  def title
    self.name
  end

  def to_param
    "#{self.id}-#{self.name.parameterize}"
  end

  def short_description
    if self.description.blank?
      nil
    else
      description = ActionController::Base.helpers.strip_tags self.description
      description.truncate 80
    end
  end

  def to_opengraph
    graph = {}

    graph["og:title"]          = self.name         unless self.name.blank?
    graph["og:description"]    = short_description unless short_description.blank?
    graph["og:latitude"]       = self.latitude     unless self.latitude.blank?
    graph["og:longitude"]      = self.longitude    unless self.longitude.blank?
    graph["og:street-address"] = self.street       unless self.street.blank?
    graph["og:locality"]       = self.location     unless self.location.blank?
    graph["og:postal-code"]    = self.zipcode      unless self.zipcode.blank?
    graph["og:country-name"]   = self.country      unless self.country.blank?

    graph
  end<commit_msg>Refactored Event#to_opengraph<commit_after>require 'location'

class Event < ActiveRecord::Base
  include Location
  geocoded_by :address

  after_validation :reset_geocode
  before_save :schedule_to_yaml
  after_save :generate_single_events

  has_many :single_events
  has_many :comments, as: :commentable, dependent: :destroy

  attr_writer :schedule

  # Provide tagging
  acts_as_taggable

  def generate_single_events
    self.future_single_events_cleanup
    self.future_single_event_creation
  end

  # Delete SingleEvents that don't match the pattern
  def future_single_events_cleanup
    rule_based_events = self.single_events.in_future.where based_on_rule: true

    rule_based_events.each do |single_event|
      single_event.delete unless schedule.occurs_at? single_event.occurrence
    end
  end

  # Add SingleEvents that are in the pattern, but haven't been created so far
  def future_single_event_creation
    self.schedule.next_occurrences(12).each do |time|
      # remove milliseconds from occurrence, this causes a bug
      # in sqlite since it uses milliseconds. Sadly the milliseconds
      # the ice_cube generates are different each time. It's the
      # current millisecond when the method is called
      # (See https://github.com/seejohnrun/ice_cube/issues/84)
      occurrence = Time.new time.year,
        time.month,
        time.day,
        time.hour,
        time.min,
        time.sec

      # ToDo: Hot-Fix for Bug #83
      if !self.schedule.extimes.map(&:to_i).include? occurrence.to_i
        SingleEvent.find_or_create event_id: self.id,
          occurrence: occurrence,
          based_on_rule: true
      end
    end
  end

  def schedule
    if @schedule.nil?
      if self.schedule_yaml.present?
        begin
          @schedule = IceCube::Schedule.from_yaml self.schedule_yaml
        rescue => e
          # Shit, parsing went wrong
        end
      end

      if @schedule.nil?
        @schedule = IceCube::Schedule.new Time.now, duration: 1.hour
      end
    end
    @schedule
  end

  def title
    self.name
  end

  def to_param
    "#{self.id}-#{self.name.parameterize}"
  end

  def short_description
    if self.description.blank?
      nil
    else
      description = ActionController::Base.helpers.strip_tags self.description
      description.truncate 80
    end
  end

  def to_opengraph
    {
      "og:title"          => name,
      "og:description"    => short_description,
      "og:latitude"       => latitude,
      "og:longitude"      => longitude,
      "og:street-address" => street,
      "og:locality"       => location,
      "og:postal-code"    => zipcode,
      "og:country-name"   => country
    }.reject { |key, value| value.blank? }
  end
{'fraction_non_alphanumeric': 0.07916812303390423, 'fraction_numerical': 0.0031457532331352674, 'mean_word_length': 2.730769230769231, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5730306', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1706, 'n_words': 428}