Skip to content

Commit

Permalink
341617 - Add ChangelogEntry to show pages (nanego#210)
Browse files Browse the repository at this point in the history
* Added changelog entries for Stack

* Made a helper to generate paths

* Implemented changelog entries for modeles from site down to air conditioners

* wip

* used turbo frame tag helper

* Improved helper

* wip

* wip

* fix

* wip

* style: code cleanup

* add changelog on other show

* style: cleanups

---------

Co-authored-by: Maximilien Bausson <[email protected]>
Co-authored-by: Nicolas Brousse <[email protected]>
  • Loading branch information
3 people authored Oct 2, 2024
1 parent ecb45f3 commit 8e18f28
Show file tree
Hide file tree
Showing 30 changed files with 158 additions and 42 deletions.
3 changes: 1 addition & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,8 @@ GEM
thread_safe (0.3.6)
tilt (2.4.0)
timeout (0.4.1)
turbo-rails (2.0.6)
turbo-rails (2.0.8)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand Down
31 changes: 31 additions & 0 deletions app/components/ChangelogEntries/object_list_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module ChangelogEntries
class ObjectListComponent < ApplicationComponent
include Turbo::FramesHelper

erb_template <<~ERB
<%= turbo_frame_tag("changelog-entries", loading: :lazy, src: object_changelog_path, **@html_options) do %>
<div class="w-100 d-flex justify-content-center align-items-center gap-3">
<span><%= t("changelog_entries.object_list_component.loading") %></span>
<span class="spinner-border spinner-border-sm p-4"></span>
</div>
<% end %>
ERB

def initialize(object, **html_options)
@object = object
@html_options = html_options

raise ArgumentError, "@object could not be nil" unless @object

super()
end

private

def object_changelog_path
object_changelog_entries_path(@object.model_name.plural, @object.id)
end
end
end
6 changes: 5 additions & 1 deletion app/controllers/changelog_entries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ChangelogEntriesController < ApplicationController
def index
@changelog_entries = sorted changelog_scope.order(created_at: :desc).page(params[:page]).per(100)
@changelog_entries = sorted changelog_scope.order(created_at: :desc).page(params[:page]).per(per_page)
end

def show
Expand Down Expand Up @@ -28,4 +28,8 @@ def changelog_scope

ChangelogEntry.all
end

def per_page
@per_page ||= turbo_frame_request? ? 10 : 50
end
end
2 changes: 2 additions & 0 deletions app/views/air_conditioners/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@air_conditioner) %>
2 changes: 2 additions & 0 deletions app/views/architectures/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@architecture) %>
2 changes: 2 additions & 0 deletions app/views/bays/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@bay) %>
2 changes: 2 additions & 0 deletions app/views/card_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@card_type) %>
2 changes: 2 additions & 0 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@category) %>
87 changes: 49 additions & 38 deletions app/views/changelog_entries/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
<% provide(:title, t("changelog_entries.title")) %>
<%= render "layouts/breadcrumb", breadcrumb_steps: {
<%
provide(:title, t("changelog_entries.title"))

breadcrumb = {}

if @scoped_object
breadcrumb[@scoped_object.model_name.human.pluralize] = polymorphic_path(@scoped_object.class)
breadcrumb[@scoped_object] = polymorphic_path(@scoped_object)
end
%>
<%= render "layouts/breadcrumb", breadcrumb_steps: breadcrumb.merge({
t("changelog_entries.title") => ""
} %>
}) %>

<div class="container-fluid">
<h1><%= t("changelog_entries.title") %></h1>
<%= turbo_frame_tag "changelog-entries" do %>
<div class="container-fluid">
<h1><%= t("changelog_entries.title") %></h1>

<%= render List::DataTableComponent.new(decorate(@changelog_entries)) do |table| %>
<%= render List::DataTableComponent.new(decorate(@changelog_entries)) do |table| %>

<% table.with_column(ChangelogEntry.human_attribute_name(:action), sort_by: :action) do |changelog_entry| %>
<%= render changelog_entry.action_label_to_component %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:action), sort_by: :action) do |changelog_entry| %>
<%= render changelog_entry.action_label_to_component %>
<% end %>

<% table.with_column(ChangelogEntry.human_attribute_name(:author), sort_by: :author_id) do |changelog_entry| %>
<%= link_to_if changelog_entry.author_id?, changelog_entry.author_display_name, changelog_entry.author %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:author), sort_by: :author_id) do |changelog_entry| %>
<%= link_to_if changelog_entry.author_id?, changelog_entry.author_display_name, changelog_entry.author, target: :_top %>
<% end %>

<% table.with_column(ChangelogEntry.human_attribute_name(:object), sort_by: :object_id) do |changelog_entry| %>
<% begin %>
<%= link_to changelog_entry.object_display_name, changelog_entry.object %>
<% rescue NoMethodError %>
<%= changelog_entry.object_display_name %>
<% table.with_column(ChangelogEntry.human_attribute_name(:object), sort_by: :object_id) do |changelog_entry| %>
<% begin %>
<%= link_to changelog_entry.object_display_name, changelog_entry.object, target: :_top %>
<% rescue NoMethodError %>
<%= changelog_entry.object_display_name %>
<% end %>
<% end %>
<% end %>

<% table.with_column(ChangelogEntry.human_attribute_name(:attributes)) do |changelog_entry| %>
<div class="row">
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.left %>
</div>
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.right %>
<% table.with_column(ChangelogEntry.human_attribute_name(:attributes)) do |changelog_entry| %>
<div class="row">
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.left %>
</div>
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.right %>
</div>
</div>
</div>
<% end %>
<% end %>

<% table.with_column(t("label_date"), sort_by: :created_at) do |changelog_entry| %>
<%= l changelog_entry.created_at %>
<% end %>
<% table.with_column(t("label_date"), sort_by: :created_at) do |changelog_entry| %>
<%= l changelog_entry.created_at %>
<% end %>

<% table.with_column(t("label_details")) do |changelog_entry| %>
<%= link_to changelog_entry_path(changelog_entry), class: "btn btn-primary" do %>
<span class="bi bi-eye" aria-hidden="true" data-controller="tooltip" data-bs-placement="left"
title="<%= t("action.show") %>"></span>
<span class="visually-hidden"><%= t("action.show") %></span>
<% table.with_column(t("label_details")) do |changelog_entry| %>
<%= link_to changelog_entry_path(changelog_entry), class: "btn btn-primary", target: :_top do %>
<span class="bi bi-eye" aria-hidden="true" data-controller="tooltip" data-bs-placement="left"
title="<%= t("action.show") %>"></span>
<span class="visually-hidden"><%= t("action.show") %></span>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>

<%= paginate @changelog_entries %>
<%= paginate @changelog_entries %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/clusters/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
</ul>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@cluster) %>
2 changes: 2 additions & 0 deletions app/views/colors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@color) %>
2 changes: 2 additions & 0 deletions app/views/contract_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@contract_type) %>
2 changes: 2 additions & 0 deletions app/views/disk_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@disk_type) %>
2 changes: 2 additions & 0 deletions app/views/domaines/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
</ul>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@domaine) %>
2 changes: 2 additions & 0 deletions app/views/frames/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@frame) %>
2 changes: 2 additions & 0 deletions app/views/gestions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
</ul>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@gestion) %>
2 changes: 2 additions & 0 deletions app/views/islets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@islet) %>
2 changes: 2 additions & 0 deletions app/views/maintainers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@maintainer) %>
2 changes: 2 additions & 0 deletions app/views/manufacturers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@manufacturer) %>
2 changes: 2 additions & 0 deletions app/views/memory_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@memory_type) %>
2 changes: 2 additions & 0 deletions app/views/modeles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@
</ul>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@modele) %>
2 changes: 2 additions & 0 deletions app/views/port_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@port_type) %>
2 changes: 2 additions & 0 deletions app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@room) %>
2 changes: 2 additions & 0 deletions app/views/servers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,6 @@
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@server) %>

<%= render "ports/modal_edit_port" %>
2 changes: 2 additions & 0 deletions app/views/sites/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@site) %>
2 changes: 2 additions & 0 deletions app/views/stacks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
</dl>
</div>
</div>

<%= render ChangelogEntries::ObjectListComponent.new(@stack) %>
3 changes: 3 additions & 0 deletions config/locales/components.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ en:
title:
one: '1 error occured while saving:'
other: '%{count} errors occured while saving:'
changelog_entries:
object_list_component:
loading: Loading changelog...
3 changes: 3 additions & 0 deletions config/locales/components.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ fr:
reset: Réinitialiser%{filters_count}
total_with_filters: "Total: %{results_count} sur %{total_count} résultats"
total: "Total: %{count}"
changelog_entries:
object_list_component:
loading: Chargement de l'historique...
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
get 'search', to: 'search#index'

resources :changelog_entries, only: %i[index show]
get "/:object_type/:object_id/changelog_entries", to: "changelog_entries#index"
get "/:object_type/:object_id/changelog_entries", to: "changelog_entries#index", as: :object_changelog_entries

mount Lookbook::Engine, at: "/lookbook" if Rails.env.development?
end
21 changes: 21 additions & 0 deletions spec/components/changelog_entries/object_list_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe ChangelogEntries::ObjectListComponent, type: :component do
context "with object" do
let(:islet) { islets(:one) }
let(:component) { described_class.new(islet) }
let(:rendered_component) { render_inline(component) }

it "renders component" do # rubocop:disable RSpec/ExampleLength
expect(rendered_component.to_html).to have_tag('turbo-frame', with: {
id: "changelog-entries",
src: "/islets/#{islet.id}/changelog_entries"
}) do
with_text(I18n.t("changelog_entries.object_list_component.loading"))
with_tag("span", with: { class: "spinner-border" })
end
end
end
end

0 comments on commit 8e18f28

Please sign in to comment.