Sha256: af8ca858c2f5edd14938eb23964bf8da50cf58ff888b0932d32e10c29953a612

Contents?: true

Size: 1.98 KB

Versions: 34

Compression:

Stored size: 1.98 KB

Contents

module Workarea
  module Admin
    class ChangesetViewModel < ApplicationViewModel
      def releasable
        @releasable ||= Mongoid::DocumentPath.find(model.document_path)
      end

      def name
        [root, releasable]
          .reject(&:blank?)
          .uniq
          .map(&:name)
          .reject(&:blank?)
          .join(' - ')
      end

      def root
        @root ||= Mongoid::DocumentPath.find(model.document_path.take(1))
      end

      def localized_change?(field, value)
        field = field.to_s

        !!releasable.class.fields[field] &&
          !!releasable.class.fields[field].options[:localize] &&
          value.is_a?(Hash)
      end

      def currency_change?(field, value)
        field = field.to_s

        !!releasable.class.fields[field] &&
          releasable.class.fields[field].options[:type] == Money &&
          value.is_a?(Hash)
      end

      def new_value_for(field)
        get_localized_value(field, changeset[field])
      end

      def old_value_for(field)
        return get_localized_value(field, undo[field]) if undo.present?
        current_releasable.send(field)
      end

      def release_date
        if release.scheduled?
          release.publish_at
        elsif release.published?
          release.published_at
        end
      end

      def publish_humanized
        if release.published?
          t('workarea.admin.changesets.published_on')
        elsif release.scheduled?
          t('workarea.admin.changesets.publishes_on')
        else
          t('workarea.admin.changesets.unscheduled')
        end
      end

      private

      def get_localized_value(field, value)
        if localized_change?(field, value)
          value.with_indifferent_access[I18n.locale]
        elsif currency_change?(field, value)
          Money.new(*value.values)
        else
          value
        end
      end

      def current_releasable
        @current_releasable ||= Release.with_current(nil) { releasable.reload }
      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/changeset_view_model.rb
workarea-admin-3.4.44 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.43 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.42 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.41 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.40 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.39 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.38 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.37 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.36 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.35 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.34 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.33 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.32 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.31 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.30 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.29 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.28 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.27 app/view_models/workarea/admin/changeset_view_model.rb
workarea-admin-3.4.26 app/view_models/workarea/admin/changeset_view_model.rb