Skip to content

Commit

Permalink
Minor changes to class name overriding support
Browse files Browse the repository at this point in the history
Rename model_class to class_map, and model_klass to model_class.

Add documentation to the README about this, and bump version to
1.4.0.
  • Loading branch information
jeremyevans committed Sep 3, 2014
1 parent 0c8db1c commit 8b02975
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README
Expand Up @@ -235,6 +235,19 @@ new fixtures.
Instantiated fixtures are not available with this plugin. Instead, you should
use load(:model__fixture_name).

== Namespace Issues

By default, fixture dependencies is going to load the model with the camelized
name in the symbol used. So for :foo_bar__baz, it's going to look for
the fixture with name baz for the model FooBar. If your model is namespaced,
such as Foo::Bar, this isn't going to work well. In that case, you can
override the default mapping:

FixtureDependencies.class_map[:bar] = Foo::Bar

and then use :bar__baz to load the fixture with name baz for the model
Foo::Bar.

== Troubleshooting

If you run into problems with loading your fixtures, it can be difficult to see
Expand Down
2 changes: 1 addition & 1 deletion fixture_dependencies.gemspec
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = "fixture_dependencies"
s.version = "1.3.3"
s.version = "1.4.0"
s.author = "Jeremy Evans"
s.email = "code@jeremyevans.net"
s.platform = Gem::Platform::RUBY
Expand Down
18 changes: 9 additions & 9 deletions lib/fixture_dependencies.rb
Expand Up @@ -5,7 +5,7 @@
class FixtureDependencies
@fixtures = {}
@loaded = {}
@model_class = {}
@class_map = {}
@verbose = 0

# Load all record arguments into the database. If a single argument is
Expand Down Expand Up @@ -63,7 +63,7 @@ def self.load(*records)

class << FixtureDependencies
attr_reader :fixtures, :loaded
attr_accessor :verbose, :fixture_path, :model_class
attr_accessor :verbose, :fixture_path, :class_map

private

Expand All @@ -77,7 +77,7 @@ def add(model_name, name, attributes)
# the fixture name.
def get(record)
model_name, name = split_name(record)
model = model_klass(model_name)
model = model_class(model_name)
model_method(:model_find, model_type(model), model, fixtures[model_name.to_sym][name.to_sym][fixture_pk(model)])
end

Expand All @@ -86,7 +86,7 @@ def get(record)
def load_yaml(model_name)
raise(ArgumentError, "No fixture_path set. Use FixtureDependencies.fixture_path = ...") unless fixture_path

filename = model_klass(model_name).table_name
filename = model_class(model_name).table_name
yaml_path = File.join(fixture_path, "#{filename}.yml")

if File.exist?(yaml_path)
Expand Down Expand Up @@ -124,10 +124,10 @@ def model_type(model)

# Return the class associated with the given model_name. By default, the
# class name is automagically derived from the model name, however this
# can be overridden by `FixtureDependencies.model_class[:name] =
# Some::Class`.
def model_klass(model_name)
model_class[model_name.to_sym] or model_name.camelize.constantize
# can be overridden by <tt>FixtureDependencies.class_map[:name] =
# Some::Class</tt>.
def model_class(model_name)
class_map[model_name.to_sym] || model_name.camelize.constantize
end

# Split the fixture name into the name of the model and the name of
Expand All @@ -148,7 +148,7 @@ def use(record, loading = [], procs = {})
puts "#{spaces}load stack:#{loading.inspect}" if verbose > 1
loading.push(record)
model_name, name = split_name(record)
model = model_klass(model_name)
model = model_class(model_name)
unless loaded[model_name.to_sym]
puts "#{spaces}loading #{model.table_name}.yml" if verbose > 0
load_yaml(model_name)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -10,7 +10,7 @@
require 'fixture_dependencies'
FixtureDependencies.fixture_path = File.join(File.dirname(File.expand_path(__FILE__)), 'fixtures')
#FixtureDependencies.verbose = 3
FixtureDependencies.model_class[:tag] = Name::Tag
FixtureDependencies.class_map[:tag] = Name::Tag

if defined?(RSpec)
require 'rspec/version'
Expand Down

0 comments on commit 8b02975

Please sign in to comment.