Skip to content

Commit

Permalink
Fixes #37059 - Templates - cloned_from reference
Browse files Browse the repository at this point in the history
Keep reference to the original template in cloned templates.
  • Loading branch information
stejskalleos authored and ShimShtein committed Jul 2, 2024
1 parent aca4023 commit e685987
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/controllers/api/v2/provisioning_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def build_pxe_default
param_group :provisioning_template_clone, :as => :create

def clone
original = @provisioning_template
@provisioning_template = @provisioning_template.clone
@provisioning_template.cloned_from = original
load_vars_from_template
@provisioning_template.name = params[:provisioning_template][:name]
process_response @provisioning_template.save
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/api/v2/report_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ def destroy
param_group :report_template_clone, :as => :create

def clone
@report_template = @report_template.dup
original = @report_template

@report_template = original.dup
@report_template.name = params[:report_template][:name]
@report_template.cloned_from = original

process_response @report_template.save
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def add_template_params_filter(filter)
:template,
:template_kind, :template_kind_id, :template_kind_name,
:vendor,
:cloned_from_id,
:template_inputs_attributes => [template_input_params_filter]
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def new
# parent classes (so all controllers don't have actions like id, clone, dup, ...), unfortunatelly they don't
# detect method definitions in controller ancestors, only methods defined directly in child controller
def clone_template
original = @template
@template = @template.dup
@template.cloned_from = original
@template.name += ' clone'
@template.locked = false
load_vars_from_template
Expand Down
1 change: 1 addition & 0 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Template < ApplicationRecord
attr_accessor :modify_locked, :modify_default

has_many :template_inputs, :dependent => :destroy, :foreign_key => 'template_id', :autosave => true
belongs_to :cloned_from, class_name: 'Template'

accepts_nested_attributes_for :template_inputs, :allow_destroy => true

Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v2/provisioning_templates/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ object @provisioning_template

extends "api/v2/provisioning_templates/main"

attributes :template, :locked
attributes :template, :locked, :cloned_from_id

child :template_combinations do
extends "api/v2/template_combinations/base"
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v2/report_templates/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ object @report_template

extends "api/v2/report_templates/main"

attributes :template, :default, :snippet, :locked
attributes :template, :default, :snippet, :locked, :cloned_from_id

node do |report_template|
partial("api/v2/taxonomies/children_nodes", :object => report_template)
Expand Down
12 changes: 12 additions & 0 deletions app/views/templates/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
</div>
</div>

<% if @template.cloned_from %>
<div class="clearfix">
<div class="form-group ">
<%= label_tag :cloned_from, _('Cloned from'), class: 'col-md-2 control-label' %>
<div class="col-md-10">
<%= link_to @template.cloned_from.name, url_for(template_hash_for_member(@template.cloned_from, 'edit')), target: '_blank' %>
</div>
</div>
</div>
<% end %>

<%= textarea_f f, :description, :size => 'col-md-10', :rows => 3, :label => _('Description'), :disabled => @template.locked? %>

<%= textarea_f f, :audit_comment, :size => "col-md-10", :rows => 3, :label => _("Audit Comment"),
Expand Down Expand Up @@ -199,4 +210,5 @@

<%= submit_or_cancel f, false, :cancel_path => template_path_for(@template.class) %>
</div>
<%= f.hidden_field :cloned_from_id %>
<% end %>
5 changes: 5 additions & 0 deletions db/migrate/20231115151349_add_cloned_from_to_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddClonedFromToTemplates < ActiveRecord::Migration[6.1]
def change
add_reference(:templates, :cloned_from, foreign_key: { to_table: :templates, on_delete: :nullify })
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Api::V2::ProvisioningTemplatesControllerTest < ActionController::TestCase
template = ActiveSupport::JSON.decode(@response.body)
assert_equal(template['name'], 'MyClone')
assert_equal(template['template'], original_provisioning_template.template)
assert_equal(template['cloned_from_id'], original_provisioning_template.id)
end

test 'clone name should not be blank' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def setup
template = ActiveSupport::JSON.decode(@response.body)
assert_equal(template['name'], 'MyClone')
assert_equal(template['template'], original_report_template.template)
assert_equal(template['cloned_from_id'], original_report_template.id)
refute_equal(template['id'], original_report_template.id)
end

Expand Down

0 comments on commit e685987

Please sign in to comment.