Module: Dataset::RecordMethods
- Defined in:
- lib/dataset/session_binding.rb
Overview
Any Dataset::Base subclass, dataset block, or test method in a dataset-using test context (including setup/teardown/before/after) may create and access models through these methods. Note that you should use Dataset::ModelFinders if you can for finding your created data.
Instance Method Summary collapse
-
#create_model(*args) ⇒ Object
This will instantiate your model class and assign each attribute WITHOUT using mass assignment.
-
#create_record(*args) ⇒ Object
Similar to old fashioned fixtures, this will do a direct database insert, without running any validations or preventing you from writing attr_protected attributes.
-
#find_id(*args) ⇒ Object
Dataset will track each of the records it creates by symbolic name to id.
-
#find_model(*args) ⇒ Object
Dataset will track each of the records it creates by symbolic name to id.
-
#name_model(*args) ⇒ Object
This is a great help when you want to create records in a custom helper method, then make it and maybe things associated to it available to tests through the Dataset::ModelFinders.
-
#name_to_sym(name) ⇒ Object
Converts string names into symbols for use in naming models.
Instance Method Details
#create_model(*args) ⇒ Object
This will instantiate your model class and assign each attribute WITHOUT using mass assignment. Validations will be run. Very nice for complex structures or hard to keep right validations, but potentially a bit slower, since it runs through all that ActiveRecord code.
create_model :type, :symbolic_name, :attr1 => 'value', :attr2 => 'value', :etc => 'etc'
The symbolic_name is an optional parameter. You may replace type with an ActiveRecord::Base subclass or anything that works with:
to_s.classify.constantize
The id of the record will be kept from the instance that is saved.
116 117 118 |
# File 'lib/dataset/session_binding.rb', line 116 def create_model(*args) dataset_session_binding.create_model(*args) end |
#create_record(*args) ⇒ Object
Similar to old fashioned fixtures, this will do a direct database insert, without running any validations or preventing you from writing attr_protected attributes. Very nice for speed, but kind of a pain if you have complex structures or hard to keep right validations.
create_record :type, :symbolic_name, :attr1 => 'value', :attr2 => 'value', :etc => 'etc'
The symbolic_name is an optional parameter. You may replace type with an ActiveRecord::Base subclass or anything that works with:
to_s.classify.constantize
The id of the model will be a hash of the symbolic name.
98 99 100 |
# File 'lib/dataset/session_binding.rb', line 98 def create_record(*args) dataset_session_binding.create_record(*args) end |
#find_id(*args) ⇒ Object
Dataset will track each of the records it creates by symbolic name to id. When you need the id of a record, there is no need to go to the database.
find_id :person, :bobby => 23425234
You may pass one name or many, with many returning an Array of ids.
128 129 130 |
# File 'lib/dataset/session_binding.rb', line 128 def find_id(*args) dataset_session_binding.find_id(*args) end |
#find_model(*args) ⇒ Object
Dataset will track each of the records it creates by symbolic name to id. When you need an instance of a record, the stored id will be used to do the fastest lookup possible: Person.find(23425234).
find_model :person, :bobby => <#Person :id => 23425234, :name => 'Bobby'>
You may pass one name or many, with many returning an Array of instances.
141 142 143 |
# File 'lib/dataset/session_binding.rb', line 141 def find_model(*args) dataset_session_binding.find_model(*args) end |
#name_model(*args) ⇒ Object
This is a great help when you want to create records in a custom helper method, then make it and maybe things associated to it available to tests through the Dataset::ModelFinders.
thingy = create_very_complex_thingy_and_stuff
name_model thingy, :thingy_bob
name_model thingy.part, :thingy_part
In tests:
thingies(:thingy_bob)
parts(:thingy_part)
158 159 160 |
# File 'lib/dataset/session_binding.rb', line 158 def name_model(*args) dataset_session_binding.name_model(*args) end |
#name_to_sym(name) ⇒ Object
Converts string names into symbols for use in naming models
name_to_sym 'my name' => :my_name
name_to_sym 'RPaul' => :r_paul
167 168 169 |
# File 'lib/dataset/session_binding.rb', line 167 def name_to_sym(name) dataset_session_binding.name_to_sym(name) end |