with_model 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,5 @@ pkg/*
3
3
  *.gem
4
4
  .bundle
5
5
  .rvmrc
6
+ doc
7
+ gemfiles/**/Gemfile.lock
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.1.2
2
+
3
+ Make Mixico optional.
data/README.rdoc ADDED
@@ -0,0 +1,128 @@
1
+ = with_model
2
+
3
+ * http://github.com/casecommons/with_model/
4
+
5
+ == DESCRIPTION
6
+
7
+ +with_model+ dynamically builds an ActiveRecord model (with table) within an RSpec context. Outside of the context, the model is no longer present.
8
+
9
+ == INSTALL
10
+
11
+ gem install with_model
12
+
13
+ === RSpec 2
14
+
15
+ In spec_helper.rb:
16
+
17
+ RSpec.configure do |config|
18
+ config.extend WithModel
19
+ end
20
+
21
+ === Rspec 1
22
+
23
+ In spec_helper.rb:
24
+
25
+ Spec::Runner.configure do |config|
26
+ config.extend WithModel
27
+ end
28
+
29
+ == USAGE
30
+
31
+ In an RSpec example group, call +with_model+ and inside its block pass it a +table+ block and a +model+ block.
32
+
33
+ require 'spec_helper'
34
+
35
+ describe "A blog post" do
36
+
37
+ with_model :blog_post do
38
+ # The table block works just like a migration.
39
+ table do |t|
40
+ t.string :title
41
+ t.timestamps
42
+ end
43
+
44
+ # The model block works just like the class definition.
45
+ model do
46
+ include SomeModule
47
+ has_many :comments
48
+ validates_presence_of :title
49
+
50
+ def self.some_class_method
51
+ 'chunky'
52
+ end
53
+
54
+ def some_instance_method
55
+ 'bacon'
56
+ end
57
+ end
58
+ end
59
+
60
+ # with_model classes can have associations.
61
+ with_model :comment do
62
+ table do |t|
63
+ t.string :text
64
+ t.belongs_to :blog_post
65
+ t.timestamps
66
+ end
67
+
68
+ model do
69
+ belongs_to :blog_post
70
+ end
71
+ end
72
+
73
+ it "can be accessed as a constant" do
74
+ BlogPost.should be
75
+ end
76
+
77
+ it "can be accessed as a local variable" do
78
+ blog_post.should be
79
+ end
80
+
81
+ it "can be accessed as an instance variable" do
82
+ @blog_post.should be
83
+ end
84
+
85
+ it "has the module" do
86
+ BlogPost.include?(SomeModule).should be_true
87
+ end
88
+
89
+ it "has the class method" do
90
+ BlogPost.some_class_method.should == 'chunky'
91
+ end
92
+
93
+ it "has the instance method" do
94
+ BlogPost.new.some_instance_method.should == 'bacon'
95
+ end
96
+
97
+ it "can do all the things a regular model can" do
98
+ record = BlogPost.new
99
+ record.should_not be_valid
100
+ record.title = "foo"
101
+ record.should be_valid
102
+ record.save.should be_true
103
+ record.reload.should == record
104
+ record.comments.create!(:text => "Lorem ipsum")
105
+ record.comments.count.should == 1
106
+ end
107
+ end
108
+
109
+ describe "another example group" do
110
+ it "should not have the constant anymore" do
111
+ defined?(BlogPost).should be_false
112
+ end
113
+ end
114
+
115
+ === Mixico
116
+
117
+ If you have the +mixico+ gem installed, WithModel will automatically unmix any modules that were mixed into your model class. Without +mixico+, each module will hold a reference to the class for the remainder of your test suite.
118
+
119
+ Usually unmixing isn't necessary, but it can be helpful if you use something like Module#included_in_classes (from Active Support 2.3) in a later test to ask a module which classes it has been mixed into.
120
+
121
+ == REQUIREMENTS
122
+
123
+ * RSpec
124
+ * ActiveRecord 2 or 3
125
+
126
+ == LICENSE:
127
+
128
+ MIT
data/Rakefile CHANGED
@@ -33,3 +33,12 @@ namespace "autotest" do
33
33
  end
34
34
  end
35
35
  end
36
+
37
+ namespace "doc" do
38
+ desc "Generate README and preview in browser"
39
+ task "readme" do
40
+ sh "rdoc -c utf8 README.rdoc && open doc/files/README_rdoc.html"
41
+ end
42
+ end
43
+
44
+ task :default => :spec
@@ -1,4 +1,7 @@
1
- require 'mixico'
1
+ begin
2
+ require 'mixico'
3
+ rescue LoadError
4
+ end
2
5
 
3
6
  module WithModel
4
7
  class Base < ActiveRecord::Base
@@ -7,20 +10,22 @@ module WithModel
7
10
  true
8
11
  end
9
12
 
10
- def include(*args)
11
- @modules_to_unmix ||= []
12
- args.each do |mod|
13
- unless @modules_to_unmix.include?(mod)
14
- @modules_to_unmix << mod
13
+ if defined?(Mixico)
14
+ def include(*args)
15
+ @modules_to_unmix ||= []
16
+ args.each do |mod|
17
+ unless @modules_to_unmix.include?(mod)
18
+ @modules_to_unmix << mod
19
+ end
15
20
  end
21
+ super
16
22
  end
17
- super
18
- end
19
23
 
20
- def _with_model_deconstructor
21
- @modules_to_unmix.each do |mod|
22
- disable_mixin mod
23
- end if defined?(@modules_to_unmix)
24
+ def _with_model_deconstructor
25
+ @modules_to_unmix.each do |mod|
26
+ disable_mixin mod
27
+ end if defined?(@modules_to_unmix)
28
+ end
24
29
  end
25
30
  end
26
31
  end
@@ -32,7 +32,7 @@ module WithModel
32
32
  end
33
33
 
34
34
  example_group.after do
35
- model._with_model_deconstructor
35
+ model._with_model_deconstructor if defined?(Mixico)
36
36
  Object.send(:remove_const, const_name)
37
37
  Object.const_set(const_name, original_const_value) if original_const_defined
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module WithModel
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -4,7 +4,14 @@ describe "ActiveRecord behaviors" do
4
4
  describe "a temporary ActiveRecord model created with with_model that has a named_scope" do
5
5
  before do
6
6
  class RegularModel < ActiveRecord::Base
7
- named_scope :title_is_foo, :conditions => {:title => 'foo'}
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'}
8
15
  end
9
16
  RegularModel.connection.drop_table(RegularModel.table_name) rescue nil
10
17
  RegularModel.connection.create_table(RegularModel.table_name) do |t|
@@ -26,7 +33,14 @@ describe "ActiveRecord behaviors" do
26
33
  end
27
34
 
28
35
  model do
29
- named_scope :title_is_foo, :conditions => {:title => 'foo'}
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
42
+
43
+ send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
30
44
  end
31
45
  end
32
46
 
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe "A blog post" do
4
+
5
+ with_model :blog_post do
6
+ # The table block works just like a migration.
7
+ table do |t|
8
+ t.string :title
9
+ t.timestamps
10
+ end
11
+
12
+ # The model block works just like the class definition.
13
+ model do
14
+ include SomeModule
15
+ has_many :comments
16
+ validates_presence_of :title
17
+
18
+ def self.some_class_method
19
+ 'chunky'
20
+ end
21
+
22
+ def some_instance_method
23
+ 'bacon'
24
+ end
25
+ end
26
+ end
27
+
28
+ # with_model classes can have associations.
29
+ with_model :comment do
30
+ table do |t|
31
+ t.string :text
32
+ t.belongs_to :blog_post
33
+ t.timestamps
34
+ end
35
+
36
+ model do
37
+ belongs_to :blog_post
38
+ end
39
+ end
40
+
41
+ it "can be accessed as a constant" do
42
+ BlogPost.should be
43
+ end
44
+
45
+ it "can be accessed as a local variable" do
46
+ blog_post.should be
47
+ end
48
+
49
+ it "can be accessed as an instance variable" do
50
+ @blog_post.should be
51
+ end
52
+
53
+ it "has the module" do
54
+ BlogPost.include?(SomeModule).should be_true
55
+ end
56
+
57
+ it "has the class method" do
58
+ BlogPost.some_class_method.should == 'chunky'
59
+ end
60
+
61
+ it "has the instance method" do
62
+ BlogPost.new.some_instance_method.should == 'bacon'
63
+ end
64
+
65
+ it "can do all the things a regular model can" do
66
+ record = BlogPost.new
67
+ record.should_not be_valid
68
+ record.title = "foo"
69
+ record.should be_valid
70
+ record.save.should be_true
71
+ record.reload.should == record
72
+ record.comments.create!(:text => "Lorem ipsum")
73
+ record.comments.count.should == 1
74
+ end
75
+ end
76
+
77
+ describe "another example group" do
78
+ it "should not have the constant anymore" do
79
+ defined?(BlogPost).should be_false
80
+ end
81
+ end
data/spec/spec_helper.rb CHANGED
@@ -16,3 +16,6 @@ end
16
16
  # WithModel requires ActiveRecord::Base.connection to be established.
17
17
  # If ActiveRecord already has a connection, as in a Rails app, this is unnecessary.
18
18
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
19
+
20
+ # For readme_spec.rb
21
+ module SomeModule; end
@@ -117,10 +117,12 @@ describe "a temporary ActiveRecord model created with with_model" do
117
117
  end
118
118
  end
119
119
 
120
- context "after a context that uses a mixin" do
121
- it "should not have the mixin" do
122
- lambda { ::ModelWithMixin.new.foo }.should raise_error(NoMethodError)
123
- ::ModelWithMixin.include?(AMixin).should be_false
120
+ if defined?(Mixico)
121
+ context "after a context that uses a mixin" do
122
+ it "should not have the mixin" do
123
+ lambda { ::ModelWithMixin.new.foo }.should raise_error(NoMethodError)
124
+ ::ModelWithMixin.include?(AMixin).should be_false
125
+ end
124
126
  end
125
127
  end
126
128
 
data/with_model.gemspec CHANGED
@@ -18,5 +18,4 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_dependency 'activerecord', '>=2.3.5', '<4.0.0'
21
- s.add_dependency 'mixico'
22
21
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_model
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
4
+ hash: 31
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Case Commons, LLC
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-22 00:00:00 -05:00
18
+ date: 2011-02-03 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,20 +42,6 @@ dependencies:
42
42
  version: 4.0.0
43
43
  type: :runtime
44
44
  version_requirements: *id001
45
- - !ruby/object:Gem::Dependency
46
- name: mixico
47
- prerelease: false
48
- requirement: &id002 !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- hash: 3
54
- segments:
55
- - 0
56
- version: "0"
57
- type: :runtime
58
- version_requirements: *id002
59
45
  description: Dynamically build a model within an Rspec context
60
46
  email:
61
47
  - casecommons-dev@googlegroups.com
@@ -68,20 +54,20 @@ extra_rdoc_files: []
68
54
  files:
69
55
  - .autotest
70
56
  - .gitignore
57
+ - CHANGELOG
71
58
  - Gemfile
72
59
  - LICENSE
73
- - README
60
+ - README.rdoc
74
61
  - Rakefile
75
62
  - gemfiles/Gemfile.common
76
63
  - gemfiles/rspec1/Gemfile
77
- - gemfiles/rspec1/Gemfile.lock
78
64
  - gemfiles/rspec2/Gemfile
79
- - gemfiles/rspec2/Gemfile.lock
80
65
  - lib/with_model.rb
81
66
  - lib/with_model/base.rb
82
67
  - lib/with_model/dsl.rb
83
68
  - lib/with_model/version.rb
84
69
  - spec/active_record_behaviors_spec.rb
70
+ - spec/readme_spec.rb
85
71
  - spec/spec_helper.rb
86
72
  - spec/with_model_spec.rb
87
73
  - with_model.gemspec
@@ -115,11 +101,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
101
  requirements: []
116
102
 
117
103
  rubyforge_project:
118
- rubygems_version: 1.3.7
104
+ rubygems_version: 1.4.2
119
105
  signing_key:
120
106
  specification_version: 3
121
107
  summary: Dynamically build a model within an Rspec context
122
108
  test_files:
123
109
  - spec/active_record_behaviors_spec.rb
110
+ - spec/readme_spec.rb
124
111
  - spec/spec_helper.rb
125
112
  - spec/with_model_spec.rb
data/README DELETED
@@ -1 +0,0 @@
1
- See spec/spec_helper.rb and spec/with_model_spec.rb for usage.
@@ -1,20 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- activerecord (2.3.5)
5
- activesupport (= 2.3.5)
6
- activesupport (2.3.5)
7
- autotest (4.4.2)
8
- mixico (0.1.4)
9
- rspec (1.3.1)
10
- sqlite3-ruby (1.3.2)
11
-
12
- PLATFORMS
13
- ruby
14
-
15
- DEPENDENCIES
16
- activerecord (~> 2.3.5)
17
- autotest
18
- mixico
19
- rspec (~> 1.0)
20
- sqlite3-ruby
@@ -1,39 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- activemodel (3.0.3)
5
- activesupport (= 3.0.3)
6
- builder (~> 2.1.2)
7
- i18n (~> 0.4)
8
- activerecord (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
11
- arel (~> 2.0.2)
12
- tzinfo (~> 0.3.23)
13
- activesupport (3.0.3)
14
- arel (2.0.4)
15
- autotest (4.4.4)
16
- builder (2.1.2)
17
- diff-lcs (1.1.2)
18
- i18n (0.4.2)
19
- mixico (0.1.4)
20
- rspec (2.1.0)
21
- rspec-core (~> 2.1.0)
22
- rspec-expectations (~> 2.1.0)
23
- rspec-mocks (~> 2.1.0)
24
- rspec-core (2.1.0)
25
- rspec-expectations (2.1.0)
26
- diff-lcs (~> 1.1.2)
27
- rspec-mocks (2.1.0)
28
- sqlite3-ruby (1.3.2)
29
- tzinfo (0.3.23)
30
-
31
- PLATFORMS
32
- ruby
33
-
34
- DEPENDENCIES
35
- activerecord (~> 3.0.0)
36
- autotest
37
- mixico
38
- rspec (~> 2.0)
39
- sqlite3-ruby