Skip to content

Commit

Permalink
render dummy data on page
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Sep 30, 2024
1 parent a1a0523 commit ca1c9c0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 38 deletions.
41 changes: 41 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Event
attr_accessor :id

def self.find(id)
new(
id: id,
name: 'Stubbed Event',
description: 'This is a stubbed event for testing purposes.',
created_at: Time.now.utc,
header: {
'user' => { 'id' => 1, 'name' => 'Alice', 'email' => '[email protected]' },
'location' => { 'city' => 'New York', 'coordinates' => { 'lat' => 40.7128, 'lon' => -74.0060 } }
},
body: {
'title' => 'Event Title',
'description' => 'This is a description of the event.',
'attendees' => [{ 'id' => 1, 'name' => 'Bob' }, { 'id' => 2, 'name' => 'Charlie' }]
}
)
end

def initialize(id:, name:, description:, created_at:, header:, body:)
@id = id
@name = name
@description = description
@created_at = created_at
@header = header
@body = body
end

def attributes
{
'id' => @id,
'name' => @name,
'description' => @description,
'created_at' => @created_at,
'header' => @header,
'body' => @body
}
end
end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require 'sidekiq'
require 'sidekiq/web'
require 'outboxer/web'

require_relative 'app/models/event'

use Rack::Session::Cookie, secret: ENV['SESSION_SECRET'], same_site: true, max_age: 86400

map '/outboxer' do
Expand Down
41 changes: 18 additions & 23 deletions lib/outboxer/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,32 +499,27 @@ def normalise_query_string(status: Messages::LIST_STATUS_DEFAULT,
redirect to("/messages#{normalised_query_string}")
end

get '/messageable/*/id' do
begin
messageable_type = params[:splat]
.first.split('/').map(&:singularize).map(&:camelize).join('::')

messageable_class = Object.const_get("::#{messageable_type}")

if messageable_class < ActiveRecord::Base
messageable = messageable_class.find(params[:id])

erb :messageable, locals: {
messageable: messageable }
else
status 400
get '/message/:id/messageable' do
denormalised_query_params = denormalise_query_params(
status: params[:status],
sort: params[:sort],
order: params[:order],
page: params[:page],
per_page: params[:per_page],
time_zone: params[:time_zone])

erb :error, locals: { error: "Invalid model" }
end
rescue NameError
status 400
message = Message.find_by_id(id: params[:id])
messages_metrics = Messages.metrics

erb :error, locals: { error: "Model not found" }
rescue ActiveRecord::RecordNotFound
status 404
messageable_class = Object.const_get("#{message[:messageable_type]}")
messageable = messageable_class.find(message[:messageable_id])

erb :error, locals: { error: "Record not found" }
end
erb :messageable, locals: {
message: message,
messageable: messageable,
messages_metrics: messages_metrics,
denormalised_query_params: denormalised_query_params
}
end
end
end
1 change: 1 addition & 0 deletions lib/outboxer/web/views/message.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</tr>
</tbody>
</table>

<div class="accordion" id="exceptionsAccordion">
<% message[:exceptions].each_with_index do |exception, index| %>
<div class="accordion-item">
Expand Down
40 changes: 25 additions & 15 deletions lib/outboxer/web/views/messageable.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
<h1><%= messageable.class.name %> Details</h1>

<ul>
<% messageable.attributes.each do |key, value| %>
<li><strong><%= key.humanize %>:</strong>
<% if value.is_a?(Hash) || value.is_a?(Array) %>
<pre><%= JSON.pretty_generate(value) %></pre>
<% else %>
<%= value %>
<% end %>
</li>
<% end %>
</ul>

<a href="/">Back to Home</a>
<div class="container mt-4"<div class="container my-4">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h3 class="mb-0"><%= messageable.class.name %> <%= messageable.id %></h3>
</div>
<div class="card-body">
<table class="table">
<tbody>
<% messageable.attributes.each do |key, value| %>
<tr>
<th scope="row"><%= key.humanize %></th>
<td>
<% if value.is_a?(Hash) || value.is_a?(Array) %>
<pre class="mb-0"><%= JSON.pretty_generate(value) %></pre>
<% else %>
<%= value %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>

0 comments on commit ca1c9c0

Please sign in to comment.