Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generation of second/subsequent resources in an extension #3372

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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|
Expand Down Expand Up @@ -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
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