with_model 0.1.4 → 0.1.5

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.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.1.5
2
+
3
+ WithModel::Base is now marked as an abstract_class, which makes polymorphic
4
+ belongs_to work properly.
5
+
1
6
  ### 0.1.4
2
7
 
3
8
  Add ability to pass arguments to create_table.
@@ -5,6 +5,7 @@ end
5
5
 
6
6
  module WithModel
7
7
  class Base < ActiveRecord::Base
8
+ self.abstract_class = true
8
9
  class << self
9
10
  def with_model?
10
11
  true
@@ -1,3 +1,3 @@
1
1
  module WithModel
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -1,60 +1,103 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "ActiveRecord behaviors" do
4
- describe "a temporary ActiveRecord model created with with_model that has a named_scope" do
5
- before do
6
- class RegularModel < ActiveRecord::Base
7
- scope_method =
8
- if respond_to?(:scope) && !protected_methods.include?('scope')
9
- :scope # ActiveRecord 3.x
10
- else
11
- :named_scope # ActiveRecord 2.x
12
- end
13
-
14
- send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
15
- end
16
- RegularModel.connection.drop_table(RegularModel.table_name) rescue nil
17
- RegularModel.connection.create_table(RegularModel.table_name) do |t|
18
- t.string 'title'
19
- t.text 'content'
20
- t.timestamps
4
+ describe "a temporary ActiveRecord model created with with_model" do
5
+ context "that has a named_scope" do
6
+ before do
7
+ class RegularModel < ActiveRecord::Base
8
+ scope_method =
9
+ if respond_to?(:scope) && !protected_methods.include?('scope')
10
+ :scope # ActiveRecord 3.x
11
+ else
12
+ :named_scope # ActiveRecord 2.x
13
+ end
14
+
15
+ send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
16
+ end
17
+ RegularModel.connection.drop_table(RegularModel.table_name) rescue nil
18
+ RegularModel.connection.create_table(RegularModel.table_name) do |t|
19
+ t.string 'title'
20
+ t.text 'content'
21
+ t.timestamps
22
+ end
21
23
  end
22
- end
23
24
 
24
- after do
25
- RegularModel.connection.drop_table(@model.table_name) rescue nil
26
- end
25
+ after do
26
+ RegularModel.connection.drop_table(@model.table_name) rescue nil
27
+ end
27
28
 
28
- with_model :blog_post do
29
- table do |t|
30
- t.string 'title'
31
- t.text 'content'
32
- t.timestamps
29
+ with_model :blog_post do
30
+ table do |t|
31
+ t.string 'title'
32
+ t.text 'content'
33
+ t.timestamps
34
+ end
35
+
36
+ model do
37
+ scope_method =
38
+ if respond_to?(:scope) && !protected_methods.include?('scope')
39
+ :scope # ActiveRecord 3.x
40
+ else
41
+ :named_scope # ActiveRecord 2.x
42
+ end
43
+
44
+ send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
45
+ end
33
46
  end
34
47
 
35
- model do
36
- scope_method =
37
- if respond_to?(:scope) && !protected_methods.include?('scope')
38
- :scope # ActiveRecord 3.x
39
- else
40
- :named_scope # ActiveRecord 2.x
41
- end
48
+ describe "the named scope" do
49
+ it "should work like a regular named scope" do
50
+ included = RegularModel.create!(:title => 'foo', :content => "Include me!")
51
+ excluded = RegularModel.create!(:title => 'bar', :content => "Include me!")
52
+
53
+ RegularModel.title_is_foo.should == [included]
42
54
 
43
- send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
55
+ included = BlogPost.create!(:title => 'foo', :content => "Include me!")
56
+ excluded = BlogPost.create!(:title => 'bar', :content => "Include me!")
57
+
58
+ BlogPost.title_is_foo.should == [included]
59
+ end
44
60
  end
45
61
  end
46
62
 
47
- describe "the named scope" do
48
- it "should work like a regular named scope" do
49
- included = RegularModel.create!(:title => 'foo', :content => "Include me!")
50
- excluded = RegularModel.create!(:title => 'bar', :content => "Include me!")
63
+ context "that has a polymorphic belongs_to" do
64
+ before do
65
+ class Animal < ActiveRecord::Base
66
+ has_many :tea_cups, :as => :pet
67
+ end
68
+ end
69
+
70
+ with_model :tea_cup do
71
+ table do |t|
72
+ t.belongs_to :pet, :polymorphic => true
73
+ end
74
+ model do
75
+ belongs_to :pet, :polymorphic => true
76
+ end
77
+ end
78
+
79
+ with_table :animals
80
+
81
+ with_model :stuffed_animal do
82
+ table
83
+ model do
84
+ has_many :tea_cups, :as => :pet
85
+ end
86
+ end
51
87
 
52
- RegularModel.title_is_foo.should == [included]
88
+ describe "the polymorphic belongs_to" do
89
+ it "should work like a regular polymorphic belongs_to" do
90
+ animal = Animal.create!
91
+ stuffed_animal = StuffedAnimal.create!
53
92
 
54
- included = BlogPost.create!(:title => 'foo', :content => "Include me!")
55
- excluded = BlogPost.create!(:title => 'bar', :content => "Include me!")
93
+ tea_cup_for_animal = TeaCup.create!(:pet => animal)
94
+ tea_cup_for_animal.pet_type.should == "Animal"
95
+ animal.tea_cups.should include(tea_cup_for_animal)
56
96
 
57
- BlogPost.title_is_foo.should == [included]
97
+ tea_cup_for_stuffed_animal = TeaCup.create!(:pet => stuffed_animal)
98
+ tea_cup_for_stuffed_animal.pet_type.should == "StuffedAnimal"
99
+ stuffed_animal.tea_cups.should include(tea_cup_for_stuffed_animal)
100
+ end
58
101
  end
59
102
  end
60
103
  end
data/spec/spec_helper.rb CHANGED
@@ -18,4 +18,26 @@ end
18
18
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
19
19
 
20
20
  # For readme_spec.rb
21
- module SomeModule; end
21
+ module SomeModule; end
22
+
23
+ if defined?(ActiveModel)
24
+ shared_examples_for "ActiveModel" do
25
+ require 'test/unit/assertions'
26
+ require 'active_model/lint'
27
+ include Test::Unit::Assertions
28
+ include ActiveModel::Lint::Tests
29
+
30
+ # to_s is to support ruby-1.9
31
+ ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
32
+ example m.gsub('_',' ') do
33
+ begin
34
+ send m
35
+ rescue
36
+ puts $!.message
37
+ end
38
+ end
39
+ end
40
+
41
+ before { @model = subject }
42
+ end
43
+ end
@@ -38,6 +38,13 @@ describe "a temporary ActiveRecord model created with with_model" do
38
38
  }.should raise_error(ActiveRecord::RecordNotFound)
39
39
  end
40
40
 
41
+ if defined?(ActiveModel)
42
+ describe "the class" do
43
+ subject { BlogPost.new }
44
+ it_should_behave_like "ActiveModel"
45
+ end
46
+ end
47
+
41
48
  it "should have methods defined in its model block" do
42
49
  blog_post.new(:title => 'New blog post').fancy_title.should == "Title: New blog post"
43
50
  end
@@ -51,6 +58,10 @@ describe "a temporary ActiveRecord model created with with_model" do
51
58
  BlogPost.with_model?.should be_true
52
59
  end
53
60
  end
61
+
62
+ it "should have a base_class of itself" do
63
+ BlogPost.base_class.should == BlogPost
64
+ end
54
65
  end
55
66
 
56
67
  context "after an example which uses with_model without shadowing an existing constant" do
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_model
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.4
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 5
10
+ version: 0.1.5
6
11
  platform: ruby
7
12
  authors:
8
13
  - Case Commons, LLC
@@ -10,7 +15,7 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-03-30 00:00:00 -04:00
18
+ date: 2011-05-18 00:00:00 -04:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
@@ -21,9 +26,19 @@ dependencies:
21
26
  requirements:
22
27
  - - ">="
23
28
  - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 5
24
34
  version: 2.3.5
25
35
  - - <
26
36
  - !ruby/object:Gem::Version
37
+ hash: 63
38
+ segments:
39
+ - 4
40
+ - 0
41
+ - 0
27
42
  version: 4.0.0
28
43
  type: :runtime
29
44
  version_requirements: *id001
@@ -39,6 +54,7 @@ extra_rdoc_files: []
39
54
  files:
40
55
  - .autotest
41
56
  - .gitignore
57
+ - .rspec
42
58
  - CHANGELOG
43
59
  - Gemfile
44
60
  - LICENSE
@@ -70,17 +86,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
86
  requirements:
71
87
  - - ">="
72
88
  - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
73
92
  version: "0"
74
93
  required_rubygems_version: !ruby/object:Gem::Requirement
75
94
  none: false
76
95
  requirements:
77
96
  - - ">="
78
97
  - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
79
101
  version: "0"
80
102
  requirements: []
81
103
 
82
104
  rubyforge_project:
83
- rubygems_version: 1.5.2
105
+ rubygems_version: 1.3.7
84
106
  signing_key:
85
107
  specification_version: 3
86
108
  summary: Dynamically build a model within an Rspec context