Sha256: 5fd72bca39ca80d00a9451ec936f804ef84dae593e8eba6062cf8e87f07e577a

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module AuthHelpers
  module Spec

    module Associatable
      def self.included(base)
        klass = base.described_class

        column = klass.columns.detect{|c| c.name =~ /_id$/ }
        raise ScriptError, "Could not find a column that ends with id in #{base.name.tableize}" unless column

        association = column.name.gsub(/_id$/, '').to_sym
        polymorphic = !!klass.columns.detect{ |c| c.name == "#{association}_type" }

        base.class_eval do
          it "should validate associated #{association}" do
            associatable = base.described_class.create(@valid_attributes.merge(:"#{association}_attributes" => {}))
            associatable.should_not be_valid

            unless associatable.send(association).errors.empty?
              associatable.errors.should be_empty # this should be blank since errors is
                                                  # on the associated object.

              associatable.send(association).errors.should_not be_empty
            end
          end
        end
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
josevalim-auth_helpers-0.2.1 lib/auth_helpers/spec/associatable.rb
josevalim-auth_helpers-0.3.0 lib/auth_helpers/spec/associatable.rb
josevalim-auth_helpers-0.3.1 lib/auth_helpers/spec/associatable.rb
josevalim-auth_helpers-0.3.2 lib/auth_helpers/spec/associatable.rb