Skip to content

Commit

Permalink
move filter_attributes config to entity.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
desheikh committed Dec 7, 2023
1 parent 3328fc2 commit 742d234
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
10 changes: 5 additions & 5 deletions app/controllers/eventsimple/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module Eventsimple
class ModelsController < ApplicationController
def show
@model_name = params[:name]
model_event_class = event_classes.find { |d| d.name == @model_name }.event_class
model_class = event_classes.find { |d| d.name == @model_name }

model_event_class = apply_filter(model_event_class)
scope = apply_filter(model_class, model_class.event_class)

@latest_entities = model_event_class.last(20).reverse
@latest_entities = scope.last(20).reverse

check_redirect_to_entity
end

private

def apply_filter(model_event_class)
filter_columns = model_event_class._filter_attributes
def apply_filter(model_class, model_event_class)
filter_columns = model_class._filter_attributes

params_filters = params.permit(filters: {})[:filters] || {}
@filters = filter_columns.to_h { |column| [column, params_filters[column]] }
Expand Down
5 changes: 4 additions & 1 deletion lib/eventsimple/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Eventsimple
module Entity
DEFAULT_IGNORE_PROPS = %w[id lock_version].freeze

def event_driven_by(event_klass, aggregate_id:)
def event_driven_by(event_klass, aggregate_id:, filter_attributes: [])
has_many :events, class_name: event_klass.name.to_s,
foreign_key: :aggregate_id,
primary_key: aggregate_id,
Expand All @@ -13,6 +13,9 @@ def event_driven_by(event_klass, aggregate_id:)

class_attribute :ignored_for_projection, default: []

class_attribute :_filter_attributes
self._filter_attributes = [aggregate_id] | Array.wrap(filter_attributes)

# disable automatic timestamp updates
self.record_timestamps = false

Expand Down
5 changes: 1 addition & 4 deletions lib/eventsimple/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Event
include GlobalID::Identification

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def drives_events_for(aggregate_klass, aggregate_id:, events_namespace: nil, filter_attributes: nil)
def drives_events_for(aggregate_klass, aggregate_id:, events_namespace: nil)
class_attribute :_events_namespace
self._events_namespace = events_namespace

Expand All @@ -20,9 +20,6 @@ def drives_events_for(aggregate_klass, aggregate_id:, events_namespace: nil, fil
class_attribute :_on_invalid_transition
self._on_invalid_transition = ->(error) { raise error }

class_attribute :_filter_attributes
self._filter_attributes = [:aggregate_id] | Array.wrap(filter_attributes)

self.inheritance_column = :type
self.store_full_sti_class = false

Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class User < ApplicationRecord
extend Eventsimple::Entity

event_driven_by UserEvent, aggregate_id: :canonical_id
event_driven_by UserEvent, aggregate_id: :canonical_id, filter_attributes: [:username, :email]
end
3 changes: 1 addition & 2 deletions spec/dummy/app/models/user_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class UserEvent < ApplicationRecord
extend Eventsimple::Event

drives_events_for User, aggregate_id: :canonical_id, events_namespace: 'UserComponent::Events',
filter_attributes: [:username, :email]
drives_events_for User, aggregate_id: :canonical_id, events_namespace: 'UserComponent::Events'
end

0 comments on commit 742d234

Please sign in to comment.