bellmyer-validates_blacklist 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,8 @@ module Bellmyer
11
11
  unless included_modules.include? InstanceMethods
12
12
  class_inheritable_accessor :options
13
13
  class_inheritable_accessor :model
14
+ class_inheritable_accessor :blacklist_file
15
+ class_inheritable_accessor :blacklist_attributes
14
16
 
15
17
  extend ClassMethods
16
18
  include InstanceMethods
@@ -20,17 +22,37 @@ module Bellmyer
20
22
  end
21
23
 
22
24
  self.options = options
23
- self.model = self.to_s.underscore
25
+ self.model = self.to_s.underscore
26
+ self.blacklist_file = "#{RAILS_ROOT}/config/blacklists/#{self.to_s.underscore}_blacklist.yml"
24
27
  end
25
28
 
26
29
  module ClassMethods
27
- def blacklist
28
- puts "blacklisted!"
30
+ def blacklist(attribute, value, message = nil)
31
+ attribute = attribute.to_s
32
+
33
+ load_blacklist
34
+ self.blacklist_attributes[attribute] ||= []
35
+ if message.nil?
36
+ self.blacklist_attributes[attribute].reject!{|a| a.is_a?(Array) ? a.first == value : a == value}
37
+ self.blacklist_attributes[attribute] << value
38
+ else
39
+ self.blacklist_attributes[attribute].reject!{|a| a.is_a?(Array) ? a.first == value : a == value}
40
+ self.blacklist_attributes[attribute] << [value, message]
41
+ end
42
+ save_blacklist
29
43
  end
30
44
 
31
45
  def unblacklist
32
46
  puts "Removed from blacklist."
33
47
  end
48
+
49
+ def load_blacklist
50
+ self.blacklist_attributes = YAML.load_file(self.blacklist_file) || {}
51
+ end
52
+
53
+ def save_blacklist
54
+ File.open(self.blacklist_file, 'w'){|f| YAML.dump(self.blacklist_attributes, f)}
55
+ end
34
56
  end
35
57
 
36
58
  module InstanceMethods
@@ -39,10 +61,8 @@ module Bellmyer
39
61
  protected
40
62
 
41
63
  def model_blacklists
42
- blacklist = "#{RAILS_ROOT}/config/blacklists/#{self.class.to_s.underscore}_blacklist.yml"
43
-
44
64
  # Load blacklist #
45
- if attributes = File.open(blacklist){|f| YAML::load(f)}
65
+ if attributes = self.class.load_blacklist
46
66
  # Cycle through attributes #
47
67
  attributes.each do |attribute, stops|
48
68
  attribute = attribute.to_sym
@@ -0,0 +1,11 @@
1
+ name:
2
+ - Stinky Pete
3
+ - [Vanessa, is not allowed after ditching my b-day party]
4
+
5
+ email:
6
+ - stinky_pete@gmail.com
7
+ - [/@aol\.com$/, is banned until they stop sending me CD\'s]
8
+
9
+ age:
10
+ - < 14
11
+ - [\> 25, is too old and pervy!exit]
@@ -1,8 +1,10 @@
1
1
  require 'test/unit'
2
2
  require 'rubygems'
3
3
  require 'active_record'
4
+ require 'ftools'
5
+
4
6
  $:.unshift File.dirname(__FILE__) + '/../lib'
5
- require File.dirname(__FILE__) + '/../init'
7
+ require File.dirname(__FILE__) + '/../lib/validates_blacklist'
6
8
 
7
9
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
8
10
 
@@ -176,3 +178,73 @@ class FriendWithBlankBlacklistTest < Test::Unit::TestCase
176
178
  assert friend.valid?
177
179
  end
178
180
  end
181
+
182
+ class FriendUpdateable < Friend
183
+ validates_blacklist
184
+ end
185
+
186
+ class FriendUpdateableTest < Test::Unit::TestCase
187
+ def setup
188
+ setup_db
189
+ File.copy("#{RAILS_ROOT}/config/blacklists/template.yml", "#{RAILS_ROOT}/config/blacklists/friend_updateable_blacklist.yml")
190
+ end
191
+
192
+ def teardown
193
+ teardown_db
194
+ File.unlink("#{RAILS_ROOT}/config/blacklists/friend_updateable_blacklist.yml")
195
+ end
196
+
197
+ def test_should_add_a_new_blacklist_item_without_message
198
+ FriendUpdateable.blacklist(:name, 'terrible')
199
+
200
+ friend = FriendUpdateable.new(:name => 'terrible')
201
+ assert !friend.valid?
202
+ assert friend.errors.invalid?(:name)
203
+ assert friend.errors.on(:name).include?('is not allowed.')
204
+ end
205
+
206
+ def test_should_add_a_new_blacklist_item_with_message
207
+ FriendUpdateable.blacklist(:name, 'terrible', "is terrible.")
208
+
209
+ friend = FriendUpdateable.new(:name => 'terrible')
210
+ assert !friend.valid?
211
+ assert friend.errors.invalid?(:name)
212
+ assert friend.errors.on(:name).include?('is terrible.')
213
+ end
214
+
215
+ def test_should_update_an_existing_blacklist_item_changing_message_without_adding_item
216
+ FriendUpdateable.blacklist(:name, 'Vanessa', "is terrible.")
217
+
218
+ friend = FriendUpdateable.new(:name => 'Vanessa')
219
+ assert !friend.valid?
220
+ assert friend.errors.invalid?(:name)
221
+ assert friend.errors.on(:name).include?('is terrible.')
222
+
223
+ list = FriendUpdateable.load_blacklist
224
+ assert_equal 1, list['name'].select{|node| node.is_a?(Array) ? node.first == 'Vanessa' : node == 'Vanessa'}.size
225
+ end
226
+
227
+ def test_should_update_an_existing_blacklist_item_adding_message_without_adding_item
228
+ FriendUpdateable.blacklist(:name, 'Stinky Pete', "is terrible.")
229
+
230
+ friend = FriendUpdateable.new(:name => 'Stinky Pete')
231
+ assert !friend.valid?
232
+ assert friend.errors.invalid?(:name)
233
+ assert friend.errors.on(:name).include?('is terrible.')
234
+
235
+ list = FriendUpdateable.load_blacklist
236
+ assert_equal 1, list['name'].select{|node| node.is_a?(Array) ? node.first == 'Stinky Pete' : node == 'Stinky Pete'}.size
237
+ end
238
+
239
+ def test_should_update_an_existing_blacklist_item_removing_message_without_adding_item
240
+ FriendUpdateable.blacklist(:name, 'Vanessa')
241
+
242
+ friend = FriendUpdateable.new(:name => 'Vanessa')
243
+ assert !friend.valid?
244
+ assert friend.errors.invalid?(:name)
245
+ assert friend.errors.on(:name).include?('is not allowed.')
246
+
247
+ list = FriendUpdateable.load_blacklist
248
+ assert_equal 1, list['name'].select{|node| node.is_a?(Array) ? node.first == 'Vanessa' : node == 'Vanessa'}.size
249
+ end
250
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'validates_blacklist'
3
- s.version = '0.1.5'
3
+ s.version = '0.1.6'
4
4
  s.date = '2009-08-26'
5
5
 
6
6
  s.summary = "Allows models to be validated against yaml-based blacklists"
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "test/config/blacklists/friend_with_blacklist_blacklist.yml",
30
30
  "test/config/blacklists/friend_with_blank_blacklist_blacklist.yml",
31
31
  "test/config/blacklists/friend_with_custom_message_blacklist.yml",
32
+ "test/config/blacklists/template.yml",
32
33
  "test/test_helper.rb",
33
34
  "test/validates_blacklist_test.rb"
34
35
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bellmyer-validates_blacklist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Bellmyer
@@ -43,6 +43,7 @@ files:
43
43
  - test/config/blacklists/friend_with_blacklist_blacklist.yml
44
44
  - test/config/blacklists/friend_with_blank_blacklist_blacklist.yml
45
45
  - test/config/blacklists/friend_with_custom_message_blacklist.yml
46
+ - test/config/blacklists/template.yml
46
47
  - test/test_helper.rb
47
48
  - test/validates_blacklist_test.rb
48
49
  has_rdoc: true