Sha256: 876b48e067ed0482fccb957f58333ea4ce7a8c55ab9573e36d4d17be2c5aa4af

Contents?: true

Size: 922 Bytes

Versions: 13

Compression:

Stored size: 922 Bytes

Contents

module NinjaModel
  module Associations
    module Builder
      class BelongsTo < SingularAssociation
        self.macro = :belongs_to

        def build
          reflection = super
          configure_dependency
          reflection
        end

        private

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

            method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
            model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
              def #{method_name}
                association = #{name}
                association.#{options[:dependent]} if association
              end
            eoruby
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ninja-model-1.0.5 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-1.0.4 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-1.0.3 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-1.0.2 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-1.0.1 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-1.0.0 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.6 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.5 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.4 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.3 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.2 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.1 lib/ninja_model/associations/builder/belongs_to.rb
ninja-model-0.9.0 lib/ninja_model/associations/builder/belongs_to.rb