Compare commits
2 Commits
ea33ffbb11
...
93e108081c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93e108081c | ||
|
|
04f6f78730 |
@@ -41,6 +41,7 @@ ActiveRecord::Schema.define do
|
||||
to_table: :users_fa_aux,
|
||||
primary_key: :base_table_id
|
||||
}
|
||||
t.integer :category
|
||||
t.integer :fa_id, index: true
|
||||
t.string :title
|
||||
t.string :species
|
||||
@@ -99,6 +100,7 @@ class FaPost < Post
|
||||
belongs_to :creator, class_name: "FaUser", inverse_of: :created_posts
|
||||
validates :fa_id, presence: true, uniqueness: true
|
||||
validates :title, presence: true
|
||||
enum :category, { image: 0, video: 1, text: 2 }
|
||||
end
|
||||
|
||||
class E621Post < Post
|
||||
|
||||
@@ -57,6 +57,7 @@ module HasAuxTable
|
||||
setup_attributes_hook!(config)
|
||||
setup_relation_extensions!(config)
|
||||
setup_attribute_getter_setter_hooks!(config)
|
||||
setup_enum_hook!(config)
|
||||
|
||||
config
|
||||
end
|
||||
@@ -197,10 +198,17 @@ module HasAuxTable
|
||||
|
||||
sig { params(load_schema_method: Method, config: AuxTableConfig).void }
|
||||
def aux_config_load_schema!(load_schema_method, config)
|
||||
puts "loading schema for #{self.name}"
|
||||
|
||||
# return if schema_loaded?
|
||||
|
||||
# first, load the main and aux table schemas like normal
|
||||
result = load_schema_method.call
|
||||
config.load_aux_schema
|
||||
|
||||
# columns_hash = schema_cache.columns_hash(table_name)
|
||||
# binding.pry if self.name == "FaPost"
|
||||
result = load_schema_method.call
|
||||
|
||||
aux_table_name = config.aux_table_name
|
||||
|
||||
check_for_overlapping_columns!(
|
||||
@@ -356,6 +364,28 @@ module HasAuxTable
|
||||
"Auxiliary table columns must not overlap with main table columns."
|
||||
end
|
||||
end
|
||||
|
||||
sig { params(config: AuxTableConfig).void }
|
||||
def setup_enum_hook!(config)
|
||||
self.define_singleton_method(
|
||||
:enum
|
||||
) do |name = nil, values = nil, **options, &block|
|
||||
config.aux.klass.enum(name, values, **options, &block)
|
||||
values.keys.each do |value_name|
|
||||
value_name = [options[:prefix], value_name, options[:suffix]].reject(
|
||||
&:blank?
|
||||
).join("_")
|
||||
[
|
||||
value_name,
|
||||
"#{value_name}?",
|
||||
"#{value_name}!",
|
||||
"#{value_name}="
|
||||
].each do |method_name|
|
||||
config.define_aux_attribute_delegate(method_name.to_sym)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
mixes_in_class_methods(ClassMethods)
|
||||
end
|
||||
|
||||
@@ -121,7 +121,7 @@ module HasAuxTable
|
||||
const :foreign_key, KeyType
|
||||
const :primary_key, KeyType
|
||||
|
||||
sig { void }
|
||||
sig { returns(T.untyped) }
|
||||
def load_aux_schema
|
||||
aux_class.load_schema
|
||||
end
|
||||
|
||||
@@ -99,6 +99,32 @@ RSpec.describe HasAuxTable do
|
||||
expect(lot.vehicles.first.name).to eq("vehicle1")
|
||||
end
|
||||
|
||||
it "can set association on aux record" do
|
||||
driver = Driver.create!(name: "John Doe")
|
||||
car = Car.create!(name: "Honda Civic")
|
||||
driver.car = car
|
||||
expect(driver.car).to eq(car)
|
||||
expect(driver.car_id).to eq(car.id)
|
||||
driver.save!
|
||||
|
||||
driver = Driver.find(driver.id)
|
||||
expect(driver.car).to eq(car)
|
||||
end
|
||||
|
||||
it "works with enums" do
|
||||
plane = Plane.create!(name: "Boeing 747", engine_type: :turbofan)
|
||||
expect(plane.engine_type).to eq("turbofan")
|
||||
|
||||
plane.engine_type = "piston"
|
||||
expect(plane.engine_type).to eq("piston")
|
||||
plane.save!
|
||||
expect(plane.engine_type).to eq("piston")
|
||||
expect(plane.piston?).to be_truthy
|
||||
|
||||
plane.turboprop!
|
||||
expect(plane.engine_type).to eq("turboprop")
|
||||
end
|
||||
|
||||
describe "#changed?" do
|
||||
it "returns true if the main record changes" do
|
||||
car = Car.create!(name: "Honda Civic")
|
||||
|
||||
@@ -81,6 +81,10 @@ module SpecHelper
|
||||
t.create_aux :boat do |t|
|
||||
t.boolean :only_freshwater
|
||||
end
|
||||
|
||||
t.create_aux :plane do |t|
|
||||
t.integer :engine_type
|
||||
end
|
||||
end
|
||||
|
||||
create_base_table :people do |t|
|
||||
@@ -90,7 +94,6 @@ module SpecHelper
|
||||
t.create_aux :driver do |t|
|
||||
t.integer :license_number
|
||||
t.references :car,
|
||||
null: false,
|
||||
foreign_key: {
|
||||
to_table: :vehicles_car_aux,
|
||||
primary_key: :base_table_id
|
||||
|
||||
@@ -21,6 +21,13 @@ class Boat < Vehicle
|
||||
belongs_to :captain, inverse_of: :boat
|
||||
end
|
||||
|
||||
class Plane < Vehicle
|
||||
aux_table :plane
|
||||
enum :engine_type,
|
||||
{ turbofan: 0, turboprop: 1, piston: 2, electric: 3 },
|
||||
type: :integer
|
||||
end
|
||||
|
||||
class Person < ActiveRecord::Base
|
||||
include HasAuxTable
|
||||
self.table_name = "people"
|
||||
@@ -28,7 +35,7 @@ end
|
||||
|
||||
class Driver < Person
|
||||
aux_table :driver
|
||||
belongs_to :car, inverse_of: :drivers
|
||||
belongs_to :car, optional: true
|
||||
end
|
||||
|
||||
class Captain < Person
|
||||
|
||||
Reference in New Issue
Block a user