Sha256: f2ca7a69c877cfaf0818718dbfaae23d3d66b8a4b4e95dcc2498d06669f83a5c

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

require_relative '../../model_creators'
require 'forwardable'

module UnitTests
  module ModelCreators
    class ActiveRecord
      class HasMany
        def self.call(args)
          new(args).call
        end

        extend Forwardable

        def_delegators(
          :arguments,
          :attribute_name,
          :attribute_default_values_by_name,
        )

        def initialize(args)
          @arguments = CreateModelArguments::HasMany.wrap(args)
        end

        def call
          child_model_creator.call
          parent_model_creator.call
        end

        protected

        attr_reader :arguments

        private

        alias_method :association_name, :attribute_name
        alias_method :parent_model_creator_arguments, :arguments

        def child_model_creator
          @_child_model_creator ||=
            UnitTests::ModelCreationStrategies::ActiveRecord.new(
              child_model_name
            )
        end

        def parent_model_creator
          @_parent_model_creator ||= begin
            model_creator = UnitTests::ModelCreators::ActiveRecord.new(
              parent_model_creator_arguments
            )

            model_creator.customize_model do |model|
              model.has_many(association_name)
            end

            model_creator
          end
        end

        def child_model_name
          association_name.to_s.classify
        end
      end
    end

    register(:"active_record/has_many", ActiveRecord::HasMany)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shoulda-matchers-3.1.3 spec/support/unit/model_creators/active_record/has_many.rb
shoulda-matchers-4.0.0.rc1 spec/support/unit/model_creators/active_record/has_many.rb
shoulda-matchers-3.1.2 spec/support/unit/model_creators/active_record/has_many.rb
shoulda-matchers-3.1.1 spec/support/unit/model_creators/active_record/has_many.rb
shoulda-matchers-3.1.0 spec/support/unit/model_creators/active_record/has_many.rb