Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
fix: normalize default values in schema for ruby 4.0 and rails 8 compatibility#252
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| class User < ApplicationRecord | ||
| enum :status, { inactive: 0, active: 1, archived: 2 } | ||
| enum :role, { user: 0, admin: 1, moderator: 2 } | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class AddDefaultsToUsers < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :users, :active, :boolean, default: false | ||
| add_column :users, :verified, :boolean, default: true | ||
| add_column :users, :status, :integer, default: 0 | ||
| add_column :users, :role, :integer, default: 1 | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -37,3 +37,5 @@ | ||
| /db/*.sqlite3 | ||
| /db/*.sqlite3-journal | ||
| /db/*.sqlite3-[0-9]* | ||
| .forestadmin-rpc-schema.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class TestDefault < ApplicationRecord | ||
| enum :status, { inactive: 0, active: 1, archived: 2 } | ||
| enum :priority, { low: 0, medium: 1, high: 2 } | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class CreateTestDefaults < ActiveRecord::Migration[7.0] | ||
| def change | ||
| create_table :test_defaults do |t| | ||
| t.boolean :active, default: false | ||
| t.boolean :verified, default: true | ||
| t.integer :status, default: 0 | ||
| t.integer :priority, default: 1 | ||
| t.string :name | ||
| t.timestamps | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| require 'spec_helper' | ||
| module ForestAdminDatasourceActiveRecord | ||
| module Parser | ||
| describe Column do | ||
| include described_class | ||
| let(:datasource) { Datasource.new({ adapter: 'sqlite3', database: 'db/database.db' }) } | ||
| let(:collection) { ForestAdminDatasourceActiveRecord::Collection.new(datasource, TestDefault) } | ||
| before do | ||
| # Run the migration to create the test_defaults table | ||
| unless ActiveRecord::Base.connection.table_exists?(:test_defaults) | ||
| ActiveRecord::Migration.suppress_messages do | ||
| CreateTestDefaults.migrate(:up) | ||
| end | ||
| end | ||
| end | ||
| describe 'normalize_default_value' do | ||
| context 'with boolean fields' do | ||
| it 'normalizes false default value' do | ||
| column = TestDefault.columns.find { |c| c.name == 'active' } | ||
| result = normalize_default_value(column) | ||
| expect(result).to be(false) | ||
| expect(result).to be_a(FalseClass) | ||
| end | ||
| it 'normalizes true default value' do | ||
| column = TestDefault.columns.find { |c| c.name == 'verified' } | ||
| result = normalize_default_value(column) | ||
| expect(result).to be(true) | ||
| expect(result).to be_a(TrueClass) | ||
| end | ||
| end | ||
| context 'with integer/enum fields' do | ||
| it 'normalizes integer default value to 0' do | ||
| column = TestDefault.columns.find { |c| c.name == 'status' } | ||
| result = normalize_default_value(column) | ||
| expect(result).to eq(0) | ||
| expect(result).to be_a(Integer) | ||
| end | ||
| it 'normalizes integer default value to 1' do | ||
| column = TestDefault.columns.find { |c| c.name == 'priority' } | ||
| result = normalize_default_value(column) | ||
| expect(result).to eq(1) | ||
| expect(result).to be_a(Integer) | ||
| end | ||
| end | ||
| context 'with nil default' do | ||
| it 'returns the original value for string with no default' do | ||
| column = TestDefault.columns.find { |c| c.name == 'name' } | ||
| # name has no default, so column.default is nil | ||
| # In the collection, we check nil before calling normalize | ||
| expect(column.default).to be_nil | ||
| end | ||
| end | ||
| end | ||
| describe 'integration with real columns' do | ||
| it 'handles all default value types correctly' do | ||
| # Test that actual column defaults are normalized properly | ||
| columns = TestDefault.columns | ||
| active = columns.find { |c| c.name == 'active' } | ||
| verified = columns.find { |c| c.name == 'verified' } | ||
| status = columns.find { |c| c.name == 'status' } | ||
| priority = columns.find { |c| c.name == 'priority' } | ||
| # All these should be properly normalized | ||
| expect(normalize_default_value(active)).to be(false) | ||
| expect(normalize_default_value(verified)).to be(true) | ||
| expect(normalize_default_value(status)).to eq(0) | ||
| expect(normalize_default_value(priority)).to eq(1) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function with high complexity (count = 5): normalize_default_value [qlty:function-complexity]