diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 186410d452..085267c68c 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -126,12 +126,13 @@ def link_to_delete(record, options = {}) #---------------------------------------------------------------------------- def link_to_discard(object) - if entity.present? - link_to(t(:discard), - url_for(controller: entity.class.to_s.tableize, action: 'discard', id: entity.id, attachment: object.class.name, attachment_id: object.id), - method: :post, - remote: true) - end + current_url = (request.xhr? ? request.referer : request.fullpath) + parent, parent_id = current_url.scan(%r{/(\w+)/(\d+)}).flatten + + link_to(t(:discard), + url_for(controller: parent, action: :discard, id: parent_id, attachment: object.class.name, attachment_id: object.id), + method: :post, + remote: true) end #---------------------------------------------------------------------------- diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 914c67f085..7ae9b24191 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -35,18 +35,13 @@ end end - describe "link_to_discard" do - let(:contact) { create(:contact) } - let(:opportunity) { create(:opportunity) } - before do - contact.opportunities << opportunity - allow(helper).to receive(:entity).and_return(contact) - end - it do - link = helper.link_to_discard(opportunity) - expect(link).to match(%r{contacts/#{contact.id}/discard}) - expect(link).to match(/attachment=Opportunity&attachment_id=#{opportunity.id}/) - end + it "link_to_discard" do + lead = create(:lead) + allow(controller.request).to receive(:fullpath).and_return("http://www.example.com/leads/#{lead.id}") + + link = helper.link_to_discard(lead) + expect(link).to match(%r{leads/#{lead.id}/discard}) + expect(link).to match(/attachment=Lead&attachment_id=#{lead.id}/) end describe "shown_on_landing_page?" do