Sha256: 195213eef814647e0ab07f8d8362815c600cb57da41a96906137dc23280b6784

Contents?: true

Size: 1.07 KB

Versions: 34

Compression:

Stored size: 1.07 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_a

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

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

        result
      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/insight_view_model.rb
workarea-admin-3.4.44 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.43 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.42 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.41 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.40 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.39 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.38 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.37 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.36 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.35 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.34 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.33 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.32 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.31 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.30 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.29 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.28 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.27 app/view_models/workarea/admin/insight_view_model.rb
workarea-admin-3.4.26 app/view_models/workarea/admin/insight_view_model.rb