Sha256: 021b8af6ff8a48e7a676b50d3849f0011f68fb71a41bfdca69b3344f5bee5f65

Contents?: true

Size: 1.9 KB

Versions: 55

Compression:

Stored size: 1.9 KB

Contents

require 'active_support/core_ext/object/inclusion'

module ActiveRecord::Associations::Builder
  class HasOne < SingularAssociation #:nodoc:
    self.macro = :has_one

    self.valid_options += [:order, :as]

    class_attribute :through_options
    self.through_options = [:through, :source, :source_type]

    def constructable?
      !options[:through]
    end

    def build
      reflection = super
      configure_dependency unless options[:through]
      reflection
    end

    private

      def validate_options
        valid_options = self.class.valid_options
        valid_options += self.class.through_options if options[:through]
        options.assert_valid_keys(valid_options)
      end

      def configure_dependency
        if options[:dependent]
          unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
            raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \
                                 ":nullify or :restrict (#{options[:dependent].inspect})"
          end

          send("define_#{options[:dependent]}_dependency_method")
          model.before_destroy dependency_method_name
        end
      end

      def dependency_method_name
        "has_one_dependent_#{options[:dependent]}_for_#{name}"
      end

      def define_destroy_dependency_method
        model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
          def #{dependency_method_name}
            association(#{name.to_sym.inspect}).delete
          end
        eoruby
      end
      alias :define_delete_dependency_method :define_destroy_dependency_method
      alias :define_nullify_dependency_method :define_destroy_dependency_method

      def define_restrict_dependency_method
        name = self.name
        model.redefine_method(dependency_method_name) do
          raise ActiveRecord::DeleteRestrictionError.new(name) unless send(name).nil?
        end
      end
  end
end

Version data entries

55 entries across 55 versions & 2 rubygems

Version Path
activerecord-3.1.12 lib/active_record/associations/builder/has_one.rb
activerecord-3.1.11 lib/active_record/associations/builder/has_one.rb
activerecord-3.1.10 lib/active_record/associations/builder/has_one.rb
activerecord-3.1.9 lib/active_record/associations/builder/has_one.rb
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.7.0 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.7 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.6 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.5 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.4 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
activerecord-3.1.8 lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
activerecord-3.1.7 lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.1 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
dirty_history-0.6.0 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb
activerecord-3.1.6 lib/active_record/associations/builder/has_one.rb
dirty_history-0.5.4 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/associations/builder/has_one.rb