to_factory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ *.swp
5
+ tmp/database.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --drb
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ree
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ source "http://gems.github.com"
3
+
4
+ group :development, :test do
5
+ gem 'factory_girl_rails'
6
+ gem 'rspec'
7
+ gem 'wirble'
8
+ gem 'spork'
9
+ gem 'activesupport', "~>3.0"
10
+ gem 'sqlite3'
11
+ gem 'ruby-debug'
12
+ gem 'activerecord'
13
+ end
14
+
15
+ # Specify your gem's dependencies in to_factory.gemspec
16
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ to_factory (0.0.1)
5
+ factory_girl_rails
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ remote: http://gems.github.com/
10
+ specs:
11
+ abstract (1.0.0)
12
+ actionpack (3.0.10)
13
+ activemodel (= 3.0.10)
14
+ activesupport (= 3.0.10)
15
+ builder (~> 2.1.2)
16
+ erubis (~> 2.6.6)
17
+ i18n (~> 0.5.0)
18
+ rack (~> 1.2.1)
19
+ rack-mount (~> 0.6.14)
20
+ rack-test (~> 0.5.7)
21
+ tzinfo (~> 0.3.23)
22
+ activemodel (3.0.10)
23
+ activesupport (= 3.0.10)
24
+ builder (~> 2.1.2)
25
+ i18n (~> 0.5.0)
26
+ activerecord (3.0.10)
27
+ activemodel (= 3.0.10)
28
+ activesupport (= 3.0.10)
29
+ arel (~> 2.0.10)
30
+ tzinfo (~> 0.3.23)
31
+ activesupport (3.0.10)
32
+ arel (2.0.10)
33
+ builder (2.1.2)
34
+ columnize (0.3.4)
35
+ diff-lcs (1.1.2)
36
+ erubis (2.6.6)
37
+ abstract (>= 1.0.0)
38
+ factory_girl (2.0.2)
39
+ factory_girl_rails (1.1.0)
40
+ factory_girl (~> 2.0.0)
41
+ railties (>= 3.0.0)
42
+ i18n (0.5.0)
43
+ linecache (0.43)
44
+ rack (1.2.3)
45
+ rack-mount (0.6.14)
46
+ rack (>= 1.0.0)
47
+ rack-test (0.5.7)
48
+ rack (>= 1.0)
49
+ railties (3.0.10)
50
+ actionpack (= 3.0.10)
51
+ activesupport (= 3.0.10)
52
+ rake (>= 0.8.7)
53
+ rdoc (~> 3.4)
54
+ thor (~> 0.14.4)
55
+ rake (0.9.2)
56
+ rdoc (3.9.2)
57
+ rspec (2.6.0)
58
+ rspec-core (~> 2.6.0)
59
+ rspec-expectations (~> 2.6.0)
60
+ rspec-mocks (~> 2.6.0)
61
+ rspec-core (2.6.4)
62
+ rspec-expectations (2.6.0)
63
+ diff-lcs (~> 1.1.2)
64
+ rspec-mocks (2.6.0)
65
+ ruby-debug (0.10.4)
66
+ columnize (>= 0.1)
67
+ ruby-debug-base (~> 0.10.4.0)
68
+ ruby-debug-base (0.10.4)
69
+ linecache (>= 0.3)
70
+ spork (0.8.5)
71
+ sqlite3 (1.3.4)
72
+ thor (0.14.6)
73
+ tzinfo (0.3.29)
74
+ wirble (0.1.3)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ activerecord
81
+ activesupport (~> 3.0)
82
+ factory_girl_rails
83
+ rspec
84
+ ruby-debug
85
+ spork
86
+ sqlite3
87
+ to_factory!
88
+ wirble
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ ToFactory
2
+ =========
3
+ This is a little convenience gem for the scenario where
4
+ you find yourself writing factories for a pre-existing project.
5
+
6
+ E.g. they didn't have tests or they were using Test:Unit with fixtures but you prefer
7
+ RSpec and FactoryGirl.
8
+
9
+ Installation
10
+ ___________
11
+
12
+ I've only tried this out with a Rails 3, Ruby 1.8.7 project.
13
+
14
+ ```ruby
15
+
16
+ #Gemfile
17
+ group =>[:test, :development] do
18
+ gem 'to_factory'
19
+ end
20
+ ```
21
+
22
+
23
+ ```bash
24
+ bundle install
25
+ ```
26
+
27
+ Usage
28
+ _____
29
+
30
+ ```ruby
31
+ #rails console
32
+
33
+ > puts User.first.to_factory
34
+
35
+ #=>
36
+ Factory.define :user do |u|
37
+ u.email "test@example.com"
38
+ u.name "Mike"
39
+ end
40
+
41
+ ```
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'logger'
3
+ require 'active_record'
4
+ require 'yaml'
5
+
6
+ namespace :test do
7
+ namespace :db do
8
+ desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
9
+ task :migrate => :environment do
10
+ ActiveRecord::Migrator.migrate('spec/db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
11
+ end
12
+
13
+ task :environment do
14
+ ActiveRecord::Base.establish_connection(YAML::load(File.read('spec/config/database.yml')))
15
+ ActiveRecord::Base.logger = Logger.new(File.open('tmp/database.log', 'a'))
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,115 @@
1
+ module ToFactory
2
+ class MissingActiveRecordInstanceException < Exception;end
3
+ class MustBeActiveRecordSubClassException < Exception;end
4
+
5
+ def self.included base
6
+ base.class_eval do
7
+ unless self.ancestors.include? ActiveRecord::Base
8
+ raise MustBeActiveRecordSubClassException
9
+ end
10
+
11
+ end
12
+
13
+ base.instance_eval do
14
+ define_method :to_factory do
15
+ Generator.new(self).factory_with_attributes
16
+ end
17
+ end
18
+ end
19
+
20
+ class Generator
21
+ attr_accessor :model_class, :object_instance
22
+
23
+ def initialize object=nil
24
+ return unless object
25
+
26
+ if object.is_a? ActiveRecord::Base
27
+ @model_class = object.class.to_s
28
+ @object_instance = object
29
+ else
30
+ @model_class = object
31
+ end
32
+ end
33
+
34
+ def factory
35
+ out = "Factory :#{name} do |#{abbreviation}|\n"
36
+ out << yield if block_given?
37
+ out << "end"
38
+ end
39
+
40
+ def factory_with_attributes
41
+ factory do
42
+ to_skip = [:id, :created_at, :updated_at]
43
+ attributes = object_instance.attributes.delete_if{|key, value| to_skip.include? key.to_sym}
44
+
45
+ attributes.map do |attr, value|
46
+ factory_attribute(attr, value)
47
+ end.sort.join("\n") << "\n"
48
+ end
49
+ end
50
+
51
+ def factory_for options
52
+ @object_instance = find_from options
53
+ return nil unless @object_instance
54
+ factory_with_attributes
55
+ end
56
+
57
+ private
58
+ def find_from options
59
+ return model_class.find options if options.is_a? Integer
60
+ unless options.is_a? Hash
61
+ raise ArgumentError.new("Expected hash of attributes or integer to look up record")
62
+ end
63
+
64
+ options = options.with_indifferent_access
65
+
66
+ if id=options['id'].to_i
67
+ return model_class.find(id) if id > 0
68
+ end
69
+
70
+ finder, params = finder_and_params_from_options options
71
+
72
+ model_class.send finder, *params
73
+ end
74
+
75
+ public
76
+
77
+ def factory_attribute attr, value=nil
78
+ raise MissingActiveRecordInstanceException unless object_instance
79
+
80
+ value = object_instance.send attr unless value
81
+
82
+ value = "nil" if value.nil?
83
+ value = "\"#{value}\"" if value.is_a? String
84
+ " #{abbreviation}.#{attr} #{value}"
85
+ end
86
+
87
+ def name
88
+ model_class.to_s.underscore
89
+ end
90
+
91
+ private
92
+
93
+ def finder_and_params_from_options options
94
+ params =[]
95
+ finder = "find_by_"
96
+
97
+ options.keys.each do |k|
98
+ if finder == "find_by_"
99
+ finder << k
100
+ else
101
+ finder << "_and_#{k}"
102
+ end
103
+
104
+ value = options[k]
105
+ params << value
106
+ end
107
+
108
+ [finder, params]
109
+ end
110
+
111
+ def abbreviation
112
+ name[0..0]
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,3 @@
1
+ module ToFactory
2
+ VERSION = "0.0.1"
3
+ end
data/lib/to_factory.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "to_factory/version"
2
+ require "to_factory/generator"
@@ -0,0 +1,2 @@
1
+ adapter: sqlite3
2
+ database: spec/db/test.sqlite
@@ -0,0 +1,13 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.column :name, :string, :null => false
5
+ t.column :email, :string
6
+ t.integer :some_id
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :users
12
+ end
13
+ end
Binary file
@@ -0,0 +1,166 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe ToFactory::Generator do
4
+ def options
5
+ YAML::load File.read('spec/config/database.yml')
6
+ end
7
+
8
+ def connect
9
+ AR::Base.establish_connection options rescue nil
10
+ AR::Base.connection
11
+ end
12
+
13
+ before(:all) do
14
+ ActiveRecord.tap do |AR|
15
+ connect
16
+ AR::Base.connection.drop_database options[:database] rescue nil
17
+
18
+ begin
19
+ # Create the SQLite database
20
+ connect
21
+ ActiveRecord::Base.connection
22
+ rescue Exception => e
23
+ $stderr.puts e, *(e.backtrace)
24
+ $stderr.puts "Couldn't create database for #{config.inspect}"
25
+ end
26
+
27
+ AR::Migrator.migrate('db/migrate')
28
+ class User < AR::Base; end
29
+ end
30
+ end
31
+
32
+ before(:each) do
33
+ User.destroy_all
34
+ ActiveRecord::Base.connection.execute "delete from sqlite_sequence where name = 'users'"
35
+ end
36
+
37
+ context "with an active record class but no instance" do
38
+ before do
39
+ @generator = ToFactory::Generator.new User
40
+ User.create :id => 1, :name => 'Tom', :email => 'blah@example.com', :some_id => 7
41
+ User.create :id => 2, :name => 'James', :email => 'james@example.com', :some_id => 8
42
+ end
43
+
44
+ it "initializes" do
45
+ @generator.model_class.should == User
46
+ end
47
+
48
+ let(:user_factory_1) do
49
+ user_factory_1 = <<-eof
50
+ Factory :user do |u|
51
+ u.email "blah@example.com"
52
+ u.name "Tom"
53
+ u.some_id 7
54
+ end
55
+ eof
56
+ user_factory_1.chop
57
+ end
58
+ let(:user_factory_2) do
59
+ user_factory_2 = <<-eof
60
+ Factory :user do |u|
61
+ u.email "james@example.com"
62
+ u.name "James"
63
+ u.some_id 8
64
+ end
65
+ eof
66
+ user_factory_2.chop
67
+ end
68
+ context "looking up attributes" do
69
+ it "takes a key value for id" do
70
+ out = @generator.factory_for :id => 1
71
+ out.should == user_factory_1
72
+ end
73
+
74
+ it "takes an integer for id" do
75
+ out = @generator.factory_for 1
76
+ out.should == user_factory_1
77
+ end
78
+
79
+ it "takes a key value and does a lookup" do
80
+ out = @generator.factory_for :name => 'Tom'
81
+ out.should == user_factory_1
82
+
83
+ out = @generator.factory_for :name => 'James'
84
+ out.should == user_factory_2
85
+ end
86
+
87
+ it "takes multiple keys and does a lookup" do
88
+ out = @generator.factory_for :id => 1, :name => 'Tom'
89
+ end
90
+
91
+ context "with invalid attributes" do
92
+ it "raises an exception" do
93
+ lambda{@generator.factory_for :id => 1000}.should raise_error ActiveRecord::RecordNotFound
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ it "initializes with an activerecord instance" do
100
+ user = User.create :name => "Jeff"
101
+ @generator = ToFactory::Generator.new user
102
+ @generator.model_class.should == "User"
103
+ end
104
+
105
+ it "initializes without an object" do
106
+ @generator = ToFactory::Generator.new
107
+ @generator.model_class.should be_nil
108
+ end
109
+
110
+ it "generates the first line of the factory" do
111
+ @generator = ToFactory::Generator.new User
112
+ f = @generator.factory
113
+ output = <<-eof
114
+ Factory :user do |u|
115
+ end
116
+ eof
117
+ f.should == output.chop
118
+ end
119
+
120
+
121
+ it "raises an exception without an AR object, when requesting attributes" do
122
+ @generator = ToFactory::Generator.new User
123
+ lambda{@generator.factory_attribute :foo}.should raise_error ToFactory::MissingActiveRecordInstanceException
124
+ end
125
+
126
+ context "with a user in the database" do
127
+ before do
128
+ User.create :name => "Jeff", :email => "test@example.com", :some_id => 8
129
+ @generator = ToFactory::Generator.new user
130
+ end
131
+
132
+ let(:user){ User.first}
133
+
134
+ it "generates lines for multiple the attributes" do
135
+ @generator.factory_attribute(:name). should == ' u.name "Jeff"'
136
+ @generator.factory_attribute(:email).should == ' u.email "test@example.com"'
137
+ end
138
+
139
+ let(:expected) do
140
+ expected = <<-eof
141
+ Factory :user do |u|
142
+ u.email "test@example.com"
143
+ u.name "Jeff"
144
+ u.some_id 8
145
+ end
146
+ eof
147
+ expected.chop
148
+ end
149
+
150
+ it "generates the full factory" do
151
+ @generator.factory_with_attributes.should == expected
152
+ end
153
+
154
+ it "adds the to_factory method to an active record object" do
155
+ ActiveRecord::Base.send :include, ToFactory
156
+ user.to_factory.should == expected
157
+ end
158
+
159
+ it "raises an error if you try to inlude in a non ActiveRecord object" do
160
+ class Egg;end
161
+
162
+ lambda{Egg.send :include, ToFactory}.should raise_error ToFactory::MustBeActiveRecordSubClassException
163
+ end
164
+ end
165
+
166
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ require 'spork/ext/ruby-debug'
4
+
5
+ Spork.prefork do
6
+ require 'active_record'
7
+ require 'rspec'
8
+ require 'yaml'
9
+ require 'fileutils'
10
+ require 'ruby-debug'
11
+ require 'active_support/core_ext/string'
12
+ require 'active_support/core_ext/hash'
13
+ end
14
+
15
+ Spork.each_run do
16
+ Dir['lib/**/*.rb'].each{|f| require f unless f=="lib/to_factory/version.rb"}
17
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "to_factory/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "to_factory"
7
+ s.version = ToFactory::VERSION
8
+ s.authors = ["Mark Burns"]
9
+ s.email = ["markthedeveloper@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Turn ActiveRecord instances into factories}
12
+ s.add_dependency('factory_girl_rails')
13
+
14
+ s.description = %q{This gem gives the object.to_factory method to give a formatted string version of an ActiveRecord object that can be used by factory_girl}
15
+
16
+ s.rubyforge_project = "to_factory"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_factory
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mark Burns
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-21 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: factory_girl_rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: This gem gives the object.to_factory method to give a formatted string version of an ActiveRecord object that can be used by factory_girl
36
+ email:
37
+ - markthedeveloper@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - .rspec
47
+ - .rvmrc
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - README.md
51
+ - Rakefile
52
+ - lib/to_factory.rb
53
+ - lib/to_factory/generator.rb
54
+ - lib/to_factory/version.rb
55
+ - spec/config/database.yml
56
+ - spec/db/migrate/201108201012010100_create_users.rb
57
+ - spec/db/test.sqlite
58
+ - spec/generator_spec.rb
59
+ - spec/spec_helper.rb
60
+ - to_factory.gemspec
61
+ has_rdoc: true
62
+ homepage: ""
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project: to_factory
91
+ rubygems_version: 1.6.2
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Turn ActiveRecord instances into factories
95
+ test_files:
96
+ - spec/config/database.yml
97
+ - spec/db/migrate/201108201012010100_create_users.rb
98
+ - spec/db/test.sqlite
99
+ - spec/generator_spec.rb
100
+ - spec/spec_helper.rb