├── install.rb ├── uninstall.rb ├── lib ├── gimmeh_fixtures.rb ├── fixtures_controller.rb └── fixture_container.rb ├── test ├── test_helper.rb └── gimmeh_fixtures_test.rb ├── init.rb ├── routes.rb ├── LICENSE ├── Rakefile └── README /install.rb: -------------------------------------------------------------------------------- 1 | # Install hook code here 2 | -------------------------------------------------------------------------------- /uninstall.rb: -------------------------------------------------------------------------------- 1 | # Uninstall hook code here 2 | -------------------------------------------------------------------------------- /lib/gimmeh_fixtures.rb: -------------------------------------------------------------------------------- 1 | module GimmehFixtures 2 | @@fixture_container = FixtureContainer.new 3 | 4 | def fixtures 5 | @@fixture_container 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'lib/fixture_container' 3 | 4 | RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../../../../')) 5 | 6 | require RAILS_ROOT + '/config/boot' 7 | require 'environment' 8 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | if %w(test development).include?(RAILS_ENV) 2 | # Shoving this into ActiveRecord::Base? Pretty horrible! 3 | # Who cares? It's the test/development environment. 4 | ActiveRecord::Base.send(:extend, GimmehFixtures) 5 | 6 | require File.dirname(__FILE__) + '/routes' 7 | end 8 | -------------------------------------------------------------------------------- /routes.rb: -------------------------------------------------------------------------------- 1 | module ActionController 2 | module Routing #:nodoc: 3 | class RouteSet #:nodoc: 4 | alias_method :draw_without_fixtures_routes, :draw 5 | def draw 6 | draw_without_fixtures_routes do |map| 7 | map.fixtures 'fixtures.:format', :controller => 'fixtures', :action => 'index' 8 | map.fixtures 'fixtures/:fixturename.:format', :controller => 'fixtures', :action => 'fixture' 9 | yield map 10 | end 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 14 rue de Plaisance, 75014 Paris, France 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. -------------------------------------------------------------------------------- /test/gimmeh_fixtures_test.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), 'test_helper') 2 | 3 | class GimmehFixturesTest < Test::Unit::TestCase 4 | 5 | # These tests are really lacking. Mostly, that's because testing 6 | # the functionality of the majority of this plugin really entails 7 | # testing how your Rails app works once its been loaded. 8 | # 9 | # You don't see exception_logger having a robust test suite, do 10 | # you now? No, you don't. So there. 11 | 12 | def test_fixture_container 13 | assert fc = FixtureContainer.new 14 | assert fc.to_hash 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/fixtures_controller.rb: -------------------------------------------------------------------------------- 1 | class FixturesController < ActionController::Base 2 | 3 | def index 4 | @all_fixtures = ActiveRecord::Base.fixtures.to_hash 5 | 6 | respond_to do |format| 7 | format.json { render :json => @all_fixtures.to_json } 8 | format.xml { render :xml => @all_fixtures.to_xml } 9 | end 10 | end 11 | 12 | def fixture 13 | @fixtures = ActiveRecord::Base.fixtures[params[:fixturename]] 14 | 15 | respond_to do |format| 16 | format.json { render :json => @fixtures.to_json } 17 | format.xml { render :xml => @fixtures.to_xml } 18 | end 19 | end 20 | end 21 | 22 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rake/rdoctask' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the gimmeh_fixtures plugin.' 9 | Rake::TestTask.new(:test) do |t| 10 | t.libs << 'lib' 11 | t.pattern = 'test/**/*_test.rb' 12 | t.verbose = true 13 | end 14 | 15 | desc 'Generate documentation for the gimmeh_fixtures plugin.' 16 | Rake::RDocTask.new(:rdoc) do |rdoc| 17 | rdoc.rdoc_dir = 'rdoc' 18 | rdoc.title = 'GimmehFixtures' 19 | rdoc.options << '--line-numbers' << '--inline-source' 20 | rdoc.rdoc_files.include('README') 21 | rdoc.rdoc_files.include('lib/**/*.rb') 22 | end 23 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | GimmehFixtures 2 | ============== 3 | 4 | Provides a RESTful API to your fixtures so they can be consumed by external 5 | applications. This is useful for giving an external test suite knowledge 6 | of your fixtures without coupling the two code bases. 7 | 8 | Runs only in test and development environments. 9 | 10 | Example Usage 11 | ============= 12 | 13 | 1. Fire up your Rails app: 14 | $ script/server 15 | 16 | 2. Ask for all the fixtures: 17 | $ curl http://localhost:3000/fixtures.xml 18 | 19 | 3. Now, ask for one particular set of fixtures: 20 | $ curl http://localhost:3000/fixtures/users.json 21 | 22 | 4. Now your external test suite is empowered with 23 | knowledge of your fixtures. 'grats, champ. 24 | 25 | Author 26 | ====== 27 | Alex Payne . 28 | 29 | License 30 | ======= 31 | Released under the WTFPL. 32 | -------------------------------------------------------------------------------- /lib/fixture_container.rb: -------------------------------------------------------------------------------- 1 | class FixtureContainer 2 | 3 | include Enumerable 4 | 5 | @@all_fixtures = {} 6 | 7 | def initialize 8 | load_yaml_fixtures 9 | end 10 | 11 | def each 12 | @@all_fixtures.each { |item| yield item } 13 | end 14 | 15 | def [](key) 16 | @@all_fixtures[key] 17 | end 18 | 19 | def to_hash 20 | @@all_fixtures 21 | end 22 | 23 | def keys 24 | @@all_fixtures.keys.collect 25 | end 26 | 27 | private 28 | 29 | def load_yaml_fixtures 30 | fixture_path = "#{RAILS_ROOT}/test/fixtures/" 31 | 32 | Dir["#{fixture_path}/**/*.yml"].each do |fixture_file| 33 | fixture_name = fixture_file.split('/').last.split('.').first 34 | 35 | fixture_contents = IO.read(fixture_file) 36 | next if fixture_contents.empty? 37 | 38 | erbed_contents = ERB.new(fixture_contents).result 39 | @@all_fixtures[fixture_name] = YAML::load(erbed_contents) 40 | end 41 | end 42 | 43 | end --------------------------------------------------------------------------------