polymorphic_model 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +12 -0
- data/VERSION +1 -1
- data/lib/polymorphic_model.rb +25 -1
- data/polymorphic_model.gemspec +2 -2
- data/spec/polymorphic_model_spec.rb +18 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -18,6 +18,7 @@ Simple use case:
|
|
18
18
|
class Configuration < ActiveRecord::Base
|
19
19
|
polymorphic_model :with_type_column => :entry_type
|
20
20
|
define_type :website_name, :singleton => true, :autocreate => true
|
21
|
+
define_type :website_motto, :singleton => true
|
21
22
|
define_type :author
|
22
23
|
end
|
23
24
|
|
@@ -26,6 +27,17 @@ Simple use case:
|
|
26
27
|
@name = Configuration.website_name.value
|
27
28
|
@authors = Configuration.author.map(&:value)
|
28
29
|
|
30
|
+
Configuration.author.create!(:value => "John Smith")
|
31
|
+
|
32
|
+
# this would throw exception (not valid)
|
33
|
+
# Configuration.create!(:entry_type => "website_name")
|
34
|
+
|
35
|
+
Configuration.website_motto
|
36
|
+
=> []
|
37
|
+
Configuration.website_motto.create(:value => "Hello world!")
|
38
|
+
Configuration.website_motto.value
|
39
|
+
=> "Hello world!"
|
40
|
+
|
29
41
|
This is just example. It can be used wherever STI is too drastically splits model file
|
30
42
|
or more than one controller is not what you actually want.
|
31
43
|
You can avoid also problems with polymorphic_path.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/polymorphic_model.rb
CHANGED
@@ -1,19 +1,42 @@
|
|
1
1
|
module PolymorphicModel
|
2
2
|
module Initializer
|
3
|
+
# Initializes model to be a polymorphic one
|
4
|
+
# Example usage:
|
5
|
+
# polymorphic_model :with_type_column => :page_type
|
3
6
|
def polymorphic_model(options = {})
|
4
7
|
if options[:with_type_column]
|
5
|
-
@_polymorphic_column = options[:with_type_column]
|
8
|
+
@_polymorphic_column = options[:with_type_column].to_sym
|
9
|
+
@_polymorphic_column.freeze
|
6
10
|
@_model_types = []
|
7
11
|
extend PolymorphicModel::ClassMethods
|
12
|
+
include PolymorphicModel::InstanceMethods
|
13
|
+
self.instance_eval do
|
14
|
+
define_method :"#{@_polymorphic_column}=" do |value|
|
15
|
+
super(value.to_s)
|
16
|
+
end
|
17
|
+
end
|
8
18
|
end
|
9
19
|
end
|
10
20
|
end
|
11
21
|
|
22
|
+
module InstanceMethods
|
23
|
+
# Returns true if model instance is correct type
|
24
|
+
def valid_type?
|
25
|
+
self.class.types.include?(send(self.class.polymorphic_column_name).to_sym)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
12
29
|
module ClassMethods
|
30
|
+
# Returns list of allowed model types
|
13
31
|
def types
|
14
32
|
@_model_types.clone.freeze
|
15
33
|
end
|
16
34
|
|
35
|
+
def polymorphic_column_name
|
36
|
+
@_polymorphic_column
|
37
|
+
end
|
38
|
+
|
39
|
+
# Type validation for polymorphic model
|
17
40
|
def validates_type
|
18
41
|
validates_each @_polymorphic_column, {:on => :save} do |record, attr_name, value|
|
19
42
|
unless value.nil? || value == ""
|
@@ -26,6 +49,7 @@ module PolymorphicModel
|
|
26
49
|
end
|
27
50
|
end
|
28
51
|
|
52
|
+
# defines new type for model
|
29
53
|
def define_type(t, options = {})
|
30
54
|
column = @_polymorphic_column
|
31
55
|
@_model_types << t.to_sym
|
data/polymorphic_model.gemspec
CHANGED
@@ -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.
|
8
|
+
s.version = "0.0.7"
|
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-
|
12
|
+
s.date = %q{2009-11-28}
|
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 = [
|
@@ -19,6 +19,24 @@ describe "PolymorphicModel" do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
describe :valid_type? do
|
23
|
+
it "should return true if type is on list" do
|
24
|
+
@job = Job.new(:job_type => "some_type")
|
25
|
+
@job.should be_valid_type
|
26
|
+
end
|
27
|
+
it "should return false if type is not on list" do
|
28
|
+
@job = Job.new(:job_type => "incorrect")
|
29
|
+
@job.should_not be_valid_type
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should allow either string and symbol while setting type via setter" do
|
34
|
+
@job = Job.new(:job_type => :some_type)
|
35
|
+
@job.job_type.should == "some_type"
|
36
|
+
@job.job_type = :other_type
|
37
|
+
@job.job_type.should == "other_type"
|
38
|
+
end
|
39
|
+
|
22
40
|
it "should create check methods" do
|
23
41
|
Job.instance_methods.should include("some_type?")
|
24
42
|
end
|
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.
|
4
|
+
version: 0.0.7
|
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-
|
12
|
+
date: 2009-11-28 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|