diff --git a/lib/generators/koi/active_record/active_record_generator.rb b/lib/generators/koi/active_record/active_record_generator.rb index 3590d3d9a..c7c717b05 100644 --- a/lib/generators/koi/active_record/active_record_generator.rb +++ b/lib/generators/koi/active_record/active_record_generator.rb @@ -13,7 +13,7 @@ def admin_search def ordinal_scope return unless attributes.any? { |attr| attr.name == "ordinal" } - insert_into_file "app/models/#{file_name}.rb", before: /end\Z/ do + insert_into_file "app/models/#{file_path}.rb", before: /end\Z/ do <<~RUBY default_scope -> { order(ordinal: :asc) } RUBY @@ -23,13 +23,13 @@ def ordinal_scope private def pg_search - insert_into_file "app/models/#{file_name}.rb", after: "class #{class_name} < ApplicationRecord\n" do + insert_into_file "app/models/#{file_path}.rb", after: "class #{class_name} < ApplicationRecord\n" do <<~RUBY include PgSearch::Model RUBY end - insert_into_file "app/models/#{file_name}.rb", before: /end\Z/ do + insert_into_file "app/models/#{file_path}.rb", before: /end\Z/ do <<~RUBY pg_search_scope :admin_search, against: %i[#{search_fields.join(' ')}], using: { tsearch: { prefix: true } } RUBY @@ -37,7 +37,7 @@ def pg_search end def sql_search - insert_into_file "app/models/#{file_name}.rb", before: /end\Z/ do + insert_into_file "app/models/#{file_path}.rb", before: /end\Z/ do clause = search_fields.map { |f| "#{f} LIKE :query" }.join(" OR ") <<~RUBY scope :admin_search, ->(query) do diff --git a/spec/generator/koi/admin_generator_spec.rb b/spec/generator/koi/admin_generator_spec.rb index 273d731e5..e428619b2 100644 --- a/spec/generator/koi/admin_generator_spec.rb +++ b/spec/generator/koi/admin_generator_spec.rb @@ -24,5 +24,24 @@ it { expect(Pathname.new("#{@dummy}/app/views/admin/tests/new.html.erb")).to exist } it { expect(Pathname.new("#{@dummy}/app/views/admin/tests/show.html.erb")).to exist } it { expect(File.read("#{@dummy}/config/routes/admin.rb")).to include "resources :tests" } + + context "with namespaced modal" do + before(:all) do + @tmpdir = Dir.mktmpdir + FileUtils.cp_r "spec/dummy/", "#{@tmpdir}/" + @dummy = "#{@tmpdir}/dummy" + Dir.chdir(@dummy) do + `bin/rails g koi:admin spaced::test` + end + end + + it { expect(Pathname.new("#{@dummy}/app/models/spaced/test.rb")).to exist } + it { expect(Pathname.new("#{@dummy}/app/controllers/admin/spaced/tests_controller.rb")).to exist } + it { expect(Pathname.new("#{@dummy}/app/views/admin/spaced/tests/index.html.erb")).to exist } + it { expect(Pathname.new("#{@dummy}/app/views/admin/spaced/tests/new.html.erb")).to exist } + it { expect(Pathname.new("#{@dummy}/app/views/admin/spaced/tests/show.html.erb")).to exist } + it { expect(File.read("#{@dummy}/config/routes/admin.rb")).to include "namespace :spaced" } + it { expect(File.read("#{@dummy}/config/routes/admin.rb")).to include "resources :tests" } + end end # rubocop:enable RSpec/InstanceVariable, RSpec/BeforeAfterAll