Skip to content

Commit

Permalink
Merge pull request ManageIQ#4310 from lpichler/save_multiple_tag_cate…
Browse files Browse the repository at this point in the history
…gories_in_chargeback_assignments

Allow to save multiple tag categories in chargeback controller
  • Loading branch information
mzazrivec authored Aug 3, 2018
2 parents 0936737 + 5b77319 commit a296004
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions app/controllers/chargeback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ def cb_rate_set_record_vars
def cb_assign_set_record_vars
if @edit[:new][:cbshow_typ].ends_with?("-tags")
@edit[:set_assignments] = []
@edit[:cb_assign][:tags].each do |id, _tag|
assigned_rates_from_all_categories = @edit[:cb_assign][:tags].values.reduce({}, :merge)
assigned_rates_from_all_categories.each_key do |id|
key = "#{@edit[:new][:cbshow_typ]}__#{id}"
if !@edit[:new][key].nil? && @edit[:new][key] != "nil"
temp = {
Expand Down Expand Up @@ -739,9 +740,10 @@ def get_categories_all
end

def get_tags_all(category)
@edit[:cb_assign][:tags] = {}
@edit[:cb_assign][:tags] ||= {}
@edit[:cb_assign][:tags][category] ||= {}
classification = Classification.find_by_id(category.to_s)
classification.entries.each { |e| @edit[:cb_assign][:tags][e.id.to_s] = e.description } if classification
classification&.entries&.each { |e| @edit[:cb_assign][:tags][category][e.id.to_s] = e.description }
end

DEFAULT_CHARGEBACK_LABELS = ["com.redhat.component"].freeze
Expand Down Expand Up @@ -799,16 +801,16 @@ def get_cis_all
end
end

def cb_assign_params_to_edit(cb_assign_key)
return unless @edit[:cb_assign][cb_assign_key]
def cb_assign_params_to_edit(cb_assign_key, tag_category_id = nil)
current_assingments = cb_assign_key == :tags ? @edit[:cb_assign][cb_assign_key].try(:[], tag_category_id) : @edit[:cb_assign][cb_assign_key]

@edit[:cb_assign][cb_assign_key].each do |id, _ci|
return unless current_assingments
current_assingments.each_key do |id|
key = "#{@edit[:new][:cbshow_typ]}__#{id}"
@edit[:new][key] = params[key].to_s if params[key]
end
end


# Get variables from edit form
def cb_assign_get_form_vars
@edit[:new][:cbshow_typ] = params[:cbshow_typ] if params[:cbshow_typ]
Expand All @@ -819,7 +821,7 @@ def cb_assign_get_form_vars

if @edit[:new][:cbshow_typ].ends_with?("-tags")
get_categories_all
get_tags_all(params[:cbtag_cat]) if params[:cbtag_cat]
get_tags_all(params[:cbtag_cat].to_i) if params[:cbtag_cat]
elsif @edit[:new][:cbshow_typ].ends_with?("-labels")
get_docker_labels_all_keys
get_docker_labels_all_values(params[:cblabel_key]) if params[:cblabel_key] && params[:cblabel_key] != 'null'
Expand All @@ -828,7 +830,7 @@ def cb_assign_get_form_vars
end

cb_assign_params_to_edit(:cis)
cb_assign_params_to_edit(:tags)
cb_assign_params_to_edit(:tags, @edit[:new][:cbtag_cat].try(:to_i))
cb_assign_params_to_edit(:docker_label_values)
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/chargeback/_cb_assignments.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
%th= _('Name')
%th= _('Rate')
%tbody
- @edit[:cb_assign][:tags].invert.sort_by { |a| a.first.downcase }.each do |tag, id|
- @edit[:cb_assign][:tags][@edit[:new][:cbtag_cat].to_i].invert.sort_by { |a| a.first.downcase }.each do |tag, id|
%tr
%td
= h(tag)
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/chargeback_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
it "returns the classification entry record" do
controller.instance_variable_set(:@edit, :cb_assign => {:tags => {}})
controller.send(:get_tags_all, tag.id)
expect(assigns(:edit)[:cb_assign][:tags]).to eq(entry.id.to_s => entry.description)
expect(assigns(:edit)[:cb_assign][:tags][tag.id]).to eq(entry.id.to_s => entry.description)
end

it "returns empty hash when classification entry is not found" do
controller.instance_variable_set(:@edit, :cb_assign => {:tags => {}})
controller.send(:get_tags_all, 1)
expect(assigns(:edit)[:cb_assign][:tags]).to eq({})
expect(assigns(:edit)[:cb_assign][:tags][1]).to eq({})
end
end

Expand Down

0 comments on commit a296004

Please sign in to comment.