Sha256: b7be291f7e1c8a7ff9ca2becefc717a7fa58a64e4725d6ed0cb32876c9562bbb

Contents?: true

Size: 1.54 KB

Versions: 34

Compression:

Stored size: 1.54 KB

Contents

module Workarea
  module Admin
    class ReleasesFeedViewModel < ApplicationViewModel
      delegate :to_ical, to: :calendar

      def calendar
        @calendar ||= Icalendar::Calendar.new.tap do |calendar|
          calendar.add_timezone(calendar_timezone)

          calendar.x_wr_calname = I18n.t(
            'workarea.admin.releases.feed.name',
            site_name: Workarea.config.site_name
          )

          releases.each do |release|
            calendar.event do |event|
              event.dtstart = format_date(release.starts_at, release.all_day_event?)
              event.dtend = format_date(release.ends_at, release.all_day_event?)
              event.summary = release.name
              event.description = release.description
            end
          end

          calendar
        end
      end

      private

      def timezone_id
        Time.zone.tzinfo.identifier
      end

      def calendar_timezone
        TZInfo::Timezone.get(timezone_id).ical_timezone(Time.current)
      end

      def releases
        @releases ||= Admin::ReleaseEventViewModel.wrap(
          (
            Release.published_within(Time.current, 1.year.from_now) +
            Release.undone_within(Time.current, 1.year.from_now)
          ).uniq
        )
      end

      def format_date(time, date_only = false)
        return unless time.present?

        if date_only
          Icalendar::Values::Date.new(time.to_date, tzid: timezone_id)
        else
          Icalendar::Values::DateTime.new(time, tzid: timezone_id)
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-admin-3.4.45 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.44 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.43 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.42 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.41 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.40 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.39 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.38 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.37 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.36 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.35 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.34 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.33 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.32 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.31 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.30 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.29 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.28 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.27 app/view_models/workarea/admin/releases_feed_view_model.rb
workarea-admin-3.4.26 app/view_models/workarea/admin/releases_feed_view_model.rb