Sha256: bff1173f664d9ababe9eda81262f97afc66793e44a1c54b645b9fd2d4f8e6f54
Contents?: true
Size: 1.27 KB
Versions: 41
Compression:
Stored size: 1.27 KB
Contents
# frozen_string_literal: true # This class is inherited by the has_one and belongs_to association classes module ActiveRecord::Associations::Builder # :nodoc: class SingularAssociation < Association # :nodoc: def self.valid_options(options) super + [:required, :touch] end def self.define_accessors(model, reflection) super mixin = model.generated_association_methods name = reflection.name define_constructors(mixin, name) unless reflection.polymorphic? mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def reload_#{name} association(:#{name}).force_reload_reader end CODE end # Defines the (build|create)_association methods for belongs_to or has_one association def self.define_constructors(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def build_#{name}(*args, &block) association(:#{name}).build(*args, &block) end def create_#{name}(*args, &block) association(:#{name}).create(*args, &block) end def create_#{name}!(*args, &block) association(:#{name}).create!(*args, &block) end CODE end private_class_method :valid_options, :define_accessors, :define_constructors end end
Version data entries
41 entries across 39 versions & 5 rubygems