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

Update tableless models to not rely on fake schema loading #17238

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions app/models/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Action < ApplicationRecord

default_scope { default }

attribute :id, :text, default: nil
attribute :permission, :text, default: nil
attribute :global, :boolean, default: false
attribute :module, :text, default: false
define_attribute :id, ActiveRecord::Type.lookup(:text), default: nil
define_attribute :permission, ActiveRecord::Type.lookup(:text), default: nil
define_attribute :global, ActiveRecord::Type.lookup(:boolean), default: false
define_attribute :module, ActiveRecord::Type.lookup(:text), default: false
end
6 changes: 3 additions & 3 deletions app/models/capability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Capability < ApplicationRecord
belongs_to :context, class_name: "Project"
belongs_to :principal

attribute :action, :text, default: nil
attribute :context_id, :integer, default: nil
attribute :principal_id, :integer, default: nil
define_attribute :action, ActiveRecord::Type.lookup(:text), default: nil
define_attribute :context_id, ActiveRecord::Type.lookup(:integer), default: nil
define_attribute :principal_id, ActiveRecord::Type.lookup(:integer), default: nil
end
20 changes: 4 additions & 16 deletions app/models/concerns/tableless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,12 @@ def readonly?
end

class_methods do
def attribute_names
@attribute_names ||= attribute_types.keys
end

def load_schema!
@columns_hash ||= Hash.new

# From active_record/attributes.rb
attributes_to_define_after_schema_loads.each do |name, (type, default)|
if type.is_a?(Symbol)
type = ActiveRecord::Type.lookup(type, default)
end

define_attribute(name, type, default:)
# noop
end

# Improve Model#inspect output
@columns_hash[name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default)
end
def columns_hash
@columns_hash ||= {}
end
end
end
6 changes: 3 additions & 3 deletions app/models/day.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Day < ApplicationRecord
primary_key: :date,
dependent: nil

attribute :date, :date, default: nil
attribute :day_of_week, :integer, default: nil
attribute :working, :boolean, default: "t"
define_attribute :date, ActiveRecord::Type.lookup(:date), default: nil
define_attribute :day_of_week, ActiveRecord::Type.lookup(:integer), default: nil
define_attribute :working, ActiveRecord::Type.lookup(:boolean), default: "t"

delegate :name, to: :week_day, allow_nil: true

Expand Down
Loading