Skip to content

Commit 57bf18d

Browse files
authored
fix that table name and file name are correctly named (#107)
* fix that table name and file name are correctly underscored and tableized * standardrb fixes
1 parent 18af74b commit 57bf18d

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

langchainrb_rails.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ Gem::Specification.new do |spec|
3535
spec.add_development_dependency "pry-byebug", "~> 3.10.0"
3636
spec.add_development_dependency "yard", "~> 0.9.34"
3737
spec.add_development_dependency "rails", "> 6.0.0"
38+
spec.add_development_dependency "generator_spec"
3839
end

lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def migration_version
2424
end
2525

2626
def add_to_model
27-
inject_into_class "app/models/#{model_name.downcase}.rb", model_name do
27+
inject_into_class "app/models/#{model_name.underscore}.rb", model_name do
2828
" vectorsearch\n\n after_save :upsert_to_vectorsearch\n\n"
2929
end
3030
end
@@ -44,7 +44,7 @@ def model_name
4444

4545
# @return [String] Table name of the model
4646
def table_name
47-
model_name.downcase.pluralize
47+
model_name.tableize
4848
end
4949

5050
# @return [String] LLM provider to use
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require "spec_helper"
2+
3+
TMP_ROOT = Pathname.new(Dir.mktmpdir("tmp"))
4+
5+
RSpec.describe LangchainrbRails::Generators::PgvectorGenerator, type: :generator do
6+
destination TMP_ROOT
7+
arguments %w[--model=SupportArticle --llm=openai]
8+
9+
before(:all) do
10+
prepare_destination
11+
model_file = TMP_ROOT.join("app/models/support_article.rb")
12+
gemfile = TMP_ROOT.join("Gemfile")
13+
write_file(model_file, "class SupportArticle < ApplicationRecord\nend")
14+
write_file(gemfile, "")
15+
run_generator
16+
end
17+
18+
after(:all) { delete_directory(TMP_ROOT) }
19+
20+
it "creates the initializer file" do
21+
assert_file "config/initializers/langchainrb_rails.rb"
22+
end
23+
24+
it "creates the vector extension migration" do
25+
assert_migration "db/migrate/enable_vector_extension.rb"
26+
end
27+
28+
it "adds the embedding column to the model" do
29+
assert_migration "db/migrate/add_vector_column_to_support_articles.rb" do |migration|
30+
assert_match(/add_column :support_articles, :embedding, :vector/, migration)
31+
end
32+
end
33+
34+
it "adds the vectorsearch module to the model" do
35+
assert_file "app/models/support_article.rb" do |model|
36+
assert_match(/vectorsearch/, model)
37+
assert_match(/after_save :upsert_to_vectorsearch/, model)
38+
end
39+
end
40+
41+
it "adds the necessary gems to the Gemfile" do
42+
assert_file "Gemfile" do |gemfile|
43+
assert_match(/gem "neighbor"/, gemfile)
44+
assert_match(/gem "ruby-openai"/, gemfile)
45+
end
46+
end
47+
end

spec/langchainrb_rails/helpers.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Helpers
2+
module FileSystem
3+
def write_file(file_name, contents)
4+
FileUtils.mkdir_p(File.dirname(file_name)) unless File.directory?(File.dirname(file_name))
5+
::File.write(file_name.to_s, contents)
6+
end
7+
8+
def delete_directory(name)
9+
FileUtils.rm_rf(name)
10+
end
11+
end
12+
end

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
require "rails"
44
require "langchainrb_rails"
5+
require "rails/generators"
6+
require "rails/generators/test_case"
7+
require "generator_spec"
8+
require "langchainrb_rails/helpers"
59

610
RSpec.configure do |config|
711
# Enable flags like --only-failures and --next-failure
@@ -13,4 +17,6 @@
1317
config.expect_with :rspec do |c|
1418
c.syntax = :expect
1519
end
20+
21+
config.include Helpers::FileSystem
1622
end

0 commit comments

Comments
 (0)