Skip to content
Rails開発中に出てくる、fixtureの取り回しの悪いところを改良しようとして、これまでやっていた改良点をgemにまとめてみました。今後はオプションの充実が目標
Ruby
Find file
Pull request Compare This branch is 138 commits behind baban:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
.document
.gitignore
CHANGELOG
Gemfile
Gemfile.lock
History.txt
LICENSE.txt
README.ja.rdoc
README.rdoc
Rakefile
VERSION
flextures.gemspec

README.rdoc

flextures

Abstruct

This plug-in aim to resolve many problems, durling rails developping about fixtures. Basic commands is simple. Each commands load or dump fixtures.

rake db:flextures:load
rake db:flextures:dump

Major different point is four.

  1. Fixture file is prefered CSV to YAML.

  2. Don't stop load if table colums are'nt match fixture file's column.

  3. Fixture file name can change if file name is not equal table name.

  4. Fixture data translate to use filter like factory girl.

How to install

This program is implemented Rails Plug-in. You want to install this plug-in. Please use bundler.

gem "flextures"

(Development emnvoriment must be ruby1.9 and rails3)

How to use

rake command

load command input fixtures file under “spec/fixtures/”. (Loading directory can change configuration file)

rake db:flextures:load
rake db:flextures:dump

rake command can set options. For example, this option set dump file name. (Option dump only “users.csv”)

rake db:flextures:dump TABLE=users

If you use this option load command. Option set load file name. (loading “users.csv” or “users.yml”)

rake db:flextures:load MODEL=User

Other options…

TABLE : set table name
MODEL : set model name
DIR : set directory name
FIXTURES : set fixture file name
FORMAT : dumping format change csv to yaml (aka. ymlload, ymldump command)
T : alias TABLE option
M : alias MODEL option
D : alias DIR option
F : alias FIXTURES option

if you only load CSV file or YAML file, next command can load.

rake db:flextures:csvload
rake db:flextures:ymlload
rake db:flextures:csvdump
rake db:flextures:ymldump

RSpec flexture support

Fixture load function implemented for RSpec.

describe ItemShopController do
  flextures :users, :items

flexture function can write like a “fixture” function, implemented in RSpec. But, “flexture” function ignore columns change.

Flextures function can change load file name.

describe ItemShopController do
  flextures :items, :users => :users_for_itmshop # load "users_for_itemshop.csv"

Flextures load & dump filter

load filter

If you create filter file.(File name is “config/flextures.factory.rb”) Factory filter translate fixture data and set database.

For example, this code set current time to last_login_date column.

Flextures::Factory.define :users do |f| 
  f.last_login_date = DateTime.now
end

This sample, generate name and sex automatically, and other tables data generate

require 'faker'
Flextures::Factory.define :users do |f|
  f.name= Faker::Name.name if !f.name  # gemerate name
  f.sex= [0,1].shuffle.first if !f.sex # generate sex
  # factory filter can generate data, use has_many association
  f.items<< [ Item.new( master_item_id: 1, count: 5 ), Item.new( master_item_id: 2, count: 3 ) ]
end

dump filter

if you need to convert table data into other data format, you use dump filter. (dump filter is same file as load filter)

dump filter has hash argumtne, it is formatted colum name key and convert method, proc, lambda value

Flextures::DumpFilter.define :users, {
  :encrypted_password => lambda { |v| Base64.encode64(v) }
}

Configuration file

Configuration file can change load and dump directory (file is config/flextures.config.rb)

# config/flextures.config.rb
module Flextures
  # Load and dump directory change "spec/fixtures/" to "test/fixtures/"
  Config.fixture_load_directory = "test/fixtures/"
  Config.fixture_dump_directory = "test/fixtures/"
end
Something went wrong with that request. Please try again.