Skip to content

Commit

Permalink
CustomToolbar: for plural case the record class needs to be taken fro…
Browse files Browse the repository at this point in the history
…m the list being displayed.
  • Loading branch information
martinpovolny committed Apr 12, 2017
1 parent 0a95184 commit 7816308
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/controllers/vm_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,11 @@ def get_node_info(treenodeid)
end
# Add adv search filter to header
@right_cell_text += @edit[:adv_search_applied][:text] if @edit && @edit[:adv_search_applied]

# save model being displayed for custom buttons
@tree_selected_model = if model.present?
model.constantize
end
end

if @edit && @edit.fetch_path(:adv_search_applied, :qs_exp) # If qs is active, save it in history
Expand Down
14 changes: 11 additions & 3 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ def controller
@view_context.respond_to?(:controller) ? @view_context.controller : @view_context
end

def model_for_custom_toolbar
controller.instance_eval { @tree_selected_model } || controller.class.model
end

# According to toolbar name in parameter `toolbar_name` either returns class
# for generic toolbar, or starts building custom toolbar
def toolbar_class(toolbar_name)
if Mixins::CustomButtons::Result === toolbar_name
model = @record ? @record.class : controller.class.model
custom_toolbar_class(model, @record, toolbar_name)
custom_toolbar_class(toolbar_name)
else
predefined_toolbar_class(toolbar_name)
end
Expand Down Expand Up @@ -280,7 +283,12 @@ def custom_button_selects(model, record, toolbar_result)
end
end

def custom_toolbar_class(model, record, toolbar_result)
def custom_toolbar_class(toolbar_result)
model = @record ? @record.class : model_for_custom_toolbar
build_custom_toolbar_class(model, @record, toolbar_result)
end

def build_custom_toolbar_class(model, record, toolbar_result)
# each custom toolbar is an anonymous subclass of this class

toolbar = Class.new(ApplicationHelper::Toolbar::Basic)
Expand Down
8 changes: 4 additions & 4 deletions spec/helpers/application_helper/toolbar_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
expect(toolbar_builder.custom_button_selects(subject.class, subject, Mixins::CustomButtons::Result.new(:single))).to be_blank
end

it ("#custom_toolbar_class") do
expect(toolbar_builder.custom_toolbar_class(subject.class, subject, Mixins::CustomButtons::Result.new(:single)).definition).to be_blank
it ("#build_custom_toolbar_class") do
expect(toolbar_builder.build_custom_toolbar_class(subject.class, subject, Mixins::CustomButtons::Result.new(:single)).definition).to be_blank
end

it("#record_to_service_buttons") { expect(toolbar_builder.record_to_service_buttons(subject)).to be_blank }
Expand Down Expand Up @@ -93,7 +93,7 @@
expect(result).to eq([:name => name, :items => items])
end

it "#custom_toolbar_class" do
it "#build_custom_toolbar_class" do
escaped_button1_text = CGI.escapeHTML(@button1.name.to_s)
button1 = {
:id => "custom__custom_#{@button1.id}",
Expand All @@ -117,7 +117,7 @@
:items => button_set_item1_items
}
group_name = "custom_buttons_#{@button_set.name}"
expect(toolbar_builder.custom_toolbar_class(subject.class, subject, Mixins::CustomButtons::Result.new(:single)).definition[group_name].buttons).to eq([button_set_item1])
expect(toolbar_builder.build_custom_toolbar_class(subject.class, subject, Mixins::CustomButtons::Result.new(:single)).definition[group_name].buttons).to eq([button_set_item1])
end
end

Expand Down

0 comments on commit 7816308

Please sign in to comment.