Skip to content

Commit

Permalink
correctly check ApplicationRecord is exist in moutable engine
Browse files Browse the repository at this point in the history
Follow up to 1813b29
  • Loading branch information
y-yagi committed Feb 24, 2016
1 parent 2c02bc0 commit 9614f93
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Expand Up @@ -41,8 +41,8 @@ def attributes_with_index

# FIXME: Change this file to a symlink once RubyGems 2.5.0 is required.
def generate_application_record
if self.behavior == :invoke && !File.exist?('app/models/application_record.rb')
template 'application_record.rb', 'app/models/application_record.rb'
if self.behavior == :invoke && !application_record_exist?
template 'application_record.rb', application_record_file_name
end
end

Expand All @@ -51,18 +51,22 @@ def parent_class_name
options[:parent] || determine_default_parent_class
end

def determine_default_parent_class
application_record = nil
def application_record_exist?
file_exist = nil
in_root { file_exist = File.exist?(application_record_file_name) }
file_exist
end

in_root do
application_record = if mountable_engine?
File.exist?("app/models/#{namespaced_path}/application_record.rb")
else
File.exist?('app/models/application_record.rb')
end
def application_record_file_name
@application_record_file_name ||= if mountable_engine?
"app/models/#{namespaced_path}/application_record.rb"
else
'app/models/application_record.rb'
end
end

if application_record
def determine_default_parent_class
if application_record_exist?
"ApplicationRecord"
else
"ActiveRecord::Base"
Expand Down
@@ -1,3 +1,5 @@
<% module_namespacing do -%>
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
<% end -%>
14 changes: 14 additions & 0 deletions railties/test/generators/plugin_generator_test.rb
Expand Up @@ -642,6 +642,20 @@ def test_model_with_existent_application_record_in_mountable_engine
assert_file "app/models/bukkits/article.rb", /class Article < ApplicationRecord/
end

def test_generate_application_record_when_does_not_exist_in_mountable_engine
run_generator [destination_root, '--mountable']
FileUtils.rm "#{destination_root}/app/models/bukkits/application_record.rb"
capture(:stdout) do
`#{destination_root}/bin/rails g model article`
end

assert_file "#{destination_root}/app/models/bukkits/application_record.rb" do |record|
assert_match(/module Bukkits/, record)
assert_match(/class ApplicationRecord < ActiveRecord::Base/, record)
assert_match(/self.abstract_class = true/, record)
end
end

def test_after_bundle_callback
path = 'http://example.org/rails_template'
template = %{ after_bundle { run 'echo ran after_bundle' } }
Expand Down

0 comments on commit 9614f93

Please sign in to comment.