Skip to content

Commit

Permalink
Fix generation of second/subsequent resources in an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
anitagraham authored and parndt committed May 25, 2018
1 parent d181fbb commit 0473938
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/lib/generators/refinery/engine/engine_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion core/lib/refinery/extension_generation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,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|
Expand Down Expand Up @@ -400,8 +408,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 0473938

Please sign in to comment.