Skip to content

Commit

Permalink
Allow for namespaced models
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCornthwaiteKatalyst committed Dec 5, 2023
1 parent 7353009 commit fabd9f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/generators/koi/active_record/active_record_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,21 +23,21 @@ 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
end
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
Expand Down
19 changes: 19 additions & 0 deletions spec/generator/koi/admin_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fabd9f9

Please sign in to comment.