Sha256: d83902de7b035e62ba1997898fdcb712d3eccecb3700c59907d54b885f0fd748

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

module Eventsimple
  class EntitiesController < ApplicationController
    # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
    def show
      @model_name = params[:model_name]
      @model_class = event_classes.find { |d| d.name == @model_name }
      @aggregate_id = params[:id]
      @event_id = params[:e] || -1
      @tab_id = (params[:t] == 'event') ? 'event' : 'entity'

      primary_key = @model_class.event_class._aggregate_id
      @entity = @model_class.find_by!(primary_key => @aggregate_id)
      @entity_event_history = @entity.events.reverse

      @selected_event = @entity_event_history.find { |e|
        e.id == @event_id.to_i
      } || @entity_event_history.first

      previous_index = @entity_event_history.find_index { |e| e.id == @selected_event.id } + 1
      @previous_event = @entity_event_history[previous_index]

      @entity_changes = changes
    rescue StandardError => e
      @error_message = e.message
      render html: '', layout: true
    end
    # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity

    private

    def changes
      current_attributes.map do |attr_name, *|
        {
          label: attr_name,
          current_value: current_attributes[attr_name],
          historical_value: previous_attributes[attr_name],
          is_changed: current_attributes[attr_name] != previous_attributes[attr_name],
        }
      end
    end

    def current_attributes
      @current_attributes ||= @entity.reproject(at: @selected_event.created_at).attributes.except(
        'lock_version',
      )
    end

    def previous_attributes
      @previous_attributes ||=
        if @previous_event
          @entity.reproject(at: @previous_event.created_at).attributes.except(
            'lock_version',
          )
        else
          @model_class.column_defaults
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
eventsimple-1.2.1 app/controllers/eventsimple/entities_controller.rb
eventsimple-1.2.0 app/controllers/eventsimple/entities_controller.rb
eventsimple-1.1.3 app/controllers/eventsimple/entities_controller.rb
eventsimple-1.1.2 app/controllers/eventsimple/entities_controller.rb
eventsimple-1.1.1 app/controllers/eventsimple/entities_controller.rb
eventsimple-1.1.0 app/controllers/eventsimple/entities_controller.rb