Sha256: 264b0879198b3bdf583cc1efca35310b2bd74e67271dd19b9efd94ccb25ce8df

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# Return a new record with fields selectively copied from the original, and
# the start_time and end_time adjusted so that their date is set to today and
# their time-of-day is set to the original record's time-of-day.
module Calagator
  class Event < ActiveRecord::Base
    class Cloner < Struct.new(:event)
      def self.clone(event)
        new(event).clone
      end

      ATTRIBUTES = %i[title description venue_id url tag_list venue_details].freeze

      def clone
        clone = Event.new
        ATTRIBUTES.each do |attribute|
          clone.send "#{attribute}=", event.send(attribute)
        end
        if event.start_time
          clone.start_time = clone_time_for_today(event.start_time)
        end
        clone.end_time = clone_time_for_today(event.end_time) if event.end_time
        clone
      end

      private

      # Return a time that's today but has the time-of-day component from the
      # +source+ time argument.
      def clone_time_for_today(source)
        today = Date.today
        Time.zone.local(today.year, today.mon, today.day, source.hour, source.min, source.sec, source.usec)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calagator-1.1.0 app/models/calagator/event/cloner.rb