carlosbrando-remarkable 0.0.99
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +124 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +48 -0
- data/Rakefile +29 -0
- data/init.rb +1 -0
- data/lib/remarkable.rb +13 -0
- data/lib/remarkable/active_record/active_record.rb +21 -0
- data/lib/remarkable/active_record/helpers.rb +16 -0
- data/lib/remarkable/active_record/macros/associations/belong_to.rb +81 -0
- data/lib/remarkable/active_record/macros/associations/have_and_belong_to_many.rb +77 -0
- data/lib/remarkable/active_record/macros/associations/have_many.rb +160 -0
- data/lib/remarkable/active_record/macros/associations/have_one.rb +133 -0
- data/lib/remarkable/active_record/macros/database/have_db_column.rb +81 -0
- data/lib/remarkable/active_record/macros/database/have_db_columns.rb +73 -0
- data/lib/remarkable/active_record/macros/database/have_indices.rb +75 -0
- data/lib/remarkable/active_record/macros/validations/allow_values_for.rb +103 -0
- data/lib/remarkable/active_record/macros/validations/ensure_length_at_least.rb +97 -0
- data/lib/remarkable/active_record/macros/validations/ensure_length_in_range.rb +134 -0
- data/lib/remarkable/active_record/macros/validations/ensure_length_is.rb +106 -0
- data/lib/remarkable/active_record/macros/validations/ensure_value_in_range.rb +117 -0
- data/lib/remarkable/active_record/macros/validations/have_class_methods.rb +74 -0
- data/lib/remarkable/active_record/macros/validations/have_instance_methods.rb +74 -0
- data/lib/remarkable/active_record/macros/validations/have_named_scope.rb +148 -0
- data/lib/remarkable/active_record/macros/validations/have_readonly_attributes.rb +81 -0
- data/lib/remarkable/active_record/macros/validations/only_allow_numeric_values_for.rb +89 -0
- data/lib/remarkable/active_record/macros/validations/protect_attributes.rb +89 -0
- data/lib/remarkable/active_record/macros/validations/require_acceptance_of.rb +94 -0
- data/lib/remarkable/active_record/macros/validations/require_attributes.rb +94 -0
- data/lib/remarkable/active_record/macros/validations/require_unique_attributes.rb +146 -0
- data/lib/remarkable/controller/controller.rb +15 -0
- data/lib/remarkable/controller/helpers.rb +48 -0
- data/lib/remarkable/controller/macros/assign_to.rb +110 -0
- data/lib/remarkable/controller/macros/filter_params.rb +52 -0
- data/lib/remarkable/controller/macros/redirect_to.rb +24 -0
- data/lib/remarkable/controller/macros/render_a_form.rb +23 -0
- data/lib/remarkable/controller/macros/render_template.rb +18 -0
- data/lib/remarkable/controller/macros/render_with_layout.rb +61 -0
- data/lib/remarkable/controller/macros/respond_with.rb +86 -0
- data/lib/remarkable/controller/macros/respond_with_content_type.rb +45 -0
- data/lib/remarkable/controller/macros/return_from_session.rb +45 -0
- data/lib/remarkable/controller/macros/route.rb +91 -0
- data/lib/remarkable/controller/macros/set_the_flash_to.rb +58 -0
- data/lib/remarkable/example/example_methods.rb +32 -0
- data/lib/remarkable/private_helpers.rb +123 -0
- data/rails/init.rb +1 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/controllers/posts_controller_spec.rb +166 -0
- data/spec/controllers/users_controller_spec.rb +14 -0
- data/spec/fixtures/addresses.yml +3 -0
- data/spec/fixtures/friendships.yml +0 -0
- data/spec/fixtures/posts.yml +5 -0
- data/spec/fixtures/products.yml +0 -0
- data/spec/fixtures/taggings.yml +0 -0
- data/spec/fixtures/tags.yml +9 -0
- data/spec/fixtures/users.yml +6 -0
- data/spec/models/address_spec.rb +21 -0
- data/spec/models/dog_spec.rb +17 -0
- data/spec/models/flea_spec.rb +9 -0
- data/spec/models/friendship_spec.rb +13 -0
- data/spec/models/post_spec.rb +35 -0
- data/spec/models/product_spec.rb +57 -0
- data/spec/models/tag_spec.rb +21 -0
- data/spec/models/tagging_spec.rb +13 -0
- data/spec/models/user_spec.rb +107 -0
- data/spec/rails_root/app/controllers/application.rb +25 -0
- data/spec/rails_root/app/controllers/posts_controller.rb +85 -0
- data/spec/rails_root/app/controllers/users_controller.rb +84 -0
- data/spec/rails_root/app/helpers/application_helper.rb +3 -0
- data/spec/rails_root/app/helpers/posts_helper.rb +2 -0
- data/spec/rails_root/app/helpers/users_helper.rb +2 -0
- data/spec/rails_root/app/models/address.rb +7 -0
- data/spec/rails_root/app/models/dog.rb +5 -0
- data/spec/rails_root/app/models/flea.rb +3 -0
- data/spec/rails_root/app/models/friendship.rb +4 -0
- data/spec/rails_root/app/models/post.rb +12 -0
- data/spec/rails_root/app/models/product.rb +12 -0
- data/spec/rails_root/app/models/tag.rb +8 -0
- data/spec/rails_root/app/models/tagging.rb +4 -0
- data/spec/rails_root/app/models/user.rb +28 -0
- data/spec/rails_root/app/views/layouts/posts.rhtml +17 -0
- data/spec/rails_root/app/views/layouts/users.rhtml +17 -0
- data/spec/rails_root/app/views/layouts/wide.html.erb +1 -0
- data/spec/rails_root/app/views/posts/edit.rhtml +27 -0
- data/spec/rails_root/app/views/posts/index.rhtml +25 -0
- data/spec/rails_root/app/views/posts/new.rhtml +26 -0
- data/spec/rails_root/app/views/posts/show.rhtml +18 -0
- data/spec/rails_root/app/views/users/edit.rhtml +22 -0
- data/spec/rails_root/app/views/users/index.rhtml +22 -0
- data/spec/rails_root/app/views/users/new.rhtml +21 -0
- data/spec/rails_root/app/views/users/show.rhtml +13 -0
- data/spec/rails_root/config/boot.rb +109 -0
- data/spec/rails_root/config/database.yml +4 -0
- data/spec/rails_root/config/environment.rb +14 -0
- data/spec/rails_root/config/environments/sqlite3.rb +0 -0
- data/spec/rails_root/config/initializers/new_rails_defaults.rb +15 -0
- data/spec/rails_root/config/initializers/shoulda.rb +8 -0
- data/spec/rails_root/config/routes.rb +6 -0
- data/spec/rails_root/db/migrate/001_create_users.rb +18 -0
- data/spec/rails_root/db/migrate/002_create_posts.rb +13 -0
- data/spec/rails_root/db/migrate/003_create_taggings.rb +12 -0
- data/spec/rails_root/db/migrate/004_create_tags.rb +11 -0
- data/spec/rails_root/db/migrate/005_create_dogs.rb +12 -0
- data/spec/rails_root/db/migrate/006_create_addresses.rb +14 -0
- data/spec/rails_root/db/migrate/007_create_fleas.rb +11 -0
- data/spec/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
- data/spec/rails_root/db/migrate/009_create_products.rb +17 -0
- data/spec/rails_root/db/migrate/010_create_friendships.rb +14 -0
- data/spec/rails_root/db/schema.rb +0 -0
- data/spec/rails_root/log/.keep +0 -0
- data/spec/rails_root/public/.htaccess +40 -0
- data/spec/rails_root/public/404.html +30 -0
- data/spec/rails_root/public/422.html +30 -0
- data/spec/rails_root/public/500.html +30 -0
- data/spec/rails_root/script/console +3 -0
- data/spec/rails_root/script/generate +3 -0
- data/spec/rails_root/vendor/plugins/.keep +0 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +58 -0
- data/tasks/rspec.rake +21 -0
- metadata +216 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class HaveOne
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(*associations)
|
9
|
+
@dependent = get_options!(associations, :dependent)
|
10
|
+
@associations = associations
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(klass)
|
14
|
+
@klass = klass
|
15
|
+
|
16
|
+
begin
|
17
|
+
@associations.each do |association|
|
18
|
+
reflection = klass.reflect_on_association(association)
|
19
|
+
fail("#{klass.name} does not have any relationship to #{association}") unless reflection && reflection.macro == :has_one
|
20
|
+
|
21
|
+
associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
|
22
|
+
|
23
|
+
if reflection.options[:foreign_key]
|
24
|
+
fk = reflection.options[:foreign_key]
|
25
|
+
elsif reflection.options[:as]
|
26
|
+
fk = reflection.options[:as].to_s.foreign_key
|
27
|
+
fk_type = fk.gsub(/_id$/, '_type')
|
28
|
+
|
29
|
+
fail("#{associated_klass.name} does not have a #{fk_type} column.") unless associated_klass.column_names.include?(fk_type)
|
30
|
+
else
|
31
|
+
fk = klass.name.foreign_key
|
32
|
+
end
|
33
|
+
|
34
|
+
fail("#{associated_klass.name} does not have a #{fk} foreign key.") unless associated_klass.column_names.include?(fk.to_s)
|
35
|
+
|
36
|
+
if @dependent
|
37
|
+
fail("#{association} should have #{@dependent} dependency") unless @dependent.to_s == reflection.options[:dependent].to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
true
|
42
|
+
rescue Exception => e
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def description
|
48
|
+
message = "have one #{@associations.to_sentence}"
|
49
|
+
message += " dependent => #{@dependent}" if @dependent
|
50
|
+
message
|
51
|
+
end
|
52
|
+
|
53
|
+
def failure_message
|
54
|
+
message = "expected #{@klass.name} to have one #{@associations.to_sentence}"
|
55
|
+
message += " dependent => #{@dependent}" if @dependent
|
56
|
+
message += ", but it didn't"
|
57
|
+
@failure_message || message
|
58
|
+
end
|
59
|
+
|
60
|
+
def negative_failure_message
|
61
|
+
message = "expected #{@klass.name} not to have one #{@associations.to_sentence}"
|
62
|
+
message += " dependent => #{@dependent}" if @dependent
|
63
|
+
message += ", but it did"
|
64
|
+
message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Ensure that the has_one relationship exists. Will also test that the
|
69
|
+
# associated table has the required columns. Works with polymorphic
|
70
|
+
# associations.
|
71
|
+
#
|
72
|
+
# Options:
|
73
|
+
# * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
|
74
|
+
#
|
75
|
+
# Example:
|
76
|
+
# it { User.should have_one(:address) }
|
77
|
+
#
|
78
|
+
def have_one(*associations)
|
79
|
+
Remarkable::Syntax::RSpec::HaveOne.new(*associations)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module Shoulda
|
84
|
+
# Ensure that the has_one relationship exists. Will also test that the
|
85
|
+
# associated table has the required columns. Works with polymorphic
|
86
|
+
# associations.
|
87
|
+
#
|
88
|
+
# Options:
|
89
|
+
# * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
|
90
|
+
#
|
91
|
+
# Example:
|
92
|
+
# should_have_one :god # unless hindu
|
93
|
+
#
|
94
|
+
def should_have_one(*associations)
|
95
|
+
dependent = get_options!(associations, :dependent)
|
96
|
+
klass = model_class
|
97
|
+
associations.each do |association|
|
98
|
+
name = "should have one #{association}"
|
99
|
+
name += " dependent => #{dependent}" if dependent
|
100
|
+
it name do
|
101
|
+
reflection = klass.reflect_on_association(association)
|
102
|
+
fail_with("#{klass.name} does not have any relationship to #{association}") unless reflection
|
103
|
+
reflection.macro.should == :has_one
|
104
|
+
|
105
|
+
associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
|
106
|
+
|
107
|
+
if reflection.options[:foreign_key]
|
108
|
+
fk = reflection.options[:foreign_key]
|
109
|
+
elsif reflection.options[:as]
|
110
|
+
fk = reflection.options[:as].to_s.foreign_key
|
111
|
+
fk_type = fk.gsub(/_id$/, '_type')
|
112
|
+
unless associated_klass.column_names.include?(fk_type)
|
113
|
+
fail_with "#{associated_klass.name} does not have a #{fk_type} column."
|
114
|
+
end
|
115
|
+
else
|
116
|
+
fk = klass.name.foreign_key
|
117
|
+
end
|
118
|
+
unless associated_klass.column_names.include?(fk.to_s)
|
119
|
+
fail_with "#{associated_klass.name} does not have a #{fk} foreign key."
|
120
|
+
end
|
121
|
+
|
122
|
+
if dependent
|
123
|
+
unless reflection.options[:dependent].to_s == dependent.to_s
|
124
|
+
fail_with "#{association} should have #{dependent} dependency"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class HaveDbColumn
|
6
|
+
def initialize(name, opts)
|
7
|
+
@name = name
|
8
|
+
@opts = opts
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(klass)
|
12
|
+
@klass = klass
|
13
|
+
|
14
|
+
column = klass.columns.detect {|c| c.name == @name.to_s }
|
15
|
+
fail("#{klass.name} does not have column #{@name}") unless column
|
16
|
+
|
17
|
+
@opts.each do |k, v|
|
18
|
+
fail(":#{@name} column on table for #{klass} does not match option :#{k}") unless column.instance_variable_get("@#{k}").to_s == v.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def description
|
23
|
+
message = "have column named :#{@name}"
|
24
|
+
message += " with options " + @opts.inspect unless @opts.empty?
|
25
|
+
message
|
26
|
+
end
|
27
|
+
|
28
|
+
def failure_message
|
29
|
+
message = "expected #{@klass.name} to have column named :#{@name}"
|
30
|
+
message += " with options " + @opts.inspect unless @opts.empty?
|
31
|
+
message += ", but it didn't"
|
32
|
+
|
33
|
+
@failure_message || message
|
34
|
+
end
|
35
|
+
|
36
|
+
def negative_failure_message
|
37
|
+
message = "expected #{@klass.name} not to have column named :#{@name}"
|
38
|
+
message += " with options " + @opts.inspect unless @opts.empty?
|
39
|
+
message += ", but it did"
|
40
|
+
message
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Ensure that the given column is defined on the models backing SQL table. The options are the same as
|
45
|
+
# the instance variables defined on the column definition: :precision, :limit, :default, :null,
|
46
|
+
# :primary, :type, :scale, and :sql_type.
|
47
|
+
#
|
48
|
+
# it { User.should have_db_column(:email, :type => "string", :default => nil, :precision => nil, :limit => 255,
|
49
|
+
# :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)') }
|
50
|
+
#
|
51
|
+
def have_db_column(name, opts = {})
|
52
|
+
Remarkable::Syntax::RSpec::HaveDbColumn.new(name, opts)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
module Shoulda
|
57
|
+
# Ensure that the given column is defined on the models backing SQL table. The options are the same as
|
58
|
+
# the instance variables defined on the column definition: :precision, :limit, :default, :null,
|
59
|
+
# :primary, :type, :scale, and :sql_type.
|
60
|
+
#
|
61
|
+
# should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
|
62
|
+
# :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
63
|
+
#
|
64
|
+
def should_have_db_column(name, opts = {})
|
65
|
+
klass = model_class
|
66
|
+
test_name = "should have column named :#{name}"
|
67
|
+
test_name += " with options " + opts.inspect unless opts.empty?
|
68
|
+
it test_name do
|
69
|
+
column = klass.columns.detect {|c| c.name == name.to_s }
|
70
|
+
fail_with("#{klass.name} does not have column #{name}") unless column
|
71
|
+
opts.each do |k, v|
|
72
|
+
unless v.to_s == column.instance_variable_get("@#{k}").to_s
|
73
|
+
fail_with(":#{name} column on table for #{klass} does not match option :#{k}")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class HaveDbColumns
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(*columns)
|
9
|
+
@column_type = get_options!(columns, :type)
|
10
|
+
@columns = columns
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(klass)
|
14
|
+
@klass = klass
|
15
|
+
|
16
|
+
@columns.each do |name|
|
17
|
+
column = klass.columns.detect {|c| c.name == name.to_s }
|
18
|
+
fail("#{klass.name} does not have column #{name}") unless column
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def description
|
23
|
+
message = "have columns #{@columns.to_sentence}"
|
24
|
+
message += " of type #{@column_type}" if @column_type
|
25
|
+
message
|
26
|
+
end
|
27
|
+
|
28
|
+
def failure_message
|
29
|
+
message = "expected to have columns #{@columns.to_sentence}"
|
30
|
+
message += " of type #{@column_type}" if @column_type
|
31
|
+
message += ", but it didn't"
|
32
|
+
|
33
|
+
@failure_message || message
|
34
|
+
end
|
35
|
+
|
36
|
+
def negative_failure_message
|
37
|
+
message = "expected not to have columns #{@columns.to_sentence}"
|
38
|
+
message += " of type #{@column_type}" if @column_type
|
39
|
+
message += ", but it did"
|
40
|
+
message
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Ensure that the given columns are defined on the models backing SQL table.
|
45
|
+
#
|
46
|
+
# it { User.should have_db_columns(:id, :email, :name, :created_at) }
|
47
|
+
#
|
48
|
+
def have_db_columns(*columns)
|
49
|
+
Remarkable::Syntax::RSpec::HaveDbColumns.new(*columns)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module Shoulda
|
54
|
+
# Ensure that the given columns are defined on the models backing SQL table.
|
55
|
+
#
|
56
|
+
# should_have_db_columns :id, :email, :name, :created_at
|
57
|
+
#
|
58
|
+
def should_have_db_columns(*columns)
|
59
|
+
column_type = get_options!(columns, :type)
|
60
|
+
klass = model_class
|
61
|
+
columns.each do |name|
|
62
|
+
test_name = "should have column #{name}"
|
63
|
+
test_name += " of type #{column_type}" if column_type
|
64
|
+
it test_name do
|
65
|
+
column = klass.columns.detect {|c| c.name == name.to_s }
|
66
|
+
fail_with("#{klass.name} does not have column #{name}") unless column
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class HaveIndices
|
6
|
+
def initialize(*columns)
|
7
|
+
@columns = columns
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(klass)
|
11
|
+
@table = klass.table_name
|
12
|
+
indices = ::ActiveRecord::Base.connection.indexes(@table).map(&:columns)
|
13
|
+
|
14
|
+
@columns.each do |column|
|
15
|
+
columns = [column].flatten.map(&:to_s)
|
16
|
+
return false unless indices.include?(columns)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def description
|
21
|
+
"have index on #{@table} for #{pretty_columns}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def failure_message
|
25
|
+
"expected to have index on #{@table} for #{pretty_columns}, but it didn't"
|
26
|
+
end
|
27
|
+
|
28
|
+
def negative_failure_message
|
29
|
+
"expected not to have index on #{@table} for #{pretty_columns}, but it did"
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def pretty_columns
|
35
|
+
@columns.collect { |col| col.is_a?(Array) ? "[#{col.join(', ')}]" : col }.to_sentence
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Ensures that there are DB indices on the given columns or tuples of columns.
|
40
|
+
# Also aliased to should_have_index for readability
|
41
|
+
#
|
42
|
+
# it { User.should have_indices(:email, :name, [:commentable_type, :commentable_id]) }
|
43
|
+
# it { User.should have_index(:age) }
|
44
|
+
#
|
45
|
+
def have_indices(*columns)
|
46
|
+
Remarkable::Syntax::RSpec::HaveIndices.new(*columns)
|
47
|
+
end
|
48
|
+
|
49
|
+
alias :have_index :have_indices
|
50
|
+
end
|
51
|
+
|
52
|
+
module Shoulda
|
53
|
+
# Ensures that there are DB indices on the given columns or tuples of columns.
|
54
|
+
# Also aliased to should_have_index for readability
|
55
|
+
#
|
56
|
+
# should_have_indices :email, :name, [:commentable_type, :commentable_id]
|
57
|
+
# should_have_index :age
|
58
|
+
#
|
59
|
+
def should_have_indices(*columns)
|
60
|
+
table = model_class.table_name
|
61
|
+
indices = ::ActiveRecord::Base.connection.indexes(table).map(&:columns)
|
62
|
+
|
63
|
+
columns.each do |column|
|
64
|
+
it "should have index on #{table} for #{column.inspect}" do
|
65
|
+
columns = [column].flatten.map(&:to_s)
|
66
|
+
assert_contains(indices, columns).should be_true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
alias_method :should_have_index, :should_have_indices
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class AllowValuesFor
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(attribute, *good_values)
|
9
|
+
get_options!(good_values)
|
10
|
+
|
11
|
+
@attribute = attribute
|
12
|
+
@good_values = good_values
|
13
|
+
end
|
14
|
+
|
15
|
+
def matches?(klass)
|
16
|
+
@klass = klass
|
17
|
+
|
18
|
+
begin
|
19
|
+
@good_values.each do |v|
|
20
|
+
return false unless assert_good_value(klass, @attribute, v)
|
21
|
+
end
|
22
|
+
|
23
|
+
true
|
24
|
+
rescue Exception => e
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def description
|
30
|
+
"allow #{@attribute} to be set to #{@good_values.to_sentence}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def failure_message
|
34
|
+
"expected allow #{@attribute} to be set to #{@good_values.to_sentence}, but it didn't"
|
35
|
+
end
|
36
|
+
|
37
|
+
def negative_failure_message
|
38
|
+
"expected allow #{@attribute} not to be set to #{@good_values.to_sentence}, but it did"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Ensures that the attribute can be set to the given values.
|
43
|
+
#
|
44
|
+
# If an instance variable has been created in the setup named after the
|
45
|
+
# model being tested, then this method will use that. Otherwise, it will
|
46
|
+
# create a new instance to test against.
|
47
|
+
#
|
48
|
+
# Example:
|
49
|
+
# it { Book.should allow_values_for(:isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0") }
|
50
|
+
# it { Book.should_not allow_values_for(:isbn, "bad 1", "bad 2") }
|
51
|
+
#
|
52
|
+
def allow_values_for(attribute, *good_values)
|
53
|
+
Remarkable::Syntax::RSpec::AllowValuesFor.new(attribute, *good_values)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module Shoulda
|
58
|
+
# Ensures that the attribute can be set to the given values.
|
59
|
+
#
|
60
|
+
# If an instance variable has been created in the setup named after the
|
61
|
+
# model being tested, then this method will use that. Otherwise, it will
|
62
|
+
# create a new instance to test against.
|
63
|
+
#
|
64
|
+
# Example:
|
65
|
+
# should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
|
66
|
+
#
|
67
|
+
def should_allow_values_for(attribute, *good_values)
|
68
|
+
get_options!(good_values)
|
69
|
+
klass = model_class
|
70
|
+
good_values.each do |v|
|
71
|
+
it "should allow #{attribute} to be set to #{v.inspect}" do
|
72
|
+
assert_good_value(klass, attribute, v).should be_true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Ensures that the attribute cannot be set to the given values
|
78
|
+
#
|
79
|
+
# If an instance variable has been created in the setup named after the
|
80
|
+
# model being tested, then this method will use that. Otherwise, it will
|
81
|
+
# create a new instance to test against.
|
82
|
+
#
|
83
|
+
# Options:
|
84
|
+
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
85
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
|
86
|
+
#
|
87
|
+
# Example:
|
88
|
+
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
|
89
|
+
#
|
90
|
+
def should_not_allow_values_for(attribute, *bad_values)
|
91
|
+
message = get_options!(bad_values, :message)
|
92
|
+
message ||= default_error_message(:invalid)
|
93
|
+
klass = model_class
|
94
|
+
bad_values.each do |v|
|
95
|
+
it "should not allow #{attribute} to be set to #{v.inspect}" do
|
96
|
+
assert_bad_value(klass, attribute, v, message).should be_true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|