remarkable_activerecord 3.1.3 → 3.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,7 @@
1
+ * Added :token and :separator to deal with :tokenizer in validates_length_of [#77]
2
+
1
3
  * Deprecated validate_format_of. It does not have the same API as the respective
2
- ActiveRecord macro, raising questions frequentely about its usage. [#76]
4
+ ActiveRecord macro, raising questions frequentely about its usage [#76]
3
5
 
4
6
  * allow_mass_assignment_of when called without arguments checks if any mass
5
7
  assignment is possible [#80]
@@ -26,8 +26,7 @@ module Remarkable
26
26
 
27
27
  def interpolation_options
28
28
  if @subject
29
- array = protected_attributes.to_a
30
- { :protected_attributes => array.empty? ? "[]" : array_to_sentence(array) }
29
+ { :protected_attributes => array_to_sentence(protected_attributes.to_a, false, '[]') }
31
30
  else
32
31
  {}
33
32
  end
@@ -53,7 +53,7 @@ module Remarkable
53
53
  options = if @in_range
54
54
  { :in => (@options[:in].first..@options[:in].last).inspect }
55
55
  elsif @options[:in].is_a?(Array)
56
- { :in => array_to_sentence(@options[:in].map{|i| i.inspect}) }
56
+ { :in => array_to_sentence(@options[:in], true, '[]') }
57
57
  else
58
58
  { :in => @options[:in].inspect }
59
59
  end
@@ -125,7 +125,9 @@ module Remarkable
125
125
  end
126
126
 
127
127
  def interpolation_options
128
- options = { :macro => Remarkable.t(@macro, :scope => matcher_i18n_scope, :default => @macro.to_s), :options => @options.inspect }
128
+ options = {}
129
+ options[:macro] = Remarkable.t(@macro, :scope => matcher_i18n_scope, :default => @macro.to_s.gsub("_", ""))
130
+ options[:options] = @options.inspect
129
131
 
130
132
  if @subject && reflection
131
133
  options.merge!(
@@ -6,7 +6,7 @@ module Remarkable
6
6
 
7
7
  optional :within, :alias => :in
8
8
  optional :minimum, :maximum, :is
9
- optional :with_kind_of
9
+ optional :token, :separator, :with_kind_of
10
10
  optional :allow_nil, :allow_blank, :default => true
11
11
  optional :message, :too_short, :too_long, :wrong_length
12
12
 
@@ -57,13 +57,11 @@ module Remarkable
57
57
  end
58
58
 
59
59
  def value_for_length(value)
60
- repeatable = if @options[:with_kind_of]
61
- [ @options[:with_kind_of].new ]
60
+ if @options[:with_kind_of]
61
+ [@options[:with_kind_of].new] * value
62
62
  else
63
- "x"
63
+ ([@options.fetch(:token, 'x')] * value).join(@options.fetch(:separator, ''))
64
64
  end
65
-
66
- repeatable * value
67
65
  end
68
66
 
69
67
  def interpolation_options
@@ -109,7 +107,17 @@ module Remarkable
109
107
  # being validated. For example, if your post accepts maximum 10 comments, you
110
108
  # can do:
111
109
  #
112
- # validate_length_of :comments, :maximum => 10, :with_kind_of => Comment
110
+ # should_validate_length_of :comments, :maximum => 10, :with_kind_of => Comment
111
+ #
112
+ # Finally, it also accepts :token and :separator, to specify how the
113
+ # tokenizer should work. For example, if you are splitting the attribute
114
+ # per word:
115
+ #
116
+ # validates_length_of :essay, :minimum => 100, :tokenizer => lambda {|str| str.scan(/\w+/) }
117
+ #
118
+ # You could do this:
119
+ #
120
+ # should_validate_length_of :essay, :minimum => 100, :token => "word", :separator => " "
113
121
  #
114
122
  # == Gotcha
115
123
  #
@@ -143,6 +143,31 @@ 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 token and separator options" do
149
+ describe "and words as tokens" do
150
+ before(:each) do
151
+ @matcher = define_and_validate(:within => 3..5, :tokenizer => lambda { |str| str.scan(/\w+/) })
152
+ end
153
+
154
+ it { should @matcher.within(3..5).token("word").separator(" ") }
155
+ it { should_not @matcher.within(2..5).token("word").separator(" ") }
156
+ it { should_not @matcher.within(4..5).token("word").separator(" ") }
157
+ it { should_not @matcher.within(3..4).token("word").separator(" ") }
158
+ it { should_not @matcher.within(3..6).token("word").separator(" ") }
159
+ it { should_not @matcher.within(3..5).token("word").separator("") }
160
+ it { should_not @matcher.within(3..5).token("word").separator(" a ") }
161
+ end
162
+
163
+ describe "and digits as tokens" do
164
+ before(:each) do
165
+ @matcher = define_and_validate(:within => 3..5, :tokenizer => lambda { |str| str.scan(/\d+/) })
166
+ end
167
+
168
+ it { should @matcher.within(3..5).token("1").separator(" ") }
169
+ it { should_not @matcher.within(3..5).token("a").separator("") }
170
+ end
146
171
  end
147
172
 
148
173
  describe "with with kind of" do
@@ -136,7 +136,7 @@ describe 'validate_uniqueness_of' do
136
136
  User.new(:username => 'jose')
137
137
  end
138
138
  end
139
- proc { @matcher.matches?(@model) }.should raise_error(ScriptError)
139
+ lambda { @matcher.matches?(@model) }.should raise_error(ScriptError)
140
140
 
141
141
  User.stub!(:find).and_return do |many, conditions|
142
142
  if many == :all
@@ -145,7 +145,7 @@ describe 'validate_uniqueness_of' do
145
145
  User.new(:username => 'jose')
146
146
  end
147
147
  end
148
- proc { @matcher.matches?(@model) }.should_not raise_error(ScriptError)
148
+ lambda { @matcher.matches?(@model) }.should_not raise_error(ScriptError)
149
149
  end
150
150
 
151
151
  describe 'when null or blank values are not allowed' do
@@ -154,7 +154,6 @@ describe 'validate_uniqueness_of' do
154
154
  validates_uniqueness_of :username, options
155
155
  end
156
156
 
157
- # Create a model
158
157
  User.create(:username => 'jose')
159
158
  validate_uniqueness_of(:username)
160
159
  end
@@ -163,7 +162,7 @@ describe 'validate_uniqueness_of' do
163
162
  it { should define_and_validate(:allow_nil => false).allow_nil(false) }
164
163
 
165
164
  it 'should raise an error if allow nil is true but we cannot save nil values in the database'do
166
- proc { should define_and_validate.allow_nil }.should raise_error(ScriptError, /You declared that username accepts nil values in validate_uniqueness_of, but I cannot save nil values in the database, got/)
165
+ lambda { should define_and_validate.allow_nil }.should raise_error(ScriptError, /You declared that username accepts nil values in validate_uniqueness_of, but I cannot save nil values in the database, got/)
167
166
  end
168
167
  end
169
168
  end
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.3
4
+ version: 3.1.4
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-28 00:00:00 +02:00
14
+ date: 2009-05-29 00:00:00 +02:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
- version: 3.1.3
35
+ version: 3.1.4
36
36
  version:
37
37
  description: "Remarkable ActiveRecord: collection of matchers and macros with I18n for ActiveRecord"
38
38
  email: