Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default and null info on hover #494

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rails/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,29 @@ def generate_column_content(name)
) if schema_file

@response_builder.push(
model[:columns].map do |name, type|
model[:columns].map do |name, type, default_value, nullable|
primary_key_suffix = " (PK)" if model[:primary_keys].include?(name)
"**#{name}**: #{type}#{primary_key_suffix}\n"
suffixes = []
suffixes << "default: #{format_default(default_value, type)}" if default_value
suffixes << "not null" unless nullable || primary_key_suffix.present?
suffix_string = " - #{suffixes.join(" - ")}" if suffixes.any?
"**#{name}**: #{type}#{primary_key_suffix}#{suffix_string}\n"
end.join("\n"),
category: :documentation,
)
end

sig { params(default_value: String, type: String).returns(String) }
def format_default(default_value, type)
case type
when "boolean"
default_value == "true" ? "true" : "false"
when "string"
default_value.inspect
else
default_value
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def resolve_database_info_from_model(model_name)

info = {
result: {
columns: const.columns.map { |column| [column.name, column.type] },
columns: const.columns.map { |column| [column.name, column.type, column.default, column.null] },
primary_keys: Array(const.primary_key),
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddDefaultsToUser < ActiveRecord::Migration[7.1]
def change
change_column_default :users, :age, from: nil, to: 0
add_column :users, :active, :boolean, default: true, null: false
change_column_default :users, :first_name, from: nil, to: ""
end
end
7 changes: 4 additions & 3 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_05_21_183200) do
ActiveRecord::Schema[7.1].define(version: 2024_10_25_225348) do
create_table "composite_primary_keys", primary_key: ["order_id", "product_id"], force: :cascade do |t|
t.integer "order_id"
t.integer "product_id"
Expand Down Expand Up @@ -48,12 +48,13 @@
end

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "first_name", default: ""
t.string "last_name"
t.integer "age"
t.integer "age", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "country_id", null: false
t.boolean "active", default: true, null: false
t.index ["country_id"], name: "index_users_on_country_id"
end

Expand Down
58 changes: 35 additions & 23 deletions test/ruby_lsp_rails/hover_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class HoverTest < ActiveSupport::TestCase
expected_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer"],
["first_name", "string"],
["last_name", "string"],
["age", "integer"],
["created_at", "datetime"],
["updated_at", "datetime"],
["id", "integer", nil, false],
["first_name", "string", "", true],
["last_name", "string", nil, true],
["age", "integer", "0", true],
["created_at", "datetime", nil, false],
["updated_at", "datetime", nil, false],
["country_id", "integer", nil, false],
["active", "boolean", "true", false],
],
primary_keys: ["id"],
}
Expand All @@ -41,28 +43,34 @@ class User < ApplicationRecord
**id**: integer (PK)
**first_name**: string
**first_name**: string - default: ""
**last_name**: string
**age**: integer
**age**: integer - default: 0
**created_at**: datetime - not null
**updated_at**: datetime - not null
**created_at**: datetime
**country_id**: integer - not null
**updated_at**: datetime
**active**: boolean - default: true - not null
CONTENT
end

test "return column information for namespaced models" do
expected_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer"],
["first_name", "string"],
["last_name", "string"],
["age", "integer"],
["created_at", "datetime"],
["updated_at", "datetime"],
["id", "integer", nil, false],
["first_name", "string", "", true],
["last_name", "string", nil, true],
["age", "integer", "0", true],
["created_at", "datetime", nil, false],
["updated_at", "datetime", nil, false],
["country_id", "integer", nil, false],
["active", "boolean", "true", false],
],
primary_keys: ["id"],
}
Expand Down Expand Up @@ -90,15 +98,19 @@ class User < ApplicationRecord
**id**: integer (PK)
**first_name**: string
**first_name**: string - default: ""
**last_name**: string
**age**: integer
**age**: integer - default: 0
**created_at**: datetime - not null
**updated_at**: datetime - not null
**created_at**: datetime
**country_id**: integer - not null
**updated_at**: datetime
**active**: boolean - default: true - not null
CONTENT
end

Expand Down Expand Up @@ -138,11 +150,11 @@ class CompositePrimaryKey < ApplicationRecord
**product_id**: integer (PK)
**note**: string
**note**: string - not null
**created_at**: datetime
**created_at**: datetime - not null
**updated_at**: datetime
**updated_at**: datetime - not null
CONTENT
end

Expand Down
15 changes: 8 additions & 7 deletions test/ruby_lsp_rails/runner_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ class RunnerClientTest < ActiveSupport::TestCase
test "#model returns information for the requested model" do
# These columns are from the schema in the dummy app: test/dummy/db/schema.rb
columns = [
["id", "integer"],
["first_name", "string"],
["last_name", "string"],
["age", "integer"],
["created_at", "datetime"],
["updated_at", "datetime"],
["country_id", "integer"],
["id", "integer", nil, false],
["first_name", "string", "", true],
["last_name", "string", nil, true],
["age", "integer", "0", true],
["created_at", "datetime", nil, false],
["updated_at", "datetime", nil, false],
["country_id", "integer", nil, false],
["active", "boolean", "1", false],
damienlethiec marked this conversation as resolved.
Show resolved Hide resolved
]
response = T.must(@client.model("User"))
assert_equal(columns, response.fetch(:columns))
Expand Down
Loading