Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/_examples/demo/app/models/user.rb
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
36 changes: 35 additions & 1 deletion packages/_examples/demo/db/schema.rb

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

2 changes: 2 additions & 0 deletions packages/_examples/warehouse/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -65,7 +65,7 @@ def fetch_fields
is_primary_key: column_name == @model.primary_key || @model.primary_key.include?(column_name),
is_read_only: false,
is_sortable: true,
default_value: column.default,
default_value: column.default.nil? ? nil : normalize_default_value(column),
enum_values: get_enum_values(@model, column),
validation: get_validations(column)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ def sti_column?(model, column)
model.inheritance_column && column.name == model.inheritance_column
end

def normalize_default_value(column)
case column.type
when :boolean
case column.default.to_s
when '0', 'f', 'false'
false
when '1', 't', 'true'
true
else
column.default
end
when :integer
column.default.to_i
when :float, :decimal
column.default.to_f
else
column.default
end
Copy link

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]

end

def operators_for_column_type(type)
result = [Operators::PRESENT, Operators::BLANK, Operators::MISSING]
equality = [Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN]
orderables = [
Operators::LESS_THAN,
Operators::GREATER_THAN,
Operators::LESS_THAN_OR_EQUAL,
Operators::GREATER_THAN_OR_EQUAL
]
strings = [
Operators::LIKE,
Operators::I_LIKE,
Operators::CONTAINS,
Operators::I_CONTAINS,
Operators::NOT_CONTAINS,
Operators::NOT_I_CONTAINS,
Operators::STARTS_WITH,
Operators::I_STARTS_WITH,
Operators::ENDS_WITH,
Operators::I_ENDS_WITH,
Operators::SHORTER_THAN,
Operators::LONGER_THAN
]

if type.is_a? String
orderables = [
Expand Down
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
Loading