# Copyright (c) 2006 Kelly McCauley # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # Extension to make it easy to read and write data to a file. class ActiveRecord::Base # Write a file that can be loaded with fixture :some_table in tests. def self.to_fixture(path = nil, options = {}, habtm_options = {}) if path file = File.open(File.expand_path("#{table_name}.yml", path), 'w') else raise "No destination path specified" end find_options = { :order => 'id' }.merge!(options) cnt = 0 omap = YAML::Omap[] self.find(:all, find_options).each do |i| cnt += 1 omap << ["r_#{cnt}", i.attributes] end file.puts(omap.to_yaml) file.close habtm_to_fixture(path, habtm_options) end # Write the habtm association table def self.habtm_to_fixture(path = nil, options = {}) joins = self.reflect_on_all_associations.select { |j| j.macro == :has_and_belongs_to_many } joins.each do |join| if path file = File.open(File.expand_path("#{join.options[:join_table]}.yml", path), 'w') else raise "No destination path specified" end cnt = 0 omap = YAML::Omap[] connection.select_all("SELECT * FROM #{join.options[:join_table]}").each do |i| cnt += 1 omap << ["r_#{cnt}", i] end file.puts(omap.to_yaml) file.close end end # Write a file that can be loaded with fixture :some_table in tests. def self.to_csv(path = nil, options = {}, habtm_options = {}) if path file = File.open(File.expand_path("#{table_name}.csv", path), 'w') else raise "No destination path specified" end CSV::Writer.generate(file) do |csv| find_options = { :order => 'id' }.merge!(options) attr_names = [] header_row = [] self.find(:all, find_options).each do |i| if header_row.empty? attr_names = i.attribute_names.delete_if {|a| a == 'id'} attr_names.sort! header_row = ['id', attr_names].flatten csv << header_row end row = [i.id] attr_names.each do |an| row << i.attributes[an] end csv << row end end file.close habtm_to_csv(path, habtm_options) end # Write the habtm association table def self.habtm_to_csv(path = nil, options = {}) joins = self.reflect_on_all_associations.select { |j| j.macro == :has_and_belongs_to_many } joins.each do |join| if path file = File.open(File.expand_path("#{join.options[:join_table]}.csv", path), 'w') else raise "No destination path specified" end CSV::Writer.generate(file) do |csv| attr_names = [] header_row = [] connection.select_all("SELECT * FROM #{join.options[:join_table]}").each do |i| if header_row.empty? attr_names = i.attribute_names.delete_if {|a| a == 'id'} attr_names.sort! header_row = attr_names header_row.unshift('id') if i.attribute_present?('id') csv << header_row end row = [] row << i.id if i.attribute_present?('id') attr_names.each do |an| row << i.attributes[an] end csv << row end end file.close end end end