Module: Railstar::ActiveRecordExt::ClassMethods
- Defined in:
- lib/railstar/active_record_ext.rb
Instance Method Summary collapse
- #create_from_csv(file_path) ⇒ Object
- #create_from_yml(file_path) ⇒ Object
- #truncation ⇒ Object
- #truncation!(sym = :yml) ⇒ Object
Instance Method Details
#create_from_csv(file_path) ⇒ Object
33 34 35 |
# File 'lib/railstar/active_record_ext.rb', line 33 def create_from_csv(file_path) CSV.foreach(file_path, :headers => true) {|row| self.create Hash[*row.to_a.flatten] } end |
#create_from_yml(file_path) ⇒ Object
27 28 29 30 31 |
# File 'lib/railstar/active_record_ext.rb', line 27 def create_from_yml(file_path) YAML.load_file(file_path).each do |value| self.create value.is_a?(Array) ? value.last : value end end |
#truncation ⇒ Object
5 6 7 8 |
# File 'lib/railstar/active_record_ext.rb', line 5 def truncation return if self.count > 0 self.truncation! end |
#truncation!(sym = :yml) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/railstar/active_record_ext.rb', line 10 def truncation!(sym=:yml) table_name = self.to_s.underscore.pluralize file_name = "#{table_name}.#{sym.to_s}" file_path = File.join(Rails.root, "resources", "db", file_name) raise "#{file_path} file not found." unless File.exist?(file_path) self.transaction do case self.connection.adapter_name when "SQLite" self.connection.execute("DELETE FROM `#{self.table_name}`") else self.connection.execute("TRUNCATE TABLE `#{self.table_name}`") end self.send("create_from_#{sym.to_s}", file_path) end end |