Skip to content

Commit

Permalink
Add diff view to lookup results #301
Browse files Browse the repository at this point in the history
Just like with regular value display from a single file this
now checks if a node is from a different environment than
the selected one.

If it is, the same tabbed view with a diff is presented,
showing the lookup result in the selected environment and
the diff to the lookup in the original environment.
  • Loading branch information
oneiros committed Mar 6, 2024
1 parent 32ed20f commit 8e7b4f4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
6 changes: 6 additions & 0 deletions app/controllers/lookups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class LookupsController < ApplicationController

def show
@result = @key.lookup(@node)
lookup_in_original_environment if @node.environment != @environment
rescue Hdm::Error => e
@error = e

Expand All @@ -19,4 +20,9 @@ def load_keys
@key = Key.new(environment: @environment, name: params[:key_id])
authorize! :show, @key
end

def lookup_in_original_environment
original_key = Key.new(environment: @node.environment, name: params[:key_id])
@original_result = original_key.lookup(@node)
end
end
21 changes: 3 additions & 18 deletions app/views/keys/_value.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
<% if file.has_differing_value_in_original_environment?(@key) %>
<ul class="nav nav-tabs mb-2" role="tablist">
<li class="nav-item" role="presentation">
<button id="<%= "tab-value-#{dom_id(file)}" %>" class="nav-link active" data-bs-target="<%= "#value-#{dom_id(file)}" %>" data-bs-toggle="tab" type="button" role="tab" aria-controls="<%= "value-#{dom_id(file)}" %>" aria-selected="true">Value</button>
</li>
<li class="nav-item" role="presentation">
<button id="<%= "tab-diff-#{dom_id(file)}" %>" class="nav-link" data-bs-target="<%= "#diff-#{dom_id(file)}" %>" data-bs-toggle="tab" type="button" role="tab" aria-controls="<%= "diff-#{dom_id(file)}" %>" aria-selected="false">Diff</button>
</li>
</ul>

<div class="tab-content">
<div id="<%= "value-#{dom_id(file)}" %>" class="tab-pane fade show active" role="tabpanel" aria-labelledby="<%= "tab-value-#{dom_id(file)}" %>">
<%= render "form", file: file, value: value %>
</div>

<div id="<%= "diff-#{dom_id(file)}" %>" class="tab-pane fade" role="tabpanel" aria-labelledby="<%= "tab-diff-#{dom_id(file)}" %>">
<%= render "diff", file: file, value: value %>
</div>
</div>
<%= render "shared/tabbed_diff", dom_id: dom_id(file), original_value:file.value_from_original_environment(key: @key).value, current_value: value.value do %>
<%= render "form", file: file, value: value %>
<% end %>
<% else %>
<%= render "form", file: file, value: value %>
<% end %>
10 changes: 9 additions & 1 deletion app/views/lookups/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<%= turbo_frame_tag "lookup-result-frame" do %>
<code><pre><%= HieraData::Util.yaml_format(@result) %></pre></code>
<% if @original_result && @original_result != @result %>
<%= render "shared/tabbed_diff", dom_id: "lookup-#{dom_id(@key)}", original_value: @original_result, current_value: @result do %>
<div class="p-2">
<code><pre><%= HieraData::Util.yaml_format(@result) %></pre></code>
</div>
<% end %>
<% else %>
<code><pre><%= HieraData::Util.yaml_format(@result) %></pre></code>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% split_diff = Diffy::SplitDiff.new(file.value_from_original_environment(key: @key).value, value.value, format: :html) %>
<% split_diff = Diffy::SplitDiff.new(original_value, current_value, format: :html) %>
<table class="table">
<thead>
<tr>
Expand Down
18 changes: 18 additions & 0 deletions app/views/shared/_tabbed_diff.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<ul class="nav nav-tabs mb-2" role="tablist">
<li class="nav-item" role="presentation">
<button id="<%= "tab-value-#{dom_id}" %>" class="nav-link active" data-bs-target="<%= "#value-#{dom_id}" %>" data-bs-toggle="tab" type="button" role="tab" aria-controls="<%= "value-#{dom_id}" %>" aria-selected="true">Value</button>
</li>
<li class="nav-item" role="presentation">
<button id="<%= "tab-diff-#{dom_id}" %>" class="nav-link" data-bs-target="<%= "#diff-#{dom_id}" %>" data-bs-toggle="tab" type="button" role="tab" aria-controls="<%= "diff-#{dom_id}" %>" aria-selected="false">Diff</button>
</li>
</ul>

<div class="tab-content">
<div id="<%= "value-#{dom_id}" %>" class="tab-pane fade show active" role="tabpanel" aria-labelledby="<%= "tab-value-#{dom_id}" %>">
<%= yield %>
</div>

<div id="<%= "diff-#{dom_id}" %>" class="tab-pane fade" role="tabpanel" aria-labelledby="<%= "tab-diff-#{dom_id}" %>">
<%= render "shared/diff", current_value:, original_value: %>
</div>
</div>

0 comments on commit 8e7b4f4

Please sign in to comment.