lib/dataset/session_binding.rb in dataset-1.3.1 vs lib/dataset/session_binding.rb in dataset-1.3.2
- old
+ new
@@ -3,12 +3,12 @@
# that doesn't exist. For example, if you do people(:jenny), and yet no
# record was ever created with the symbolic name :jenny, this error will be
# raised.
#
class RecordNotFound < StandardError
- def initialize(record_type, symbolic_name)
- super "There is no '#{record_type.name}' found for the symbolic name ':#{symbolic_name}'."
+ def initialize(record_heirarchy, symbolic_name)
+ super "There is no '#{record_heirarchy.base_class.name}' found for the symbolic name ':#{symbolic_name}'."
end
end
# Whenever you use Dataset::RecordMethods, you will get finder methods in
# your tests that help you load instances of the records you have created
@@ -39,16 +39,16 @@
# create_record :user, :bobby, :name => 'Bobby'
#
# people(:bobby) OR users(:bobby)
#
module ModelFinders
- def create_finder(record_meta) # :nodoc:
+ def create_finders(record_meta) # :nodoc:
@finders_generated ||= []
+ heirarchy_finders_hash = record_meta.heirarchy.model_finder_names.join('').hash
+ return if @finders_generated.include?(heirarchy_finders_hash)
- return if @finders_generated.include?(record_meta)
-
- record_meta.model_finder_names.each do |finder_name|
+ record_meta.heirarchy.model_finder_names.each do |finder_name|
unless instance_methods.include?(finder_name)
define_method finder_name do |*symbolic_names|
names = Array(symbolic_names)
models = names.inject([]) do |c,n|
c << dataset_session_binding.find_model(record_meta, n); c
@@ -56,11 +56,11 @@
names.size == 1 ? models.first : models
end
end
end
- record_meta.id_finder_names.each do |finder_name|
+ record_meta.heirarchy.id_finder_names.each do |finder_name|
unless instance_methods.include?(finder_name)
define_method finder_name do |*symbolic_names|
names = Array(symbolic_names)
ids = names.inject([]) do |c,n|
c << dataset_session_binding.find_id(record_meta, n); c
@@ -68,11 +68,11 @@
names.size == 1 ? ids.first : ids
end
end
end
- @finders_generated << record_meta
+ @finders_generated << heirarchy_finders_hash
end
end
# Any Dataset::Base subclass, dataset block, or test method in a
# dataset-using test context (including setup/teardown/before/after) may
@@ -205,27 +205,29 @@
insert(Dataset::Record::Fixture, record_type, *args)
end
def find_id(record_type_or_meta, symbolic_name)
record_meta = record_meta_for_type(record_type_or_meta)
- if local_id = @id_cache[record_meta.id_cache_key][symbolic_name]
+ heirarchy = record_meta.heirarchy
+ if local_id = @id_cache[heirarchy.id_cache_key][symbolic_name]
local_id
elsif !parent_binding.nil?
parent_binding.find_id record_meta, symbolic_name
else
- raise RecordNotFound.new(record_meta, symbolic_name)
+ raise RecordNotFound.new(heirarchy, symbolic_name)
end
end
def find_model(record_type_or_meta, symbolic_name)
record_meta = record_meta_for_type(record_type_or_meta)
- if local_id = @id_cache[record_meta.id_cache_key][symbolic_name]
- record_meta.record_class.find local_id
+ heirarchy = record_meta.heirarchy
+ if local_id = @id_cache[heirarchy.id_cache_key][symbolic_name]
+ heirarchy.base_class.find local_id
elsif !parent_binding.nil?
parent_binding.find_model record_meta, symbolic_name
else
- raise RecordNotFound.new(record_meta, symbolic_name)
+ raise RecordNotFound.new(heirarchy, symbolic_name)
end
end
def install_block_variables(target)
block_variables.each do |k,v|
@@ -233,12 +235,12 @@
end
end
def name_model(record, symbolic_name)
record_meta = database.record_meta(record.class)
- @model_finders.create_finder(record_meta)
- @id_cache[record_meta.id_cache_key][symbolic_name] = record.id
+ @model_finders.create_finders(record_meta)
+ @id_cache[record_meta.heirarchy.id_cache_key][symbolic_name] = record.id
record
end
def record_meta_for_type(record_type)
record_type.is_a?(Record::Meta) ? record_type : begin
@@ -256,13 +258,13 @@
symbolic_name, attributes = extract_creation_arguments args
record_meta = record_meta_for_type(record_type)
record = dataset_record_class.new(record_meta, attributes, symbolic_name, self)
return_value = nil
- @model_finders.create_finder(record_meta)
+ @model_finders.create_finders(record_meta)
ActiveRecord::Base.silence do
return_value = record.create
- @id_cache[record_meta.id_cache_key][symbolic_name] = record.id
+ @id_cache[record_meta.heirarchy.id_cache_key][symbolic_name] = record.id
end
return_value
end
def extract_creation_arguments(arguments)
\ No newline at end of file