Sha256: 1c82c4fc4f087d25a00788db3704af86771d55985babae4a19ba96a05d236319
Contents?: true
Size: 1.34 KB
Versions: 41
Compression:
Stored size: 1.34 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 def reset_#{name} association(:#{name}).reset 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 41 versions & 5 rubygems