Skip to content

Commit

Permalink
Simplified filtering and added smart redirect to model entity
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashlavacka committed Dec 7, 2023
1 parent 9949b80 commit 3328fc2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
16 changes: 14 additions & 2 deletions app/controllers/eventsimple/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ def show
model_event_class = apply_filter(model_event_class)

@latest_entities = model_event_class.last(20).reverse

check_redirect_to_entity
end

private

def apply_filter(model_event_class)
filter_columns = model_event_class._filter_attributes || []
return model_event_class unless filter_columns.any?
filter_columns = model_event_class._filter_attributes

params_filters = params.permit(filters: {})[:filters] || {}
@filters = filter_columns.to_h { |column| [column, params_filters[column]] }
Expand All @@ -24,10 +25,21 @@ def apply_filter(model_event_class)
model_event_class = model_event_class.joins(aggregate_class_symbol)
@filters.each do |key, value|
next if value.blank?
key = model_event_class._aggregate_id if key == :aggregate_id
model_event_class = model_event_class.where({ aggregate_class_symbol => { key => value } })
end

model_event_class
end

def check_redirect_to_entity
return unless @latest_entities.any?

first_aggregate_id = @latest_entities.first.aggregate_id

return unless @latest_entities.all? { |entity| entity.aggregate_id == first_aggregate_id }

redirect_to model_entity_path(@model_name, first_aggregate_id)
end
end
end
16 changes: 2 additions & 14 deletions app/views/eventsimple/shared/_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
<!-- Header Search Start -->
<header class="container mb-4">
<div class="row mb-3">
<% if @model_name %>
<div class="col col-sm-10">
<%= form_with url: model_entity_path(@model_name, ''), id: 'model-search' do |f| %>
<%= f.search_field :event_id, class: 'form-control', placeholder: 'Go to event by identifier', value: @aggregate_id, aria: { label: 'Go to event by identifier' } %>
</div>
<div class="col col-sm-2">
<%= f.submit 'Search', class: 'btn btn-primary' %>
</div>
<% end %>
<% end %>
</div>
<% if @model_name && !@aggregate_id && @filters&.any? %>
<header class="container mb-4">
<% if @model_name && !@aggregate_id%>
<%= form_with scope: :filters, url: model_path(@model_name), method: :get, id: 'model-filter' do |f| %>
<div class="row">
<label for="filters_keys[keys]" class="col-sm-2 col-form-label">Filter attribute</label>
Expand Down
6 changes: 0 additions & 6 deletions app/views/layouts/eventsimple/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@
query.set(param, value);
link.attr("href",window.location.pathname + '?' + query.toString());
});

$('#model-search').on('submit', (event) => {
event.preventDefault();
const form = $(event.currentTarget);
window.location = form.attr('action') + form.children('#event_id').val();
});
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion lib/eventsimple/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def drives_events_for(aggregate_klass, aggregate_id:, events_namespace: nil, fil
self._on_invalid_transition = ->(error) { raise error }

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

self.inheritance_column = :type
self.store_full_sti_class = false
Expand Down

0 comments on commit 3328fc2

Please sign in to comment.