ball_gag 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -124,6 +124,36 @@ end
124
124
  BallGag.engine = ModerationServiceEngine
125
125
  ````
126
126
 
127
+ ## Cloaking
128
+ If you need flexibility in how the library is used, you can reprogram the API.
129
+ First, an alternative top-level module called <tt>BowlingBall</tt> is provided. Include <tt>BowlingBall</tt> in your models to get access to the same functionality you would get with <tt>BallGag</tt>.
130
+ You can also specify an alternate verb for gagging attributes, and an alternate preterite form of the verb for querying the attribute. The preterite form supplied also makes positive and negative validations of the same name available.
131
+
132
+ ````ruby
133
+ BowlingBall.verb = 'censor'
134
+ BowlingBall.preterite = 'censored'
135
+
136
+ class Post < ActiveRecord::Base
137
+ validates :text, not_censored: true
138
+ censor(:text) { |text| !/damn/.match text }
139
+ end
140
+ ````
141
+
142
+ You may also indicate that the preterite form of the verb has an inverted meaning.
143
+
144
+ ````ruby
145
+ BowlingBall.verb = 'censor'
146
+ BowlingBall.negative_preterite = 'acceptable'
147
+
148
+ class Post < ActiveRecord::Base
149
+ validates :text, acceptable: true
150
+ censor(:text) { |text| !/damn/.match text }
151
+ end
152
+
153
+ Post.new(text: 'That was some damn fine watermelon').text_acceptable?
154
+ # false
155
+ ````
156
+
127
157
  ## License
128
158
 
129
159
  MIT License
@@ -7,18 +7,30 @@ class GaggedValidator < ActiveModel::EachValidator
7
7
 
8
8
  private
9
9
  def condition_method_name attribute
10
- "#{attribute}_#{BallGag.preterite}?"
10
+ BallGag.preterite_negative? ?
11
+ neg_method_name(attribute) :
12
+ pos_method_name(attribute)
11
13
  end
12
14
 
13
15
  def default_message
14
16
  "is not #{BallGag.preterite}"
15
17
  end
18
+
19
+ def neg_method_name attribute
20
+ "#{attribute}_not_#{BallGag.preterite}?"
21
+ end
22
+
23
+ def pos_method_name attribute
24
+ "#{attribute}_#{BallGag.preterite}?"
25
+ end
16
26
  end
17
27
 
18
28
  class NotGaggedValidator < GaggedValidator
19
29
  private
20
30
  def condition_method_name attribute
21
- "#{attribute}_not_#{BallGag.preterite}?"
31
+ BallGag.preterite_negative? ?
32
+ pos_method_name(attribute) :
33
+ neg_method_name(attribute)
22
34
  end
23
35
 
24
36
  def default_message
@@ -1,4 +1,4 @@
1
1
  module BallGag
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
4
4
 
data/spec/cloak_spec.rb CHANGED
@@ -104,6 +104,16 @@ describe 'BallGag cloaking' do
104
104
  instance.valid?
105
105
  instance.errors[:words].should include 'is censored'
106
106
  end
107
+
108
+ specify 'negative preterite should not alter meaning of extant validators' do
109
+ BallGag.negative_preterite = 'acceptable'
110
+
111
+ ExampleActiveModel.gag(:words) { |words| false }
112
+ ExampleActiveModel.validates :words, not_gagged: true
113
+ instance = ExampleActiveModel.new
114
+ instance.valid?
115
+ instance.errors[:words].should include 'is acceptable'
116
+ end
107
117
  end
108
118
 
109
119
  describe BowlingBall do
@@ -31,6 +31,22 @@ describe 'ActiveModel::Validations integration' do
31
31
  instance.valid?
32
32
  instance.errors[:words].should include 'is not acceptable'
33
33
  end
34
+
35
+ it 'should respect :allow_blank option' do
36
+ mock_words = mock('words')
37
+
38
+ ExampleActiveModel.gag(:words) { |words| false }
39
+ ExampleActiveModel.validates :words,
40
+ not_gagged: { allow_blank: true }
41
+
42
+ instance = ExampleActiveModel.new
43
+ instance.stub!(words: mock_words)
44
+ mock_words.should_receive(:blank?).
45
+ and_return(true)
46
+
47
+ instance.should_not_receive(:words_not_gagged?)
48
+ instance.valid?
49
+ end
34
50
  end
35
51
 
36
52
  describe GaggedValidator do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ball_gag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70299098639740 !ruby/object:Gem::Requirement
16
+ requirement: &70253003020680 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70299098639740
24
+ version_requirements: *70253003020680
25
25
  description: Validate user input using pluggable back-ends
26
26
  email:
27
27
  - duncan@dweebd.com