Module: Upgrow::Record::ClassMethods

Defined in:
lib/upgrow/record.rb

Overview

Class methods to be extended by the class that includes the Record module.

Instance Method Summary collapse

Instance Method Details

#apply_default_options(name, options) ⇒ Object



102
103
104
105
106
107
108
# 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

#belongs_to(name, scope = nil, **options) ⇒ Object

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



74
75
76
77
# File 'lib/upgrow/record.rb', line 74

def belongs_to(name, scope = nil, **options)
  options = apply_default_options(name, options)
  super
end

#foreign_key(record_name) ⇒ Object



111
112
113
# File 'lib/upgrow/record.rb', line 111

def foreign_key(record_name)
  Naming.record_to_model(record_name.to_s).foreign_key
end

#has_and_belongs_to_many(name, scope = nil, **options, &extension) ⇒ Object

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



90
91
92
93
94
95
96
97
98
99
# 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

#has_many(name, scope = nil, **options, &extension) ⇒ Object

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



33
34
35
36
37
38
39
40
41
# 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

#has_one(name, scope = nil, **options, &extension) ⇒ Object

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



54
55
56
57
58
59
60
61
62
# 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

#table_nameObject

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



16
17
18
19
20
# File 'lib/upgrow/record.rb', line 16

def table_name
  return @table_name if defined?(@table_name)

  self.table_name = super&.sub('_record', '')
end