validate_block 0.1.0 → 0.2.0

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.
@@ -1,3 +1,13 @@
1
+ === 0.2.0 2010-09-13
2
+
3
+ * changed method_missing to accept params in *args to fix bug where
4
+ 'validates_presence_of' would fail if more than one attribute was
5
+ given.
6
+
7
+ === 0.1.1 2010-09-10
8
+
9
+ * added specs from justinbaker
10
+
1
11
  === 0.1 2010-08-20
2
12
 
3
13
  * Initial release
@@ -8,14 +8,14 @@ This gem allows similar ActiveRecord validates_* commands to be grouped together
8
8
 
9
9
  How often have you had a block of validation commands in an ActiveRecord object that are repeated, especially :id or :unless options? Does this look familiar?
10
10
 
11
- validates_presence_of :hair, :unless => :bald?
11
+ validates_presence_of :hair, :hair_color, :unless => :bald?
12
12
  validates_length_of :hair, :within => 3..15, :unless => :bald?
13
13
  validates_inclusion_of :hair_color, :in => HAIR_COLORS, :unless => bald?
14
14
 
15
15
  Instead, this gem will allow you to replace the above code with:
16
16
 
17
17
  validate_block :unless => :bald? do
18
- presence_of :hair
18
+ presence_of :hair, :hair_color
19
19
  length_of :hair, :within => 3..15
20
20
  inclusion_of :hair_color, :in => HAIR_COLORS
21
21
  end
@@ -24,29 +24,26 @@ Instead, this gem will allow you to replace the above code with:
24
24
 
25
25
  Basically, this gem 1) removes the requirement to have 'validates_' on the front of the commands and 2) passes the options on the validate_block command to each validation command inside the block.
26
26
 
27
- The syntax of the validation commands remains the same. Keeping the 'validate_*' prefix on the commands inside the block _will_ work but it is not required.
28
-
29
- WARNING: There are no specs for this yet because I suck. Actually, it's because I don't properly understand how to spec a module like this so any help on that front would be appreciated.
27
+ The syntax of the validation commands remains the same. Keeping the 'validates_*' prefix on the commands inside the block _will_ work but it is not required.
30
28
 
31
29
  == SYNOPSIS:
32
30
 
33
- require 'rubygems'
34
- require 'activerecord'
35
- require 'validate_block'
31
+ require 'rubygems'
32
+ require 'activerecord'
33
+ require 'validate_block'
36
34
 
37
- class SomeObject < ActiveRecord::Base
35
+ class SomeObject < ActiveRecord::Base
38
36
 
39
- validate_block :if => :some_method? do
40
- presence_of :some_field
41
- inclusion_of :some_other_field, :in => SOME_VALUES
42
- end
37
+ validate_block :if => :some_method? do
38
+ presence_of :some_field
39
+ inclusion_of :some_other_field, :in => SOME_VALUES
40
+ end
43
41
 
44
- end
42
+ end
45
43
 
46
44
  == REQUIREMENTS:
47
45
 
48
- ActiveRecord >= 2.1.0
49
- Rspec (for specs)
46
+ ActiveRecord >= 2.1.0
50
47
 
51
48
  == INSTALL:
52
49
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'hoe'
6
6
  require 'fileutils'
7
7
  require './lib/validate_block'
8
8
 
9
- VERSION = "0.1.0"
9
+ VERSION = "0.2.0"
10
10
 
11
11
  Hoe.plugin :newgem
12
12
 
@@ -9,11 +9,17 @@ module ActiveRecord
9
9
  yield
10
10
  end
11
11
 
12
- def method_missing(name, attrib, opts = {}, &block)
12
+ def method_missing(name, *args, &block)
13
+ if args.last.class == Hash
14
+ args.last.merge!(@block_opts)
15
+ else
16
+ args << @block_opts
17
+ end
18
+
13
19
  if name.to_s =~ /^validates_/
14
- send(name, attrib, opts.merge(@block_opts))
20
+ send(name, *args)
15
21
  elsif respond_to?(("validates_"+name.to_s).to_sym)
16
- send(("validates_"+name.to_s).to_sym, attrib, opts.merge(@block_opts))
22
+ send(("validates_"+name.to_s).to_sym, *args)
17
23
  else
18
24
  super
19
25
  end
@@ -7,4 +7,21 @@ rescue LoadError
7
7
  end
8
8
 
9
9
  $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'rubygems'
11
+ require 'active_record'
12
+ require 'active_record/version'
13
+ require 'action_controller'
14
+ require 'action_view'
15
+ require 'action_mailer'
16
+
17
+
18
+ ActiveRecord::Migration.verbose = false
19
+ ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
20
+ ActiveRecord::Schema.define do
21
+ create_table :people do |table|
22
+ table.column :hair, :string
23
+ table.column :hair_color, :string
24
+ end
25
+ end
26
+
10
27
  require 'validate_block'
@@ -1,5 +1,40 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
+ class Person < ActiveRecord::Base
4
+
5
+ HAIR_COLORS = %w{blond brown black}
6
+
7
+ def bald?
8
+ hair.nil?
9
+ end
10
+
11
+ validate_block :unless => :bald? do
12
+ presence_of :hair, :hair_color
13
+ length_of :hair, :within => 3..15
14
+ inclusion_of :hair_color, :in => HAIR_COLORS
15
+ end
16
+
17
+ end
3
18
  describe "validate_block" do
4
- # there wer currently no specs, I'm lazy and don't understand how to spec a module like this.
19
+
20
+ before(:each) do
21
+ @person = Person.new
22
+ end
23
+
24
+ it "returns true" do
25
+ @person.save.should == true #:nohair:
26
+ end
27
+
28
+ it "returns an error for long hair" do
29
+ @person.hair = "a lot of effin hair mannnnn"
30
+ @person.save.should == false
31
+ end
32
+
33
+ it "returns an error for red hair" do
34
+
35
+ @person.hair = "short"
36
+ @person.hair_color = "red"
37
+ @person.save.should == false
38
+ end
39
+
5
40
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate_block
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthew Nielsen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-20 00:00:00 -06:00
18
+ date: 2010-09-13 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -55,14 +55,14 @@ description: |-
55
55
 
56
56
  How often have you had a block of validation commands in an ActiveRecord object that are repeated, especially :id or :unless options? Does this look familiar?
57
57
 
58
- validates_presence_of :hair, :unless => :bald?
58
+ validates_presence_of :hair, :hair_color, :unless => :bald?
59
59
  validates_length_of :hair, :within => 3..15, :unless => :bald?
60
60
  validates_inclusion_of :hair_color, :in => HAIR_COLORS, :unless => bald?
61
61
 
62
62
  Instead, this gem will allow you to replace the above code with:
63
63
 
64
64
  validate_block :unless => :bald? do
65
- presence_of :hair
65
+ presence_of :hair, :hair_color
66
66
  length_of :hair, :within => 3..15
67
67
  inclusion_of :hair_color, :in => HAIR_COLORS
68
68
  end
@@ -71,9 +71,7 @@ description: |-
71
71
 
72
72
  Basically, this gem 1) removes the requirement to have 'validates_' on the front of the commands and 2) passes the options on the validate_block command to each validation command inside the block.
73
73
 
74
- The syntax of the validation commands remains the same. Keeping the 'validate_*' prefix on the commands inside the block _will_ work but it is not required.
75
-
76
- WARNING: There are no specs for this yet because I suck. Actually, it's because I don't properly understand how to spec a module like this so any help on that front would be appreciated.
74
+ The syntax of the validation commands remains the same. Keeping the 'validates_*' prefix on the commands inside the block _will_ work but it is not required.
77
75
  email:
78
76
  - xunker@pyxidis.org
79
77
  executables: []