attachment_fu_fixtures icon indicating copy to clipboard operation
attachment_fu_fixtures copied to clipboard

Allows attachment_fu models to fully use fixtures. Ideal for seed, sample or dev data.

attachment_fu_fixtures

This plugin enables models that implement attachment_fu to also use fixtures. It processes and stores the attachment file as if it had being uploaded. Useful if you use fixtures in your tests/specs and want to test with real attachments, but especially useful for, and intended to be used as, seed data fixtures.

Fixtures are simple and convenient for seed/sample/dev data; they have built-in tasks (rake db:fixtures:load) and are automatically generated with scaffolds.

Great for preview and staging environments where you want content and attachments to be available. Images, for example, can help a fair deal with presentation (think about your client viewing how the site is evolving) and to present a more realistic experience. Perfect for production seed data that requires attachments.

Also useful in development, for when you use fixtures to populate the interface. Essential for front-end development, and to get a quick glance at the interface.

Features

  • Parses and stores attachment files, just like any upload would
  • Seamless integration with existing fixtures
  • Extremely simple to use
  • Keeps all model relationships, just like regular fixtures

Dependencies

  • Rick Olson's attachment_fu plugin
  • Rspec and SQLite if you want to run the specs
  • Rails, etc.

Examples

To use, all you have to do is add a fixture for the attachment with an 'attachment_file' field, pointing to your attachment file. That's it!

= fixtures/images.yml beach: title: kho phi phi beach attachment_file: test/images/maya.png

All other fields required by attachment_fu will be automatically filled in. If explicitly specified in the fixture though, they take precedence.

belongs_to associations are specified just like in normal fixtures. For example, for an image that belongs to a user:

= fixtures/images.yml harbour: title: victoria harbour attachment_file: test/images/harbour.png user: mynyml

To load the fixture data, cd to RAILS_ROOT and call

rake db:fixtures:load #for test/fixtures rake spec:db:fixtures:load #for spec/fixtures

TIP: If you also use fixtures for tests/specs, I recommend you create a separate set of fixtures to use as seed data. You can store them in another directory (e.g. db/dev_data, db/fixtures, or even categorize them per environment) and create a custom rake task to load them (you can copy-paste rails's from railties/lib/tasks/databases.rake and simply change the path).


The examples above use rails 2.0 style fixtures. If you're a bit confused by how fixtures are written here (e.g. because they don't have ids, etc.), check out http://railscasts.com/episodes/81

Installation

If you're using edge rails from the github repository, you can install with the built in plugin installation command

./script/plugin install git://github.com/mynyml/attachment_fu_fixtures.git

Otherwise you can install manually with just a few steps

o download the tarball at http://github.com/mynyml/attachment_fu_fixtures/tarball/master into your vendor/plugins directory o extract it o rename the extracted directory to attachment_fu_fixtures o delete the file you downloaded (you won't need it anymore)

TIP: If you're on linux, the terminal commands for the above steps are:

from witin your vendor/plugins directory:

wget http://github.com/mynyml/attachment_fu_fixtures/tarball/master; tar zxvf mynyml-attachment_fu_fixtures-.tar.gz; mv mynyml-attachment_fu_fixtures-/ attachment_fu_fixtures; rm mynyml-attachment_fu_fixtures-*.tar.gz

Tests

Tested with:

  • attachment_fu (r3179 and commit 3836c313d6)
  • rails (2.0.2 r9237 and 2.0.2 gem)
  • rspec (1.1.4)

To run specs:

  • make sure rspec is installed
  • make sure attachment_fu is installed
  • make sure sqlite is installed
  • run from RAILS_ROOT with rake spec:plugins

Contributions

  • Thanks to Patrick Crosby for submitting a patch for the model_class bug
  • Thanks to Jeff Webb for patching HABTM and STI handling

Contact

If you have suggestions, comments, a patch, a git pull request, rants, doc fixes/improvements, etc., feel free to contact me: mynyml at gmail, irc.freenode.net #rubyonrails

And if you find this plugin useful... I especially appreciate emails mentioning what sites/projects it's been used with

Happy Hacking!


Copyright (c) 2008 Martin Aumont (mynyml), released under the MIT license