diff --git a/core/lib/generators/refinery/engine/engine_generator.rb b/core/lib/generators/refinery/engine/engine_generator.rb index b326b99b82..a34949790d 100644 --- a/core/lib/generators/refinery/engine/engine_generator.rb +++ b/core/lib/generators/refinery/engine/engine_generator.rb @@ -43,5 +43,6 @@ def reject_file?(file) def in_frontend_directory?(file) file.to_s.include?('app') && file.to_s.scan(/admin|models|mailers/).empty? end + end end diff --git a/core/lib/generators/refinery/engine/templates/app/controllers/refinery/namespace/plural_name_controller.rb.erb b/core/lib/generators/refinery/engine/templates/app/controllers/refinery/namespace/plural_name_controller.rb.erb index 42e614761b..c71cfa6a5f 100644 --- a/core/lib/generators/refinery/engine/templates/app/controllers/refinery/namespace/plural_name_controller.rb.erb +++ b/core/lib/generators/refinery/engine/templates/app/controllers/refinery/namespace/plural_name_controller.rb.erb @@ -26,7 +26,7 @@ module Refinery end def find_page - @page = ::Refinery::Page.where(:link_url => "/<%= plural_name %>").first + @page = ::Refinery::Page.where(link_url: "<%= index_route %>").first end end diff --git a/core/lib/refinery/extension_generation.rb b/core/lib/refinery/extension_generation.rb index c7b6207571..742804aa9e 100644 --- a/core/lib/refinery/extension_generation.rb +++ b/core/lib/refinery/extension_generation.rb @@ -333,6 +333,14 @@ def substitute_path_placeholders(path) gsub('namespace', namespacing.underscore) end + def index_route + if namespacing.underscore == plural_name + '/' + plural_name + else + '/' + namespacing.underscore + '/' + plural_name + end + end + def viable_templates @viable_templates ||= begin all_templates.reject(&method(:reject_template?)).inject({}) do |hash, path| @@ -393,8 +401,10 @@ def templated_merge! end end + # merge_rb is only used for merging routes.rb + # Put destination lines first, so that extension namespaced routes precede the default extension route def merge_rb - (source_lines[0..-2] + destination_lines[1..-2] + [source_lines.last]).join "\n" + (destination_lines[0..-2] + source_lines[1..-2] + [destination_lines.last]).join "\n" end def merge_yaml diff --git a/core/spec/lib/generators/refinery/engine/engine_generator_multiple_resources_spec.rb b/core/spec/lib/generators/refinery/engine/engine_generator_multiple_resources_spec.rb index 4d63c0549c..f79f79489d 100644 --- a/core/spec/lib/generators/refinery/engine/engine_generator_multiple_resources_spec.rb +++ b/core/spec/lib/generators/refinery/engine/engine_generator_multiple_resources_spec.rb @@ -7,6 +7,7 @@ module Refinery describe EngineGenerator do include GeneratorSpec::TestCase destination File.expand_path("../../../../../../tmp", __FILE__) + let(:extension_root){"#{destination_root}/vendor/extensions/rspec_product_tests"} before do prepare_destination @@ -41,6 +42,13 @@ module Refinery end } end + + it "does not namespace the link_url in the extension controller" do + File.read("#{extension_root}/app/controllers/refinery/rspec_product_tests/rspec_product_tests_controller.rb") do |file| + expect(file.grep(%r{/:link_url => "/rspec_product_tests"\)\.first/})).to be_truthy + end + end + end context "when generating a resource inside existing extensions dir" do @@ -66,16 +74,29 @@ module Refinery end it "appends existing seeds file" do - File.open("#{destination_root}/vendor/extensions/rspec_product_tests/db/seeds.rb") do |file| + File.open("#{extension_root}/db/seeds.rb") do |file| expect(file.grep(%r{/rspec_product_tests|/rspec_item_tests}).count).to eq(2) end end it "appends routes to the routes file" do - File.open("#{destination_root}/vendor/extensions/rspec_product_tests/config/routes.rb") do |file| + File.open("#{extension_root}/config/routes.rb") do |file| expect(file.grep(%r{rspec_item_tests}).count).to eq(2) end end + + it "places second and subsequent routes before the primary extension routes" do + content = File.read("#{extension_root}/config/routes.rb") + item_front_end_route = content.index("resources :rspec_item_tests, :only => [:index, :show]") + product_front_end_route = content.index("resources :rspec_product_tests, :path => '', :only => [:index, :show]") + expect(item_front_end_route).to be < product_front_end_route + end + + it "uses the namespaced url in the extension controller" do + content = File.read("#{extension_root}/app/controllers/refinery/rspec_product_tests/rspec_item_tests_controller.rb") do |file| + expect(file.grep(%r{/:link_url => "/rspec_product_tests/rspec_item_tests"\)\.first/})).to be_truthy + end + end end end end