Skip to content

Commit

Permalink
Merge branch '3.0'
Browse files Browse the repository at this point in the history
Conflicts:
	spec/spec_helpers.rb
  • Loading branch information
smtlaissezfaire committed Nov 21, 2009
2 parents 3dbb7da + dc1f42d commit 1f55e5f
Show file tree
Hide file tree
Showing 57 changed files with 1,038 additions and 620 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
doc
doc
pkg
19 changes: 15 additions & 4 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
== 3.0.1

- Made it into a rubygem. Get it from gemcutter:

gem install fixture_replacement

You'll need to require "fixture_replacement" (or load "fixture_replacement")
in your spec_helper, test_helper, or anywhere else you plan to use it.

- validate instances with FixtureReplacement.validate!

== 3.0.0

- Better Support for Reloading (call FR.reload! after calling reload! in the console or in your spec helper)
- default_* is gone. Use new_* instead.
This will necessitate that declarations of the form validates_presence_of :association_id
This will necessitate that declarations of the form validates_presence_of :association_id
change to validates_presence_of :association
- attributes_for now yields the real active record object, not an OpenStruct, providing for increased
flexibility
Expand All @@ -11,7 +22,7 @@
attributes_for :event do |event, hash|
e.schedule = new_schedule(:event => event) unless hash[:schedule]
end

attributes_for :schedule do |schedule, hash|
s.event = new_event(:schedule => schedule) unless hash[:event]
end
Expand All @@ -38,14 +49,14 @@

- Add FR as an alias for FixtureReplacement. This should make it considerably easier to
use FR in the console:

smt$ ./script/console
Loading development environment (Rails 2.2.2)
>> include FR
=> Object
>> create_user
=> #<User id: 14 ... >

- Use better loading. Allow FixtureReplacement to be reloaded if cache_classes = false
- Removed generator, since db/example_data.rb is no longer required.
- Removed required db/example_data.rb. fixture_replacement can now be used outside of a rails
Expand Down
70 changes: 45 additions & 25 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
= FixtureReplacement (version 3.0)
= FixtureReplacement (version 3.0.1)

== What is FixtureReplacement

FixtureReplacement is a Rails[http://rubyonrails.org/] plugin that provides a simple way to
quickly populate your test database with model objects without having to manage multiple,
brittle fixture files. You can easily set up complex object graphs (with models which
FixtureReplacement is a Rails[http://rubyonrails.org/] plugin that provides a simple way to
quickly populate your test database with model objects without having to manage multiple,
brittle fixture files. You can easily set up complex object graphs (with models which
reference other models) and add new objects on the fly.

Not only can FixtureReplacement make your test data easier to maintain, it can also help
to make your tests and specs much more readable and intention-revealing by allowing you
to omit extraneous details and focus only on the attributes that are important for a
particular behaviour. It works well with both RSpec[http://rspec.rubyforge.org/] and
Not only can FixtureReplacement make your test data easier to maintain, it can also help
to make your tests and specs much more readable and intention-revealing by allowing you
to omit extraneous details and focus only on the attributes that are important for a
particular behaviour. It works well with both RSpec[http://rspec.rubyforge.org/] and
Test::Unit[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html].

== What's new since 2.0:

* default_* is gone in favor of new_*.
* Cyclic dependencies are no longer an issue. The "overrides hash" (the hash passed to new_* or create_*) can now be processed.
* Cyclic dependencies are no longer an issue. The "overrides hash" (the hash passed to new_* or create_*) can now be processed.

See CHANGELOG.rdoc + test suite for further changes.

Expand Down Expand Up @@ -46,15 +46,15 @@ Add the following to your <tt>test/test_helper.rb</tt> file:

=== Defining default attributes

At the heart of FixtureReplacement is the <tt>db/example_data.rb</tt> file where you
At the heart of FixtureReplacement is the <tt>db/example_data.rb</tt> file where you
define the default attributes for each of your test models. This example shows the default
attributes for a user:

module FixtureReplacement

attributes_for :user do |u|
password = random_string

u.value = "a value",
u.other = "other value",
u.another = random_string, # random string 10 characters long
Expand All @@ -63,41 +63,41 @@ attributes for a user:
u.password_confirmation = password,
u.associated_object = new_bar # expects attributes_for :bar to be setup
end

end

Note that:

- A 'random_string' method is provided for attributes whose exact value isn't important; this means you can
- A 'random_string' method is provided for attributes whose exact value isn't important; this means you can
create multiple, unique model instances
- you can perform arbitrary set-up and execute any Ruby code prior to returning the hash
(as shown here where a <tt>password</tt> is generated and then used for both the <tt>:password</tt> and
- you can perform arbitrary set-up and execute any Ruby code prior to returning the hash
(as shown here where a <tt>password</tt> is generated and then used for both the <tt>:password</tt> and
<tt>:password_confirmation</tt> attributes)
- a <tt>new_modelname</tt> method is automatically provided that allows you to set up dependent
- a <tt>new_modelname</tt> method is automatically provided that allows you to set up dependent
model objects (in this case an instance of the <tt>Bar</tt> model)

=== Available methods

Based on the above definition FixtureReplacement makes the following methods available:

- <tt>random_string</tt>: generates a random string as shown above
- <tt>new_user</tt>: equivalent to <tt>User.new</tt> with the attributes for the user.
- <tt>create_user</tt>: equivalent to <tt>User.create!</tt> with the user's attributes.
- <tt>valid_user</tt>: returns a hash of the user's attributes including associations, specified in db/example_data.rb.
- <tt>valid_user_attributes</tt>: returns a hash of the user's attributes including associations, specified in db/example_data.rb.

=== Overriding attributes

Overrides of specific attributes can be performed as follows:

new_user(:thing => "overridden")
new_user(:thing => "overridden")
create_user(:thing => "overridden")

Overrides can also be used with associations:

scott = create_user(:username => "scott")
post = create_post(:user => scott)


=== attr_protected / attr_accessible

In the case that the model has an attr_protected field, FixtureReplacement
Expand All @@ -109,10 +109,30 @@ will assign the field as if it wasn't protected, which is convenient for testing

user = create_user(:username => "scott", :admin_status => true)
user.admin_status # => true


=== Validate your fixtures (thanks Fixjour)

Validate your fixture definitions after including it in the spec helper or test helper:

==== spec_helper.rb:

Spec::Runner.configuration do |config|
config.include FixtureReplacement
end

FixtureReplacement.validate!

==== test_helper.rb

class Test::Unit::TestCase
include FixtureReplacement
end

FixtureReplacement.validate!

=== Using FixtureReplacement within <tt>script/console</tt>

$ ./script/console
$ ./script/console
Loading development environment
>> include FR
=> Object
Expand Down
124 changes: 3 additions & 121 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,126 +1,8 @@
require 'rake'
require 'rake/testtask'
require 'hanna/rdoctask'
require 'spec/rake/spectask'
require 'spec/rake/verify_rcov'
require 'rake/contrib/rubyforgepublisher'

Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/**/*test*.rb']
t.verbose = true
Dir.glob("rake_tasks/**/*.rb").each do |file|
require file
end

desc 'Default: run unit tests.'
desc 'Default: run unit tests (specs + tests).'
task :default => [:spec, :test]

# Create specs + Rake Task

def doc_directory
"doc"
end

desc 'Generate documentation for the fixture_replacement plugin.'
Rake::RDocTask.new(:rdoc_without_analytics) do |rdoc|
rdoc.rdoc_dir = doc_directory
rdoc.title = 'FixtureReplacement'
rdoc.options << '--line-numbers' << '--inline-source'

rdoc.options << '--webcvs=http://github.com/smtlaissezfaire/fixturereplacement/tree/master/'

[
"README.rdoc",
"CHANGELOG.rdoc",
"GPL_LICENSE",
"MIT_LICENSE",
"contributions.rdoc",
"philosophy_and_bugs.rdoc",
"lib/**/*.rb"
].each do |file|
rdoc.rdoc_files.include(file)
end
end

task :rdoc => [:rdoc_without_analytics] do
google_analytics = File.read(File.dirname(__FILE__) + "/etc/google_analytics")
rdoc_index = File.dirname(__FILE__) + "/#{doc_directory}/index.html"

contents = File.read(rdoc_index)
contents.gsub!("</head>", "#{google_analytics}\n</head>")

File.open(rdoc_index, "r+") do |file|
file.write(contents)
end
end

task :rerdoc => [:clobber_rdoc, :rdoc]
task :clobber_rdoc => [:clobber_rdoc_without_analytics]

desc 'Run the specs'
Spec::Rake::SpecTask.new do |t|
t.warning = false
t.spec_opts = ["--color"]
end

desc 'Publish the website, building the docs first'
task :publish_website => [:build_docs] do
publisher = Rake::SshDirPublisher.new(
"[email protected]",
"/var/www/gforge-projects/replacefixtures/",
"doc"
)
publisher.upload
end

def create_doc_directory
unless File.exists?(doc_directory)
`mkdir doc`
end
end

task :create_doc_directory do
create_doc_directory
end

desc "Create the html specdoc"
Spec::Rake::SpecTask.new(:specdoc => :create_doc_directory) do |t|
t.spec_opts = ["--format", "html:doc/specdoc.html"]
end

desc 'Create the specdoc + rdoc'
task :build_docs => [:rerdoc, :specdoc, :rcov, :flog_to_disk]

desc "Run all examples with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.rcov = true
t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems']
t.rcov_dir = "doc/rcov"
end

desc "Feel the pain of my code, and submit a refactoring patch"
task :flog do
puts %x(find lib | grep ".rb$" | xargs flog)
end

task :flog_to_disk => :create_doc_directory do
puts "Flogging..."
%x(find lib | grep ".rb$" | xargs flog > doc/flog.txt)
puts "Done Flogging...\n"
end

def sloc
`sloccount #{File.dirname(__FILE__)}/lib`
end

desc "Output sloccount report. You'll need sloccount installed."
task :sloc do
puts "Counting lines of code"
puts sloc
end

desc "Write sloccount report"
task :output_sloc => :create_doc_directory do
File.open(File.dirname(__FILE__) + "/doc/lines_of_code.txt", "w") do |f|
f << sloc
end
end
2 changes: 1 addition & 1 deletion TODO.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
- Update website info
- Update with github info
- Register Lighthouse project
- Remove TODOs from Rubyforge tracker (http://rubyforge.org/tracker/?group_id=4556)
- Remove TODOs from Rubyforge tracker (http://rubyforge.org/tracker/?group_id=4556)
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.1
3 changes: 2 additions & 1 deletion contributions.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ Thanks to the following for making this software better:
- patch to ignore attr_protected in mass assignment (applied in revision 57)
- Most of this README Documentation (applied in revision 62)
- patch: silencing sqlite3 in memory creation of table output (applied in revision 72)
- doc patch: typo fix in readme
- Carl Porth
- patch: classify should be camelize (applied in revision 74)
- LinoJ, JW, Matthew Bass, Andy Watts, Dave Spurr
- bug reports
- Bryan Helmkamp: Feedback on back associating models.
- Pat Nakajima: Wonderful ideas from Fixjour. Elimination of default_* methods in favor
of new_* methods.
of new_* methods. validation of new instances.
4 changes: 2 additions & 2 deletions etc/bug_reports/2007_09_28_linoj.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ Hi Scott,

I'm wondering if you have insight into this or if I should go ask somewhere else.

I installed 2 plug-in today,
I installed 2 plug-in today,
first fixture_replacement plugin
and been using it successfully

Then I installed validates_email_format_of plugin
http://code.dunae.ca/validates_email_format_of

and added this to my User model
validates_email_format_of :email
validates_email_format_of :email

then script/server wont start
When I comment out this line, it's fine
Expand Down
2 changes: 1 addition & 1 deletion etc/google_analytics
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<script type="text/javascript">
_uacct = "UA-3080808-2";
urchinTracker();
</script>
</script>
6 changes: 3 additions & 3 deletions etc/patches/2007_09_14_default_model_name_with_arguments.diff
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Index: lib/fixture_replacement/fixture_replacement.rb
===================================================================
--- lib/fixture_replacement/fixture_replacement.rb (revision 1151)
+++ lib/fixture_replacement/fixture_replacement.rb (working copy)
--- lib/fixture_replacement/fixture_replacement.rb (revision 1151)
+++ lib/fixture_replacement/fixture_replacement.rb (working copy)
@@ -40,7 +40,8 @@
def merge_unevaluated_method(obj, method_for_instantiation, hash={})
hash.each do |key, value|
Expand All @@ -14,7 +14,7 @@ Index: lib/fixture_replacement/fixture_replacement.rb
end
@@ -63,9 +64,9 @@
default_method = "default_#{model_name}".to_sym

fixture_module.module_eval do
- define_method(default_method) do
+ define_method(default_method) do |*args|
Expand Down
Loading

0 comments on commit 1f55e5f

Please sign in to comment.