polymorphic_model 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = polymorphic_model
2
2
 
3
- Description goes here.
3
+ This is simple substitute for ActiveRecord's STI mechanism.
4
4
 
5
5
  == Note on Patches/Pull Requests
6
6
 
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/sevos/polymorphic_model"
12
12
  gem.authors = ["Artur Roszczyk"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "sqlite3-ruby", ">= 1.2.4"
14
15
 
15
16
  gem.add_dependency "activerecord", ">= 2.3.4"
16
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -10,18 +10,18 @@ module PolymorphicModel
10
10
 
11
11
  module ClassMethods
12
12
  def define_type(t, options = {})
13
+ column = @_polymorphic_column
13
14
  define_method :"#{t.to_s}?" do
14
- offer_type == t.to_s
15
+ send(column) == t.to_s
15
16
  end
16
17
 
17
18
  if options[:singleton] == true
18
- validates_uniqueness_of @_polymorphic_column, :if => :"#{t}?"
19
+ validates_uniqueness_of column, :if => :"#{t}?"
19
20
  self.class.instance_eval do
20
-
21
21
  define_method t do
22
- existing = find(:first, :conditions => {@_polymorphic_column => t.to_s})
22
+ existing = find(:first, :conditions => {column => t.to_s})
23
23
  if options[:autocreate]
24
- existing || create!(@_polymorphic_column => t.to_s)
24
+ existing || create!(column => t.to_s)
25
25
  else
26
26
  existing
27
27
  end
@@ -29,7 +29,7 @@ module PolymorphicModel
29
29
 
30
30
  end
31
31
  else
32
- named_scope(t.to_s, :conditions => "#{@_polymorphic_column} = '#{t.to_s}'")
32
+ named_scope(t, :conditions => { column => "#{t.to_s}" })
33
33
  end
34
34
  end
35
35
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{polymorphic_model}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
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"]
@@ -27,6 +27,8 @@ Gem::Specification.new do |s|
27
27
  "lib/polymorphic_model.rb",
28
28
  "polymorphic_model.gemspec",
29
29
  "rails/init.rb",
30
+ "spec/lib/database.rb",
31
+ "spec/lib/job_model.rb",
30
32
  "spec/polymorphic_model_spec.rb",
31
33
  "spec/spec.opts",
32
34
  "spec/spec_helper.rb"
@@ -37,7 +39,9 @@ Gem::Specification.new do |s|
37
39
  s.rubygems_version = %q{1.3.5}
38
40
  s.summary = %q{Alternative for ActiveRecord's Single Table Inheritance}
39
41
  s.test_files = [
40
- "spec/polymorphic_model_spec.rb",
42
+ "spec/lib/database.rb",
43
+ "spec/lib/job_model.rb",
44
+ "spec/polymorphic_model_spec.rb",
41
45
  "spec/spec_helper.rb"
42
46
  ]
43
47
 
@@ -47,13 +51,16 @@ Gem::Specification.new do |s|
47
51
 
48
52
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
53
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
54
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.2.4"])
50
55
  s.add_runtime_dependency(%q<activerecord>, [">= 2.3.4"])
51
56
  else
52
57
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.4"])
53
59
  s.add_dependency(%q<activerecord>, [">= 2.3.4"])
54
60
  end
55
61
  else
56
62
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
63
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.4"])
57
64
  s.add_dependency(%q<activerecord>, [">= 2.3.4"])
58
65
  end
59
66
  end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ require 'logger'
4
+
5
+ DATABASE_CONFIG = {
6
+ :adapter => 'sqlite3',
7
+ :database => ':memory:'
8
+ }.freeze
9
+ def set_database
10
+ ActiveRecord::Base.establish_connection(DATABASE_CONFIG)
11
+ #ActiveRecord::Base.logger = Logger.new(STDERR)
12
+
13
+ ActiveRecord::Schema.define do
14
+ create_table :jobs do |t|
15
+ t.string :job_type
16
+
17
+ t.string :title
18
+ t.string :url
19
+ t.text :description
20
+
21
+ t.timestamps
22
+ end
23
+ end
24
+ end
25
+ require 'lib/job_model'
@@ -0,0 +1,2 @@
1
+ class Job < ActiveRecord::Base
2
+ end
@@ -1,5 +1,88 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'lib/job_model'
2
3
 
3
4
  describe "PolymorphicModel" do
5
+ before(:all) do
6
+ Job.instance_eval do
7
+ polymorphic_model :with_type_column => :job_type
8
+ end
9
+ end
10
+
11
+ it "should create define_type method" do
12
+ Job.methods.should include("define_type")
13
+ end
14
+
15
+ describe :define_type do
16
+ before do
17
+ Job.instance_eval do
18
+ define_type :some_type
19
+ end
20
+ end
21
+
22
+ it "should create check methods" do
23
+ Job.instance_methods.should include("some_type?")
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "When normal collection types are defined" do
29
+ before do
30
+ Job.instance_eval do
31
+ define_type :internal
32
+ define_type :external
33
+ end
34
+ 2.times { @external = Job.create(:job_type => "external") }
35
+ 3.times { @internal = Job.create(:job_type => "internal") }
36
+ end
37
+
38
+ describe "check methods" do
39
+ it "should return true if object is in kind of method" do
40
+ @external.should be_external
41
+ @internal.should be_internal
42
+ @external.should_not be_internal
43
+ @internal.should_not be_external
44
+ end
45
+ end
46
+
47
+ describe "named scopes" do
48
+ it "should return correct collections" do
49
+ Job.external.count.should == 2
50
+ Job.internal.count.should == 3
51
+ end
52
+ end
53
+
54
+ after do
55
+ Job.destroy_all
56
+ end
57
+ end
58
+
59
+ describe "When singleton type is defined" do
60
+ describe "with autocreate" do
61
+ before do
62
+ Job.instance_eval do
63
+ define_type :basic, :singleton => true, :autocreate => true
64
+ end
65
+ end
66
+
67
+ describe "when object doesn't exist yet" do
68
+ before do
69
+ Job.destroy_all
70
+ end
71
+
72
+ it "should create object" do
73
+ Job.basic.should be_basic
74
+ end
75
+ end
76
+
77
+ describe "when object exists" do
78
+ before do
79
+ Job.basic
80
+ end
81
+
82
+ it "should not allow to create another instance of object" do
83
+ Job.new(:job_type => "basic").should_not be_valid
84
+ end
85
+ end
86
+ end
4
87
 
5
88
  end
data/spec/spec.opts CHANGED
@@ -1 +1 @@
1
- --color
1
+ --color
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,8 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'polymorphic_model'
4
4
  require 'spec'
5
5
  require 'spec/autorun'
6
+ require 'lib/database'
6
7
 
7
8
  Spec::Runner.configure do |config|
8
-
9
+ set_database
9
10
  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.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Roszczyk
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.2.9
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3-ruby
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.4
34
+ version:
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: activerecord
27
37
  type: :runtime
@@ -52,6 +62,8 @@ files:
52
62
  - lib/polymorphic_model.rb
53
63
  - polymorphic_model.gemspec
54
64
  - rails/init.rb
65
+ - spec/lib/database.rb
66
+ - spec/lib/job_model.rb
55
67
  - spec/polymorphic_model_spec.rb
56
68
  - spec/spec.opts
57
69
  - spec/spec_helper.rb
@@ -84,5 +96,7 @@ signing_key:
84
96
  specification_version: 3
85
97
  summary: Alternative for ActiveRecord's Single Table Inheritance
86
98
  test_files:
99
+ - spec/lib/database.rb
100
+ - spec/lib/job_model.rb
87
101
  - spec/polymorphic_model_spec.rb
88
102
  - spec/spec_helper.rb