Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/ar_fixtures.rb
Overview
Extension to make it easy to read and write data to a file.
Class Method Summary collapse
-
.dump_to_file(path = nil, limit = nil) ⇒ Object
Writes content of this table to db/table_name.yml, or the specified file.
-
.habtm_to_fixture ⇒ Object
Write the habtm association table.
-
.load_from_file(path = nil) ⇒ Object
Delete existing data in database and load fresh from file in db/table_name.yml.
-
.to_fixture(limit = nil) ⇒ Object
Write a file that can be loaded with fixture :some_table in tests.
-
.to_skeleton ⇒ Object
Generates a basic fixture file in test/fixtures that lists the table’s field names.
-
.write_file(path, content) ⇒ Object
:nodoc:.
Class Method Details
.dump_to_file(path = nil, limit = nil) ⇒ Object
Writes content of this table to db/table_name.yml, or the specified file.
Writes all content by default, but can be limited.
9 10 11 12 13 14 |
# File 'lib/ar_fixtures.rb', line 9 def dump_to_file(path=nil, limit=nil) opts = {} opts[:limit] = limit if limit path ||= "db/#{table_name}.yml" write_file(File.(path, RAILS_ROOT), self.find(:all, opts).to_yaml) end |
.habtm_to_fixture ⇒ Object
Write the habtm association table
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ar_fixtures.rb', line 61 def habtm_to_fixture joins = self.reflect_on_all_associations.select { |j| j.macro == :has_and_belongs_to_many } joins.each do |join| hsh = {} connection.select_all("SELECT * FROM #{join.[:join_table]}").each_with_index { |record, i| hsh["join_#{'%05i' % i}"] = record } write_file(File.("test/fixtures/#{join.[:join_table]}.yml", RAILS_ROOT), hsh.to_yaml(:SortKeys => true)) end end |
.load_from_file(path = nil) ⇒ Object
Delete existing data in database and load fresh from file in db/table_name.yml
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ar_fixtures.rb', line 17 def load_from_file(path=nil) path ||= "db/#{table_name}.yml" self.destroy_all if connection.respond_to?(:reset_pk_sequence!) connection.reset_pk_sequence!(table_name) end records = YAML::load( File.open( File.(path, RAILS_ROOT) ) ) records.each do |record| record_copy = self.new(record.attributes) record_copy.id = record.id # For Single Table Inheritance klass_col = record.class.inheritance_column.to_sym if record[klass_col] record_copy.type = record[klass_col] end record_copy.save end if connection.respond_to?(:reset_pk_sequence!) connection.reset_pk_sequence!(table_name) end end |
.to_fixture(limit = nil) ⇒ Object
Write a file that can be loaded with fixture :some_table in tests. Uses existing data in the database.
Will be written to test/fixtures/table_name.yml
. Can be restricted to some number of rows.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ar_fixtures.rb', line 49 def to_fixture(limit=nil) opts = {} opts[:limit] = limit if limit write_file(File.("test/fixtures/#{table_name}.yml", RAILS_ROOT), self.find(:all, opts).inject({}) { |hsh, record| hsh.merge("#{table_name.singularize}_#{'%05i' % record.id}" => record.attributes) }.to_yaml(:SortKeys => true)) habtm_to_fixture end |
.to_skeleton ⇒ Object
Generates a basic fixture file in test/fixtures that lists the table’s field names.
You can use it as a starting point for your own fixtures.
record_1:
name:
rating:
record_2:
name:
rating:
TODO Automatically add :id field if there is one.
86 87 88 89 90 91 92 93 |
# File 'lib/ar_fixtures.rb', line 86 def to_skeleton record = { "record_1" => self.new.attributes, "record_2" => self.new.attributes } write_file(File.("test/fixtures/#{table_name}.yml", RAILS_ROOT), record.to_yaml) end |
.write_file(path, content) ⇒ Object
:nodoc:
95 96 97 98 99 |
# File 'lib/ar_fixtures.rb', line 95 def write_file(path, content) # :nodoc: f = File.new(path, "w+") f.puts content f.close end |