Skip to content

Commit

Permalink
Fix boolean generation saving
Browse files Browse the repository at this point in the history
  • Loading branch information
penguoir committed Feb 15, 2024
1 parent 35d59bf commit bff72df
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 16 deletions.
11 changes: 8 additions & 3 deletions lib/active_cortex/generator/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def self.accepts?(record:, field_name:)
record.class.attribute_types[field_name.to_s].type == :boolean
end

def self.generate(record:, field_name:)
def save_generation(record:, field_name:)
record.send("#{field_name}=", generation)
end

Expand All @@ -14,10 +14,13 @@ def generation
private

def convert_to_boolean(content)
content = content.downcase
.gsub(/[^a-z]/, "")

case content
when "Yes", "yes", "True", "true", "1"
when "yes", "true", "1"
true
when "No", "no", "False", "false", "0"
when "no", "false", "0"
false
else
raise ActiveCortex::Error, "Could not convert content to boolean: #{content}"
Expand All @@ -26,6 +29,8 @@ def convert_to_boolean(content)

def openai_content
convert_to_boolean(openai_response["choices"][0]["message"]["content"])
rescue ActiveCortex::Error => e
raise e
rescue
nil
end
Expand Down
7 changes: 7 additions & 0 deletions spec/active_cortex/generator/boolean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
expect(result).to be_a(TrueClass).or be_a(FalseClass)
end
end

context "when saving the generation" do
it "saves the generation to the record", :vcr do
generator.save_generation(record: book, field_name: :completed)
expect(book.completed).to be_a(TrueClass).or be_a(FalseClass)
end
end
end
1 change: 1 addition & 0 deletions spec/dummy/db/migrate/20231026194012_create_books.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def change
create_table :books do |t|
t.string :title, null: false
t.string :summary
t.boolean :completed, default: false

t.timestamps
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
create_table "books", force: :cascade do |t|
t.string "title", null: false
t.string "summary"
t.boolean "completed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bff72df

Please sign in to comment.