module Upgrow::Record::ClassMethods
Class methods to be extended by the class that includes the Record
module.
Public Instance Methods
@private
# File lib/upgrow/record.rb, line 102 def apply_default_options(name, options) default_options = { class_name: Naming.model_to_record(name.to_s.singularize.camelize), } default_options.merge(options) end
Overwrites the belongs to association macro to adjust the default class and foreign key names to Upgrow's conventions.
The default class name for the associated Record
is based on the association name and the `Record` suffix.
All these values can be set explicitly according to Active Record's original behaviour.
See api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
# File lib/upgrow/record.rb, line 74 def belongs_to(name, scope = nil, **options) options = apply_default_options(name, options) super end
@private
# File lib/upgrow/record.rb, line 111 def foreign_key(record_name) Naming.record_to_model(record_name.to_s).foreign_key end
Overwrites the has and belongs to many association macro to adjust the default class and foreign key names to Upgrow's conventions.
The default class name for the associated Record
is based on the association name and the `Record` suffix. The association's foreign key is derived from the caller's model name.
All these values can be set explicitly according to Active Record's original behaviour.
See api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
# File lib/upgrow/record.rb, line 90 def has_and_belongs_to_many(name, scope = nil, **options, &extension) options = apply_default_options(name, options) options[:foreign_key] ||= foreign_key(model_name) options[:association_foreign_key] ||= foreign_key(options.fetch(:class_name)) super end
Overwrites the has many association macro to adjust the default class and foreign key names to Upgrow's conventions.
The default class name for the associated collection of Records is based on the association name and the `Record` suffix. The association's foreign key is derived from the caller's model name.
All these values can be set explicitly according to Active Record's original behaviour.
See api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
# File lib/upgrow/record.rb, line 33 def has_many(name, scope = nil, **options, &extension) options = apply_default_options(name, options) unless options.key?(:as) options[:foreign_key] ||= foreign_key(model_name) end super end
Overwrites the has one association macro to adjust the default class and foreign key names to Upgrow's conventions.
The default class name for the associated Record
is based on the association name and the `Record` suffix. The association's foreign key is derived from the caller's model name.
All these values can be set explicitly according to Active Record's original behaviour.
See api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
# File lib/upgrow/record.rb, line 54 def has_one(name, scope = nil, **options, &extension) options = apply_default_options(name, options) unless options.key?(:as) options[:foreign_key] ||= foreign_key(model_name) end super end
Overwrites the table name reader from ActiveRecord::ModelSchema, removing the `records` suffix by default while still allowing table name to be set explicitly.
See api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html
# File lib/upgrow/record.rb, line 16 def table_name return @table_name if defined?(@table_name) self.table_name = super&.sub('_record', '') end