Sha256: f25e0568ab3bbe1e45d62605367399f465ea881a63cfe418df3724a502c4f1d1

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'spec_helper'

class User < ActiveRecord::Base
  has_many :projects
  has_many :ideas
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :ideas
  validates_presence_of :user_id, presence: true
end

class Idea < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
  validates_presence_of :user_id, :project_id, presence: true
end

describe "Behaviour without validate_foreign_keys" do

  let!(:user) { User.create }
  let!(:project) { user.projects.create }
  let!(:idea) { project.ideas.create user: user }

  it "uses same user ids by default" do
    expect(project.user_id).to eq(user.id)
    expect(user.id).to eq(user.id)
    expect(idea.user_id).to eq(user.id)
  end

  it "allow to rewrite user id of idea" do
    idea.user_id = 42
    idea.save
    idea.reload
    expect(idea.user_id).to eq(42)
  end

  it "allow to rewrite user id of project" do
    project.user_id = 42
    project.save
    project.reload
    expect(project.user_id).to eq(42)
  end

end

describe "Behaviour with validate_foreign_keys" do

  before do
    Idea.class_eval do
      validate_foreign_keys
    end
  end

  let!(:user) { User.create }
  let!(:project) { user.projects.create }
  let!(:idea) { project.ideas.create user: user }

  it "uses same user ids by default" do
    expect(project.user_id).to eq(user.id)
    expect(user.id).to eq(user.id)
    expect(idea.user_id).to eq(user.id)
  end

  it "does not allow to rewrite user id of idea" do
    idea.user_id = 42
    idea.save
    expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id")
    expect(idea.reload.user_id).to_not eq(42)
  end

  it "does allow to rewrite user id of project" do
    project.user_id = 42
    project.save
    expect(project.errors).to be_empty
    expect(project.reload.user_id).to eq(42)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foreign_key_validation-0.0.2 spec/models/model_spec.rb