Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

foreigner-matcher

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

foreigner-matcher

  • 0.3.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

foreigner-matcher

Gem Version Build Status Code Climate

RSpec matcher for the Foreigner gem, inspired by Remarkable ActiveRecord matchers.

Version Information

The 0.3.x verison should accompany use of RSpec 3.0. The same version should be safe to use with RSpec 2.14.x as well.

Note that Foreigner >= 1.0.0 is Rails 3.x and higher only!

Rails 4.x + 3.x

Foreigner >= 1.0.0 use foreigner-matcher ~> 0.3.0

Foreigner ~> 0.9 use foreigner-matcher ~> 0.1.1

Rails 2.3

Use foreigner-matcher ~> 0.1.1

Installation

Please check the Version Information for the appropriate version!

For installation with bundler, add the following to the approrpriate group in your Gemfile:

gem "foreigner-matcher", "~> 0.3.0", :require => nil

For installation without bundler in Rails 2.3, add the following to your config/environments/test.rb:

config.gem "foreigner-matcher", "~> 0.1.1", :require => nil

To install the gem the old fashioned way:

gem install foreigner-matcher

In spec_helper.rb:

require 'foreigner-matcher'

Note that the entry in spec_helper.rb must follow any require for RSpec libraries.

Usage

The matcher can be used in RSpec to ensure an ActiveRecord model has the desired foreign key. The minimum argument is the table name that the subject model should have a foreign key to.

For example. Given these two models:

class User < ActiveRecord::Base
  has_many user_logins
end

class UserLogin < ActiveRecord::Base
  belongs_to user
end

And given this migration for the foreign key:

class ChangeUserIdToForeignKeyOnUserLogins < ActiveRecord::Migration
  def self.up
    add_foreign_key :user_logins, :users
  end

  def self.down
    remove_foreign_key :user_logins, :users
  end
end

The spec would look like this:

describe UserLogin do
  it { should have_foreign_key_for(:users) }
end

In addition to the table name, you can include any options that add_foreign_key (see Foreigner) accepts. Some more examples using the same models:

it { should have_foreign_key_for(:users, :dependent => :delete) }
it { should have_foreign_key_for(:users, :column => "unique_user_id", :name => "user_logins_unique_user_id_fk") }
it { should_not have_foreign_key_for(:users, :dependent => :nullify) }

A Note on Table Names

These examples will also work passing :user as the table name. Why? The example has a belongs_to relationship, and my feeling is that it reads better to say "it should have foreign key for user". This is just my taste; use what makes sense to you!

My Testing Environment

The project is tested on Travis-CI. The rubies and databases that the matcher is regularly verified with can be seen in .travis.yml

The current gems that are tested:

  • ActiveRecord ~> 4.1.0, Rspec ~> 3.0.0, Foreigner >= 1.00
  • ActiveRecord ~> 4.1.0, Rspec ~> 2.14.0, Foreigner >= 1.00
  • ActiveRecord ~> 4.0.0, Rspec ~> 3.0.0, Foreigner >= 1.00
  • ActiveRecord ~> 4.0.0, Rspec ~> 2.14.0, Foreigner >= 1.00
  • ActiveRecord ~> 3.2.0, Rspec ~> 3.0.0, Foreigner >= 1.00
  • ActiveRecord ~> 3.2.0, Rspec ~> 2.14.0, Foreigner >= 1.00

Copyright (c) 2011-2014 Cameron Dykes. See LICENSE.txt for further details.

FAQs

Package last updated on 14 Jul 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc