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.27 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.26 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.25 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.23 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.22 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.21 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.20 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.19 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.18 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.17 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.16 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.15 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.14 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.13 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.12 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.11 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.10 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.9 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.8 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.5.7 app/view_models/workarea/admin/insight_view_model.rb