daddys_girl 0.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle/*
3
+ spec/test.db
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm use --create 1.9.2@daddys_girl
2
+ echo "Loading bundle..."
3
+ bundle install --without=production
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in daddys_girl.gemspec
4
+ gemspec
5
+
6
+ spec = Bundler.load_gemspec(Dir["./{,*}.gemspec"].first)
7
+ spec.runtime_dependencies.each do |dep|
8
+ gem dep.name, *(dep.requirement.as_list)
9
+ end
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ daddys_girl (0.6.0)
5
+ activerecord (~> 3.0.0)
6
+ factory_girl (~> 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (3.0.12)
12
+ activesupport (= 3.0.12)
13
+ builder (~> 2.1.2)
14
+ i18n (~> 0.5.0)
15
+ activerecord (3.0.12)
16
+ activemodel (= 3.0.12)
17
+ activesupport (= 3.0.12)
18
+ arel (~> 2.0.10)
19
+ tzinfo (~> 0.3.23)
20
+ activesupport (3.0.12)
21
+ arel (2.0.10)
22
+ builder (2.1.2)
23
+ diff-lcs (1.1.3)
24
+ factory_girl (2.6.4)
25
+ activesupport (>= 2.3.9)
26
+ i18n (0.5.0)
27
+ rspec (2.9.0)
28
+ rspec-core (~> 2.9.0)
29
+ rspec-expectations (~> 2.9.0)
30
+ rspec-mocks (~> 2.9.0)
31
+ rspec-core (2.9.0)
32
+ rspec-expectations (2.9.0)
33
+ diff-lcs (~> 1.1.3)
34
+ rspec-mocks (2.9.0)
35
+ sqlite3 (1.3.5)
36
+ sqlite3-ruby (1.3.3)
37
+ sqlite3 (>= 1.3.3)
38
+ tzinfo (0.3.32)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ activerecord (~> 3.0.0)
45
+ daddys_girl!
46
+ factory_girl (~> 2.0)
47
+ rspec (~> 2.0)
48
+ sqlite3-ruby
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Inventables
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Daddy's Girl
2
+
3
+ Daddy's Girl is a Ruby gem to provide object_daddy-like syntax for factory_girl
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'daddys_girl'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install daddys_girl
18
+
19
+ ## Usage
20
+ First, you must add a class definition to the Factory Girl factories file (normally spec/factories.rb)
21
+
22
+ Methods:
23
+ 1. ```ClassName.spawn(params)```: creates an object, but does not save it
24
+ 2. ```ClassName.generate(params)```: creates an object and attempts to save it
25
+ 3. ```ClassName.generate!(params)```: creates an object, and throws an error if it can not save it
26
+
27
+ Daddy's Girl also works with associations. For example:
28
+ ```object.has_many_association.generate!(params)``` will generate the associated object, and bind it to the calling object.
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/daddys_girl/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Kurt Preston"]
6
+ gem.email = ["development@inventables.com"]
7
+ gem.description = "Rubygem to provide object_daddy-like syntax for factory_girl"
8
+ gem.summary = "[Object] Daddy's [Factory] Girl"
9
+ gem.homepage = "https://github.com/inventables/daddys_girl"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "daddys_girl"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = DaddysGirl::VERSION
17
+ gem.add_dependency "activerecord", "~> 3.0.0"
18
+ gem.add_dependency "factory_girl", "~> 2.0"
19
+ gem.add_development_dependency "rspec", "~> 2.0"
20
+ gem.add_development_dependency "sqlite3-ruby"
21
+ end
22
+
@@ -1,62 +1,61 @@
1
- module DaddysGirl
2
- module ActiveRecordModel
3
- class << self
4
- def symbol
5
- self.name.underscore.to_sym
6
- end
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.class_eval do
4
+ class << self
5
+ def symbol
6
+ self.name.underscore.to_sym
7
+ end
7
8
 
8
- def generate(attributes = {})
9
+ def generate(attributes = {})
10
+ begin
9
11
  FactoryGirl.create(self.symbol, attributes)
12
+ rescue ActiveRecord::RecordInvalid
13
+ FactoryGirl.build(self.symbol, attributes)
10
14
  end
15
+ end
11
16
 
12
- def generate!(attributes = {})
13
- FactoryGirl.create(self.symbol, attributes).tap do |obj|
14
- raise obj.errors.inspect unless obj.errors.empty?
15
- end
16
- end
17
+ def generate!(attributes = {})
18
+ FactoryGirl.create(self.symbol, attributes)
19
+ end
17
20
 
18
- def spawn(attributes = {})
19
- FactoryGirl.build(self.symbol, attributes)
20
- end
21
+ def spawn(attributes = {})
22
+ FactoryGirl.build(self.symbol, attributes)
21
23
  end
22
24
  end
23
25
  end
24
26
 
25
- ActiveRecord::Base.send(:include, DaddysGirl::ActiveRecordModel) if defined?(ActiveRecord)
26
27
 
28
+ ActiveRecord::Associations::AssociationProxy.class_eval do
29
+ def target_class_symbol
30
+ self.symbol
31
+ end
27
32
 
28
- module DaddysGirl
29
- module AssociationModel
30
- def target_class_symbol
31
- self.symbol
32
- end
33
-
34
- def generate(attributes = {})
35
- attributes = attributes.merge(association_attribute)
33
+ def generate(attributes = {})
34
+ attributes = attributes.merge(association_attribute)
35
+ begin
36
36
  FactoryGirl.create(target_class_symbol, attributes)
37
+ rescue ActiveRecord::RecordInvalid
38
+ FactoryGirl.build(target_class_symbol, attributes)
37
39
  end
40
+ end
38
41
 
39
- def generate!(attributes = {})
40
- attributes = attributes.merge(association_attribute)
41
- FactoryGirl.create(target_class_symbol, attributes).tap do |obj|
42
- raise obj.errors.inspect unless obj.errors.empty?
43
- end
44
- end
42
+ def generate!(attributes = {})
43
+ attributes = attributes.merge(association_attribute)
44
+ FactoryGirl.create(target_class_symbol, attributes)
45
+ end
45
46
 
46
- def spawn(attributes = {})
47
- attributes = attributes.merge(association_attribute)
48
- FactoryGirl.build(target_class_symbol, attributes)
49
- end
47
+ def spawn(attributes = {})
48
+ attributes = attributes.merge(association_attribute)
49
+ FactoryGirl.build(target_class_symbol, attributes)
50
+ end
50
51
 
51
- private
52
- def owner_association
53
- proxy_reflection.primary_key_name.to_sym
54
- end
52
+ private
53
+ def owner_association
54
+ proxy_reflection.primary_key_name.to_sym
55
+ end
55
56
 
56
- def association_attribute
57
- {owner_association => proxy_owner.id}
58
- end
57
+ def association_attribute
58
+ {owner_association => proxy_owner.id}
59
59
  end
60
60
  end
61
61
 
62
- ActiveRecord::Associations::AssociationProxy.send(:include, DaddysGirl::AssocationModel) if defined?(ActiveRecord)
@@ -0,0 +1,3 @@
1
+ module DaddysGirl
2
+ VERSION = "0.6.0"
3
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaddysGirl do
4
+ before(:all) do
5
+ FactoryGirl.define do
6
+ factory :test_class do
7
+ name 'Test Name'
8
+ end
9
+
10
+ factory :test_association do
11
+ name 'Association'
12
+ end
13
+ end
14
+ end
15
+
16
+ before(:each) do
17
+ define_model('TestClass', {:name => :string})
18
+ define_model('TestAssociation', {:name => :string, :test_class_id => :integer})
19
+
20
+ TestClass.class_eval do
21
+ validates_format_of :name, :with => /^[^!]+$/
22
+ has_many :test_associations
23
+ end
24
+ TestAssociation.class_eval do
25
+ validates_format_of :name, :with => /^[^!]+$/
26
+ belongs_to :test_class
27
+ end
28
+
29
+ @test_object = TestClass.create(:name => "Valid")
30
+ end
31
+
32
+ describe "ActiveRecord::Base#spawn" do
33
+ it "creates a new object without saving" do
34
+ TestClass.spawn.class.should == TestClass
35
+ TestClass.spawn(:name => "Test Name").name.should == "Test Name"
36
+ TestClass.spawn.id.should be_nil
37
+ end
38
+ end
39
+
40
+ describe "ActiveRecord::Base#generate" do
41
+ context "if the object is valid" do
42
+ it "creates a new object and saves" do
43
+ TestClass.generate(:name => "Test Name").name.should == "Test Name"
44
+ TestClass.generate.id.should_not be_nil
45
+ end
46
+ end
47
+
48
+ context "if the object is not valid" do
49
+ it "creates a new object without saving" do
50
+ TestClass.generate(:name => 'Invalid!').id.should be_nil
51
+ end
52
+ end
53
+ end
54
+
55
+ describe "ActiveRecord::Base#generate!" do
56
+ context "if the object is valid" do
57
+ it "creates a new object and saves" do
58
+ TestClass.generate!(:name => "Test Name").name.should == "Test Name"
59
+ TestClass.generate!.id.should_not be_nil
60
+ end
61
+ end
62
+
63
+ context "if the object is not valid" do
64
+ it "throws an error" do
65
+ begin
66
+ TestClass.generate!(:name => "Invalid!")
67
+ false
68
+ rescue ActiveRecord::RecordInvalid
69
+ true
70
+ end.should be_true
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "ActiveRecord::Associations::AssociationProxy.spawn" do
76
+ it "creates a new object without saving" do
77
+ @test_object.test_associations.spawn.class.should == TestAssociation
78
+ @test_object.test_associations.spawn(:name => 'Test Association').name.should == 'Test Association'
79
+ @test_object.test_associations.spawn.id.should be_nil
80
+ @test_object.test_associations.spawn.test_class.should == @test_object.reload
81
+ end
82
+ end
83
+
84
+ describe "ActiveRecord::Associations::AssociationProxy.generate" do
85
+ context "if the object is valid" do
86
+ it "creates a new object and saves" do
87
+ @test_object.test_associations.generate.id.should_not be_nil
88
+ @test_object.test_associations.generate.test_class.id == @test_object.id
89
+ end
90
+ end
91
+
92
+ context "if the object is not valid" do
93
+ it "creates a new object without saving" do
94
+ @test_object.test_associations.generate(:name => "Invalid!").id.should be_nil
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "ActiveRecord::Associations::AssociationProxy.generate!" do
100
+ context "if the object is valid" do
101
+ it "creates a new object and saves" do
102
+ @test_object.test_associations.generate!.id.should_not be_nil
103
+ end
104
+ end
105
+
106
+ context "if the object is not valid" do
107
+ it "throws an error" do
108
+ begin
109
+ @test_object.test_associations.generate!(:name => "Invalid!")
110
+ false
111
+ rescue ActiveRecord::RecordInvalid
112
+ true
113
+ end.should be_true
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,89 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ Bundler.require(:default)
4
+ require 'active_record'
5
+
6
+ module DefineConstantMacros
7
+ def define_class(path, base = Object, &block)
8
+ namespace, class_name = *constant_path(path)
9
+ klass = Class.new(base)
10
+ namespace.const_set(class_name, klass)
11
+ klass.class_eval(&block) if block_given?
12
+ @defined_constants << path
13
+ klass
14
+ end
15
+
16
+ def define_model(name, columns = {}, &block)
17
+ model = define_class(name, ActiveRecord::Base, &block)
18
+ create_table(model.table_name) do |table|
19
+ columns.each do |name, type|
20
+ table.column name, type
21
+ end
22
+ end
23
+ model
24
+ end
25
+
26
+ def create_table(table_name, &block)
27
+ connection = ActiveRecord::Base.connection
28
+
29
+ begin
30
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
31
+ connection.create_table(table_name, &block)
32
+ @created_tables << table_name
33
+ connection
34
+ rescue Exception => exception
35
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
36
+ raise exception
37
+ end
38
+ end
39
+
40
+ def constant_path(constant_name)
41
+ names = constant_name.split('::')
42
+ class_name = names.pop
43
+ namespace = names.inject(Object) { |result, name| result.const_get(name) }
44
+ [namespace, class_name]
45
+ end
46
+
47
+ def default_constants
48
+ @defined_constants ||= []
49
+ @created_tables ||= []
50
+ end
51
+
52
+ def clear_generated_constants
53
+ @defined_constants.reverse.each do |path|
54
+ namespace, class_name = *constant_path(path)
55
+ namespace.send(:remove_const, class_name)
56
+ end
57
+
58
+ @defined_constants.clear
59
+ end
60
+
61
+ def clear_generated_tables
62
+ @created_tables.each do |table_name|
63
+ ActiveRecord::Base.
64
+ connection.
65
+ execute("DROP TABLE IF EXISTS #{table_name}")
66
+ end
67
+ @created_tables.clear
68
+ end
69
+ end
70
+
71
+ RSpec.configure do |config|
72
+ config.include DefineConstantMacros
73
+
74
+ config.before(:all) do
75
+ ActiveRecord::Base.establish_connection(
76
+ adapter: 'sqlite3',
77
+ database: File.join(File.dirname(__FILE__), 'test.db')
78
+ )
79
+ end
80
+
81
+ config.before do
82
+ default_constants
83
+ end
84
+
85
+ config.after do
86
+ clear_generated_constants
87
+ clear_generated_tables
88
+ end
89
+ end
metadata CHANGED
@@ -1,99 +1,103 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: daddys_girl
3
- version: !ruby/object:Gem::Version
4
- hash: 3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 4
9
- version: "0.4"
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Kurt Preston
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2020-03-13 00:00:00 Z
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
12
+ date: 2012-04-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
20
15
  name: activerecord
21
- prerelease: false
22
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &9152520 !ruby/object:Gem::Requirement
23
17
  none: false
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- hash: 7
28
- segments:
29
- - 3
30
- - 0
31
- version: "3.0"
32
- - - <=
33
- - !ruby/object:Gem::Version
34
- hash: 3
35
- segments:
36
- - 3
37
- - 2
38
- version: "3.2"
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
39
22
  type: :runtime
40
- version_requirements: *id001
41
- - !ruby/object:Gem::Dependency
42
- name: factory_girl
43
23
  prerelease: false
44
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *9152520
25
+ - !ruby/object:Gem::Dependency
26
+ name: factory_girl
27
+ requirement: &9152020 !ruby/object:Gem::Requirement
45
28
  none: false
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- hash: 3
50
- segments:
51
- - 0
52
- version: "0"
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
53
33
  type: :runtime
54
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *9152020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &9151540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *9151540
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3-ruby
49
+ requirement: &9151120 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *9151120
55
58
  description: Rubygem to provide object_daddy-like syntax for factory_girl
56
- email: development@inventables.com
59
+ email:
60
+ - development@inventables.com
57
61
  executables: []
58
-
59
62
  extensions: []
60
-
61
63
  extra_rdoc_files: []
62
-
63
- files:
64
+ files:
65
+ - .gitignore
66
+ - .rvmrc
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - daddys_girl.gemspec
64
73
  - lib/daddys_girl.rb
74
+ - lib/daddys_girl/version.rb
75
+ - spec/daddys_girl_spec.rb
76
+ - spec/spec_helper.rb
65
77
  homepage: https://github.com/inventables/daddys_girl
66
78
  licenses: []
67
-
68
79
  post_install_message:
69
80
  rdoc_options: []
70
-
71
- require_paths:
81
+ require_paths:
72
82
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
83
+ required_ruby_version: !ruby/object:Gem::Requirement
74
84
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- hash: 3
79
- segments:
80
- - 0
81
- version: "0"
82
- required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
90
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
91
95
  requirements: []
92
-
93
96
  rubyforge_project:
94
- rubygems_version: 1.7.2
97
+ rubygems_version: 1.8.10
95
98
  signing_key:
96
99
  specification_version: 3
97
- summary: "[Object] Daddy's [Factory] Girl"
98
- test_files: []
99
-
100
+ summary: ! '[Object] Daddy''s [Factory] Girl'
101
+ test_files:
102
+ - spec/daddys_girl_spec.rb
103
+ - spec/spec_helper.rb