remarkable_activerecord 3.1.0 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ * validate_length_of accepts :with_kind_of to enable it to work with associations [#69]
2
+ In your Post specs now you can write:
3
+
4
+ should_validate_length_of :comments, :maximum => 10, :with_kind_of => Comment
5
+
1
6
  # v3.1
2
7
 
3
8
  * Allow validate_presence_of to work with associations [#63]
@@ -5,9 +5,10 @@ module Remarkable
5
5
  arguments :collection => :attributes, :as => :attribute
6
6
 
7
7
  optional :within, :alias => :in
8
- optional :minimum, :maximum, :is
8
+ optional :minimum, :maximum, :is
9
+ optional :with_kind_of
9
10
  optional :allow_nil, :allow_blank, :default => true
10
- optional :message, :too_short, :too_long, :wrong_length
11
+ optional :message, :too_short, :too_long, :wrong_length
11
12
 
12
13
  collection_assertions :less_than_min_length?, :exactly_min_length?,
13
14
  :more_than_max_length?, :exactly_max_length?,
@@ -55,8 +56,14 @@ module Remarkable
55
56
  @max_value.nil? || @min_value == @max_value || good?(value_for_length(@max_value), default_message_for(:too_long))
56
57
  end
57
58
 
58
- def value_for_length(value)
59
- "x" * value
59
+ def value_for_length(value)
60
+ repeatable = if @options[:with_kind_of]
61
+ [ @options[:with_kind_of].new ]
62
+ else
63
+ "x"
64
+ end
65
+
66
+ repeatable * value
60
67
  end
61
68
 
62
69
  def interpolation_options
@@ -96,6 +103,13 @@ module Remarkable
96
103
  # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % range.last</tt>
97
104
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
98
105
  # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % value</tt>
106
+ #
107
+ # It also accepts an extra option called :with_kind_of. If you are validating
108
+ # the size of an association array, you have to specify the kind of the array
109
+ # being validated. For example, if your post accepts maximum 10 comments, you
110
+ # can do:
111
+ #
112
+ # validate_length_of :comments, :maximum => 10, :with_kind_of => Comment
99
113
  #
100
114
  # == Gotcha
101
115
  #
@@ -203,7 +203,9 @@ en:
203
203
  minimum:
204
204
  positive: "is minimum {{inspect}} characters"
205
205
  is:
206
- positive: "is equal to {{inspect}} characters"
206
+ positive: "is equal to {{inspect}} characters"
207
+ with_kind_of:
208
+ positive: "with kind of {{value}}"
207
209
 
208
210
  validate_numericality_of:
209
211
  description: "ensure numericality of {{attributes}}"
@@ -143,6 +143,29 @@ describe 'validate_length_of' do
143
143
  it { should define_and_validate(:is => 3).is(3) }
144
144
  it { should_not define_and_validate(:is => 3).is(2) }
145
145
  it { should_not define_and_validate(:is => 3).is(4) }
146
+ end
147
+
148
+ describe "with with kind of" do
149
+ def define_and_validate(options)
150
+ define_model :variant, :product_id => :integer
151
+
152
+ @model = define_model :product do
153
+ has_many :variants
154
+ validates_length_of :variants, options
155
+ end
156
+
157
+ validate_length_of(:variants)
158
+ end
159
+
160
+ it { should define_and_validate(:within => 3..6).within(3..6).with_kind_of(Variant) }
161
+ it { should_not define_and_validate(:within => 2..6).within(3..6).with_kind_of(Variant) }
162
+ it { should_not define_and_validate(:within => 3..7).within(3..6).with_kind_of(Variant) }
163
+
164
+ it "should raise association type mismatch if with_kind_of is not supplied" do
165
+ lambda {
166
+ should_not define_and_validate(:within => 3..6).within(3..6)
167
+ }.should raise_error(ActiveRecord::AssociationTypeMismatch)
168
+ end
146
169
  end
147
170
 
148
171
  # Those are macros to test optionals which accept only boolean values
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remarkable_activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Brando
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-05-04 00:00:00 +02:00
14
+ date: 2009-05-13 00:00:00 +02:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,7 @@ dependencies:
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: 3.1.0
25
+ version: 3.1.1
26
26
  version:
27
27
  description: "Remarkable ActiveRecord: collection of matchers and macros with I18n for ActiveRecord"
28
28
  email: