polymorphic_model 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -3,18 +3,34 @@ module PolymorphicModel
3
3
  def polymorphic_model(options = {})
4
4
  if options[:with_type_column]
5
5
  @_polymorphic_column = options[:with_type_column]
6
+ @_model_types = []
6
7
  extend PolymorphicModel::ClassMethods
7
8
  end
8
9
  end
9
10
  end
10
11
 
11
12
  module ClassMethods
13
+ def types
14
+ @_model_types.clone.freeze
15
+ end
16
+
17
+ def validates_type
18
+ validates_each @_polymorphic_column, {:on => :save} do |record, attr_name, value|
19
+ unless self.types.include?(value.to_sym)
20
+ record.errors.add_to_base("is undefined type (#{value.to_s}), correct types are: [#{self.types.map(&:to_s).join(', ')}]")
21
+ end
22
+ end
23
+ end
24
+
12
25
  def define_type(t, options = {})
13
26
  column = @_polymorphic_column
27
+ @_model_types << t.to_sym
14
28
  define_method :"#{t.to_s}?" do
15
29
  send(column) == t.to_s
16
30
  end
17
31
 
32
+ validates_type
33
+
18
34
  condition_hash = {column => t.to_s}
19
35
  if options[:singleton] == true
20
36
  validates_uniqueness_of column, :if => :"#{t}?"
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{polymorphic_model}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Artur Roszczyk"]
12
- s.date = %q{2009-11-21}
12
+ s.date = %q{2009-11-23}
13
13
  s.description = %q{Alternative for ActiveRecord's Single Table Inheritance}
14
14
  s.email = %q{artur.roszczyk@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -54,6 +54,14 @@ describe "When normal collection types are defined" do
54
54
  Job.internal.count.should == 3
55
55
  end
56
56
  end
57
+
58
+ it "should allow only defined types" do
59
+ Job.new(:job_type => "other").should_not be_valid
60
+ end
61
+
62
+ it "should provide list of defined types" do
63
+ Job.types.should include(:internal, :external)
64
+ end
57
65
  end
58
66
 
59
67
  describe "When singleton type is defined" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorphic_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Roszczyk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-21 00:00:00 +01:00
12
+ date: 2009-11-23 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency