<commit_before>require "iknow_view_models/version"
require "view_model"
require "active_record_view_model"
require "active_record_view_model/controller"
require "active_record_view_model/singular_nested_controller"
require "active_record_view_model/collection_nested_controller"

module IknowViewModels

  class ExceptionView < ViewModel
    attributes :exception, :status, :metadata

    def serialize_view(json, serialize_context: nil)
      json.errors [exception] do |e|
        json.status status
        json.detail exception.message

        if error_type = exception.try(:error_type)
          json.type error_type
        end

        json.metadata do
          ViewModel.serialize(metadata, json, serialize_context: serialize_context)
        end

        if Rails.env != 'production'
          json.exception do
            json.set! :class, exception.class.name
            json.backtrace exception.backtrace
            if cause = exception.cause
              json.cause do
                json.set! :class, cause.class.name
                json.backtrace cause.backtrace
              end
            end
          end
        end
      end
    end
  end


  # expects a class that defines a "render" method
  # accepting json as a key like ActionController::Base
  # defines klass#render_view_model on the class

  def self.renderable!(klass)
    klass.class_eval do
      def render_viewmodel(viewmodel, status: nil, serialize_context: viewmodel.class.try(:new_deserialize_context))
        render_jbuilder(status: status) do |json|
          json.data do
            ViewModel.serialize(viewmodel, json, serialize_context: serialize_context)<commit_msg>Fix default serialize context on rendering<commit_after>require "iknow_view_models/version"
require "view_model"
require "active_record_view_model"
require "active_record_view_model/controller"
require "active_record_view_model/singular_nested_controller"
require "active_record_view_model/collection_nested_controller"

module IknowViewModels

  class ExceptionView < ViewModel
    attributes :exception, :status, :metadata

    def serialize_view(json, serialize_context: nil)
      json.errors [exception] do |e|
        json.status status
        json.detail exception.message

        if error_type = exception.try(:error_type)
          json.type error_type
        end

        json.metadata do
          ViewModel.serialize(metadata, json, serialize_context: serialize_context)
        end

        if Rails.env != 'production'
          json.exception do
            json.set! :class, exception.class.name
            json.backtrace exception.backtrace
            if cause = exception.cause
              json.cause do
                json.set! :class, cause.class.name
                json.backtrace cause.backtrace
              end
            end
          end
        end
      end
    end
  end


  # expects a class that defines a "render" method
  # accepting json as a key like ActionController::Base
  # defines klass#render_view_model on the class

  def self.renderable!(klass)
    klass.class_eval do
      def render_viewmodel(viewmodel, status: nil, serialize_context: viewmodel.class.try(:new_serialize_context))
        render_jbuilder(status: status) do |json|
          json.data do
            ViewModel.serialize(viewmodel, json, serialize_context: serialize_context)
{'fraction_non_alphanumeric': 0.06589492430988424, 'fraction_numerical': 0.0, 'mean_word_length': 2.7361419068736144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5252527', 'n_tokens_mistral': 919, 'n_tokens_neox': 892, 'n_words': 230}