From 7e96fc577e3ef8bcce0d2b16d657e46acec092b6 Mon Sep 17 00:00:00 2001 From: Mike Boone Date: Sat, 7 Jun 2008 15:12:14 -0400 Subject: [PATCH 1/2] Corrected to hide scope when scope array is blank. --- lib/shoulda/active_record_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index ec840571..76135ab7 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -67,7 +67,7 @@ module ThoughtBot # :nodoc: klass = model_class attributes.each do |attribute| attribute = attribute.to_sym - should "require unique value for #{attribute}#{" scoped to #{scope.join(', ')}" if scope}" do + should "require unique value for #{attribute}#{" scoped to #{scope.join(', ')}" unless scope.blank?}" do assert existing = klass.find(:first), "Can't find first #{klass}" object = klass.new From 5eb9304489af5cb0314c971b55fe6fb75a0741b4 Mon Sep 17 00:00:00 2001 From: Mike Boone Date: Tue, 24 Jun 2008 15:50:38 -0400 Subject: [PATCH 2/2] Added should_have_readonly_attributes Added should_have_readonly_attributes ActiveRecord helper to test attributes set with attr_readonly. --- lib/shoulda/active_record_helpers.rb | 22 ++++++++++++++++++++++ test/rails_root/app/models/user.rb | 1 + test/unit/user_test.rb | 1 + 3 files changed, 24 insertions(+) diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index 76135ab7..3d7db2b6 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -129,6 +129,28 @@ module ThoughtBot # :nodoc: end end + # Ensures that the attribute cannot be changed once the record has been created. + # Requires an existing record. + # + # should_have_readonly_attributes :password, :admin_flag + # + def should_have_readonly_attributes(*attributes) + get_options!(attributes) + klass = model_class + + attributes.each do |attribute| + attribute = attribute.to_sym + should "make #{attribute} read-only" do + readonly = klass.readonly_attributes || [] + + assert readonly.include?(attribute.to_s), + (readonly.empty? ? + "#{klass} attribute #{attribute} is not read-only" : + "#{klass} is making #{readonly.to_a.to_sentence} read-only, but not #{attribute}.") + end + end + end + # Ensures that the attribute cannot be set to the given values # Requires an existing record # diff --git a/test/rails_root/app/models/user.rb b/test/rails_root/app/models/user.rb index 02a643a7..9b1db5bb 100644 --- a/test/rails_root/app/models/user.rb +++ b/test/rails_root/app/models/user.rb @@ -5,6 +5,7 @@ class User < ActiveRecord::Base has_one :address, :as => :addressable attr_protected :password + attr_readonly :name validates_format_of :email, :with => /\w*@\w*.com/ validates_length_of :email, :in => 1..100 diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index af4202f7..f06b8ca7 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -24,4 +24,5 @@ class UserTest < Test::Unit::TestCase :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)' should_require_acceptance_of :eula should_require_unique_attributes :email, :scoped_to => :name + should_have_readonly_attributes :name end