Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 8 commits
  • 9 files changed
  • 0 commit comments
  • 6 contributors
Commits on Jun 12, 2010
@knapo knapo rails3 compatibility 2bbcc70
Commits on Feb 10, 2011
Tim Olsen support primary keys not named "id" 16a88f4
Commits on Aug 01, 2011
@whatcould whatcould Rails 3.1 compat: use Rails.abc instead of RAILS_ENV, etc ed7f005
Commits on Nov 25, 2011
@kubalasecki kubalasecki Update ar_fixtures.gemspec 24ebaa8
Commits on Jan 17, 2012
@dmitryz dmitryz Update ar_fixtures.gemspec 94879ed
@dmitryz dmitryz Add to gemspec lib/tasks/ar_fixtures.rb 18b102f
@dmitryz dmitryz Update ar_fixtures.gemspec d656870
Commits on May 04, 2012
@avit Merge remote-tracking branch 'kubalasecki/master'
* kubalasecki/master:
  Update ar_fixtures.gemspec

Conflicts:
	ar_fixtures.gemspec
8791e48
View
6 ar_fixtures.gemspec
@@ -1,4 +1,3 @@
-
Gem::Specification.new do |s|
s.name = %q{ar_fixtures}
s.version = "0.0.4"
@@ -10,11 +9,8 @@ Gem::Specification.new do |s|
s.date = %q{2008-06-11}
s.description = %q{Creates test fixtures from data in the database.}
s.email = %q{boss AT topfunky.com}
- s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
- s.files = ["CHANGELOG", "History.txt", "MIT-LICENSE", "Manifest.txt", "README.txt", "Rakefile", "about.yml", "init.rb", "lib/ar_fixtures.rb", "tasks/ar_fixtures.rake", "test/ar_fixtures_test.rb", "test/database.yml", "test/fixtures/beer.rb", "test/fixtures/beers.yml", "test/fixtures/beers_drunkards.yml", "test/fixtures/drunkard.rb", "test/fixtures/drunkards.yml", "test/fixtures/glass.rb", "test/fixtures/glasses.yml", "test/schema.rb", "test/test_helper.rb"]
- s.has_rdoc = true
+ s.files = ["CHANGELOG", "History.txt", "MIT-LICENSE", "Manifest.txt", "README.txt", "Rakefile", "about.yml", "init.rb", "lib/ar_fixtures.rb", "lib/tasks/ar_fixtures.rake", "test/ar_fixtures_test.rb", "test/database.yml", "test/fixtures/beer.rb", "test/fixtures/beers.yml", "test/fixtures/beers_drunkards.yml", "test/fixtures/drunkard.rb", "test/fixtures/drunkards.yml", "test/fixtures/glass.rb", "test/fixtures/glasses.yml", "test/schema.rb", "test/test_helper.rb"]
s.homepage = %q{http://rubyforge.org/projects/seattlerb}
- s.rdoc_options = ["--main", "README.txt"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{seattlerb}
s.rubygems_version = %q{1.1.1}
View
31 lib/ar_fixtures.rb
@@ -11,7 +11,7 @@ class << self
def dump_to_file(path=nil, limit=nil, opts={})
opts[:limit] = limit if limit
path ||= "db/#{table_name}.yml"
- write_file(File.expand_path(path, RAILS_ROOT), self.find(:all, opts).to_yaml)
+ write_file(File.expand_path(path, Rails.root), self.find(:all, opts).to_yaml)
habtm_to_file
end
@@ -26,7 +26,7 @@ def habtm_to_file
connection.select_all("SELECT * FROM #{join.options[:join_table]}").each_with_index { |record, i|
hsh["join_#{'%05i' % i}"] = record
}
- write_file(File.expand_path("db/#{join.options[:join_table]}.yml", RAILS_ROOT), hsh.to_yaml(:SortKeys => true))
+ write_file(File.expand_path("db/#{join.options[:join_table]}.yml", Rails.root), hsh.to_yaml(:SortKeys => true))
end
end
@@ -43,12 +43,12 @@ def load_from_file(path=nil)
connection.reset_pk_sequence!(table_name)
end
- rawdata = File.read(File.expand_path(path, RAILS_ROOT))
+ rawdata = File.read(File.expand_path(path, Rails.root))
erb_data = ERB.new(rawdata).result
records = YAML::load( erb_data )
records.each do |record|
- unless 'test' == RAILS_ENV
+ unless 'test' == Rails.env
puts "______________"
puts record.to_yaml
puts "______________"
@@ -75,13 +75,20 @@ def load_from_file(path=nil)
# Uses existing data in the database.
#
# Will be written to +test/fixtures/table_name.yml+. Can be restricted to some number of rows.
- def to_fixture(limit=nil, key=nil, opts={})
- opts[:limit] = limit if limit
+ def to_fixture(limit=nil, key=nil)
+ str = "--- \n"
+ self.limit(limit).order(self.primary_key).each do |record|
+ name = case
+ when record.respond_to?('fixture_name') then record.fixture_name
+ when record.respond_to?('name') then record.name.underscore.gsub(/\W/,'_')
+ else table_name.singularize
+ end
+ name << "_#{record.id}"
+ name.gsub!(/(_)+/,'_')
+ str << {name => record.attributes}.to_yaml(:SortKeys => true).gsub("--- \n","")
+ end
- write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
- self.find(:all, opts).inject({}) { |hsh, record|
- hsh.merge((record.attributes[key.to_s] || "#{table_name.singularize}_#{'%05i' % record.id rescue record.id}") => record.attributes)
- }.to_yaml(:SortKeys => true))
+ write_file(Rails.root.join("test/fixtures/#{table_name}.yml").to_s, str)
habtm_to_fixture
end
@@ -95,7 +102,7 @@ def habtm_to_fixture
connection.select_all("SELECT * FROM #{join.options[:join_table]}").each_with_index { |record, i|
hsh["join_#{'%05i' % i}"] = record
}
- write_file(File.expand_path("test/fixtures/#{join.options[:join_table]}.yml", RAILS_ROOT), hsh.to_yaml(:SortKeys => true))
+ write_file(File.expand_path("test/fixtures/#{join.options[:join_table]}.yml", Rails.root), hsh.to_yaml(:SortKeys => true))
end
end
@@ -116,7 +123,7 @@ def to_skeleton
"record_1" => self.new.attributes,
"record_2" => self.new.attributes
}
- write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
+ write_file(File.expand_path("test/fixtures/#{table_name}.yml", Rails.root),
record.to_yaml)
end
View
0  tasks/ar_fixtures.rake → lib/tasks/ar_fixtures.rake
File renamed without changes
View
2  nbproject/private/private.properties
@@ -0,0 +1,2 @@
+file.reference.ar_fixtures-lib=/home/knapo/workspace/ar_fixtures/lib
+file.reference.ar_fixtures-test=/home/knapo/workspace/ar_fixtures/test
View
0  nbproject/private/rake-d.txt
No changes.
View
7 nbproject/project.properties
@@ -0,0 +1,7 @@
+file.reference.ar_fixtures-lib=lib
+file.reference.ar_fixtures-test=test
+main.file=
+platform.active=Ruby
+source.encoding=UTF-8
+src.dir=${file.reference.ar_fixtures-lib}
+test.src.dir=${file.reference.ar_fixtures-test}
View
15 nbproject/project.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://www.netbeans.org/ns/project/1">
+ <type>org.netbeans.modules.ruby.rubyproject</type>
+ <configuration>
+ <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
+ <name>ar_fixtures</name>
+ <source-roots>
+ <root id="src.dir"/>
+ </source-roots>
+ <test-roots>
+ <root id="test.src.dir"/>
+ </test-roots>
+ </data>
+ </configuration>
+</project>
View
28 test/ar_fixtures_test.rb
@@ -13,7 +13,7 @@ class ArFixturesTest < Test::Unit::TestCase
include FileUtils
def setup
- %w(db test/fixtures).each { |dir| mkdir_p File.join(RAILS_ROOT, dir) }
+ %w(db test/fixtures).each { |dir| mkdir_p File.join(Rails.root, dir) }
end
def test_dump_to_file
@@ -21,7 +21,7 @@ def test_dump_to_file
assert_equal 2, Beer.count
Beer.dump_to_file
- assert File.exist?(File.join(RAILS_ROOT, 'db', 'beers.yml'))
+ assert File.exist?(File.join(Rails.root, 'db', 'beers.yml'))
Beer.destroy_all
assert_equal 0, Beer.count
@@ -31,14 +31,14 @@ def test_dump_to_file
def test_habtm_to_file
Beer.habtm_to_file
- assert File.exist?(File.join(RAILS_ROOT, 'db', 'beers_drunkards.yml'))
+ assert File.exist?(File.join(Rails.root, 'db', 'beers_drunkards.yml'))
end
def test_load_from_file
- cp File.join(RAILS_ROOT, 'fixtures', 'beers.yml'),
- File.join(RAILS_ROOT, 'db', 'beers.yml')
- cp File.join(RAILS_ROOT, 'fixtures', 'beers_drunkards.yml'),
- File.join(RAILS_ROOT, 'db', 'beers_drunkards.yml')
+ cp File.join(Rails.root, 'fixtures', 'beers.yml'),
+ File.join(Rails.root, 'db', 'beers.yml')
+ cp File.join(Rails.root, 'fixtures', 'beers_drunkards.yml'),
+ File.join(Rails.root, 'db', 'beers_drunkards.yml')
Beer.load_from_file
assert_equal 2, Beer.count
# assert_equal 1, Beer.find(1).drunkards.size
@@ -46,32 +46,32 @@ def test_load_from_file
def test_to_fixture
Beer.to_fixture
- assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers.yml'))
- assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers_drunkards.yml'))
+ assert File.exist?(File.join(Rails.root, 'test', 'fixtures', 'beers.yml'))
+ assert File.exist?(File.join(Rails.root, 'test', 'fixtures', 'beers_drunkards.yml'))
end
def test_habtm_to_fixture
Beer.habtm_to_fixture
- assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers_drunkards.yml'))
+ assert File.exist?(File.join(Rails.root, 'test', 'fixtures', 'beers_drunkards.yml'))
end
def test_to_skeleton
Beer.to_skeleton
- assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers.yml'))
+ assert File.exist?(File.join(Rails.root, 'test', 'fixtures', 'beers.yml'))
end
def test_should_use_erb
Beer.delete_all
- cp File.join(RAILS_ROOT, 'fixtures', 'beers.yml'),
- File.join(RAILS_ROOT, 'db', 'beers.yml')
+ cp File.join(Rails.root, 'fixtures', 'beers.yml'),
+ File.join(Rails.root, 'db', 'beers.yml')
assert_equal 0, Beer.count
Beer.load_from_file
assert_equal 2, Beer.count
end
def teardown
- %w(db test).each { |dir| rm_rf File.join(RAILS_ROOT, dir) }
+ %w(db test).each { |dir| rm_rf File.join(Rails.root, dir) }
end
end
View
4 test/test_helper.rb
@@ -1,5 +1,5 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
-RAILS_ROOT = File.dirname(__FILE__)
+Rails.root = File.dirname(__FILE__)
require 'rubygems'
require 'test/unit'
@@ -16,7 +16,7 @@
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
-RAILS_ENV = 'test' unless defined?(RAILS_ENV)
+Rails.env = 'test' unless defined?(Rails.env)
class Test::Unit::TestCase #:nodoc:
def create_fixtures(*table_names)

No commit comments for this range

Something went wrong with that request. Please try again.