Spec-Zone.ru › Ruby on Rails 6.1

класс ActiveRecord::Reflection::MacroReflection

Родитель:
ActiveRecord::Reflection::AbstractReflection

Base класс для AggregateReflection и AssociationReflection. Объекты AggregateReflection и AssociationReflection возвращаются методом Reflection::ClassMethods.

Атрибуты

active_record[R]
name[R]

Возвращает имя макроса.

composed_of :balance, class_name: 'Money' возвращает :balance has_many :clients возвращает :clients

options[R]

Возвращает хеш опций, используемых для макроса.

composed_of :balance, class_name: 'Money' возвращает { class_name: "Money" } has_many :clients возвращает {}

scope[R]

Общедоступные методы класса

new(name, scope, options, active_record) Показать исходный код
# File activerecord/lib/active_record/reflection.rb, line 332
def initialize(name, scope, options, active_record)
  @name          = name
  @scope         = scope
  @options       = options
  @active_record = active_record
  @klass         = options[:anonymous_class]
  @plural_name   = active_record.pluralize_table_names ?
                      name.to_s.pluralize : name.to_s
end

Общедоступные методы экземпляра

==(other_aggregation) Показать исходный код
# File activerecord/lib/active_record/reflection.rb, line 375
def ==(other_aggregation)
  super ||
    other_aggregation.kind_of?(self.class) &&
    name == other_aggregation.name &&
    !other_aggregation.options.nil? &&
    active_record == other_aggregation.active_record
end

Возвращает true если self и other_aggregation имеют одинаковый атрибут name, атрибут active_record, и other_aggregation имеет присвоенный хеш опций.

Вызывает метод родительского класса
autosave=(autosave) Показать исходный код
# File activerecord/lib/active_record/reflection.rb, line 342
def autosave=(autosave)
  @options[:autosave] = autosave
  parent_reflection = self.parent_reflection
  if parent_reflection
    parent_reflection.autosave = autosave
  end
end
compute_class(name) Показать исходный код
# File activerecord/lib/active_record/reflection.rb, line 369
def compute_class(name)
  name.constantize
end
klass() Показать исходный код
# File activerecord/lib/active_record/reflection.rb, line 365
def klass
  @klass ||= compute_class(class_name)
end

Возвращает класс для макроса.

composed_of :balance, class_name: 'Money' возвращает класс Money has_many :clients возвращает класс Client

class Company < ActiveRecord::Base
  has_many :clients
end

Company.reflect_on_association(:clients).klass
# => Client

Примечание: Не вызывайте klass.new или klass.create для создания нового объекта ассоциации. Используйте build_association или create_association вместо этого. Это позволяет плагинам подключаться к созданию объекта ассоциации.

scope_for(relation, owner = nil) Показать исходный код
# File activerecord/lib/active_record/reflection.rb, line 383
def scope_for(relation, owner = nil)
  relation.instance_exec(owner, &scope) || relation
end

© 2004–2020 David Heinemeier Hansson
Licensed under the MIT License.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API