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