Skip to content

Commit

Permalink
Check primality of DB columns. Fixes #35.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Mar 30, 2012
1 parent 2b98e49 commit 68e65b2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/shoulda/matchers/active_record/have_db_column_matcher.rb
Expand Up @@ -35,6 +35,7 @@ def with_options(opts = {})
@default = opts[:default]
@null = opts[:null]
@scale = opts[:scale]
@primary = opts[:primary]
self
end

Expand All @@ -46,7 +47,8 @@ def matches?(subject)
correct_limit? &&
correct_default? &&
correct_null? &&
correct_scale?
correct_scale? &&
correct_primary?
end

def failure_message
Expand Down Expand Up @@ -150,6 +152,21 @@ def correct_scale?
end
end

def correct_primary?
return true if @primary.nil?
if matched_column.primary == @primary
true
else
@missing = "#{model_class} has a db column named #{@column} "
if @primary
@missing << "that is not primary, but should be"
else
@missing << "that is primary, but should not be"
end
false
end
end

def matched_column
model_class.columns.detect { |each| each.name == @column.to_s }
end
Expand Down
18 changes: 18 additions & 0 deletions spec/shoulda/active_record/have_db_column_matcher_spec.rb
Expand Up @@ -164,4 +164,22 @@
Superhero.new.should_not @matcher
end
end

context "have_db_column with primary option" do
it "should accept a column that is primary" do
create_table 'superheros' do |table|
table.integer :id, :primary => true
end
define_model_class 'Superhero'
Superhero.new.should have_db_column(:id).with_options(:primary => true)
end

it "should reject a column that is not primary" do
create_table 'superheros' do |table|
table.integer :primary
end
define_model_class 'Superhero'
Superhero.new.should_not have_db_column(:primary).with_options(:primary => true)
end
end
end

0 comments on commit 68e65b2

Please sign in to comment.