Sha256: 864e403ec05730e1ce7d0102d0fe1003f3a129cd9166d947d532222e0c9c91ab

Contents?: true

Size: 1.05 KB

Versions: 28

Compression:

Stored size: 1.05 KB

Contents

module Workarea
  module Admin
    class InsightViewModel < ApplicationViewModel
      def results
        @results ||= model.results.map do |result|
          OpenStruct.new(
            Workarea.config.insights_model_classes.reduce(result) do |memo, klass|
              memo.merge(add_models(klass.constantize, memo))
            end
          )
        end
      end

      def add_models(klass, insight_result)
        result = insight_result.deep_dup
        key_name = klass.name.demodulize.underscore
        id_key_name = "#{key_name}_id"

        ids = model.results.flat_map { |r| r.select { |k| k =~ /#{id_key_name}$/ }.values }
        models = klass.any_in(id: ids).to_lookup_hash

        insight_result.each do |key, value|
          if key =~ /#{id_key_name}$/
            new_key = key.gsub(/#{id_key_name}$/, key_name)
            model = models[value]

            if model.present?
              result.merge!(new_key => ApplicationController.wrap_in_view_model(model))
            end
          end
        end

        result
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-admin-3.5.6 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.5 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.4 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.3 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.2 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.1 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.0 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.0.beta.1 app/view_models/workarea/admin/insight_view_model.rb