Module: Dataset::ModelFinders

Defined in:
lib/dataset/session_binding.rb

Overview

Whenever you use Dataset::RecordMethods, you will get finder methods in your tests that help you load instances of the records you have created (or named models).

create_record :person, :jimmy, :name => 'Jimmy'
person_id(:jimmy)  => The id was captured from create_record
people(:jimmy)     => The same as Jimmy.find(person_id(:jimmy))

The methods will not exist in a test unless it utilizes a dataset (or defines one itself through the block technique) that creates a record for the type.

You may also pass multiple names to these methods, which will have them return an array of values.

people(:jimmy, :jane, :jeff)     => [<# Person :name => 'Jimmy'>, <# Person :name => 'Jane'>, <# Person :name => 'Jeff'>]
person_id(:jimmy, :jane, :jeff)  => [1, 2, 3]

NOTE the plurality of the instance finder, versus the singularity of the id finder.

Single Table Inheritence

class Person < ActiveRecord::Base; end
class User < Person; end

create_record :user, :bobby, :name => 'Bobby'

people(:bobby) OR users(:bobby)

Instance Method Summary collapse

Instance Method Details

#create_finders(record_meta) ⇒ Object

:nodoc:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dataset/session_binding.rb', line 44

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)
  
  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
        end
        names.size == 1 ? models.first : models
      end
    end
  end
  
  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
        end
        names.size == 1 ? ids.first : ids
      end
    end
  end
  
  @finders_generated << heirarchy_finders_hash
end