Sha256: 3e0a10cb233240f70ec06fc258c28fd540a759262ddf96b89f6dce644384fc19
Contents?: true
Size: 1.18 KB
Versions: 3
Compression:
Stored size: 1.18 KB
Contents
require 'active_support/inflector' require 'active_support/core_ext' module Hydroponics module Actions # Foreign Dupe # # Just like Dupe, except it pulls valid foreign keys from table foreign_table. # # It starts from the beginning and increments as it goes. def foreign_dupe(table, data) table = table.to_sym data.stringify_keys! foreign_table = data['foreign_table'].to_sym foreign_key_col = data['foreign_key'] || (foreign_table.to_s.singularize + "_id") foreign_key_col = foreign_key_col.to_sym current_count = @db[table].count target_count = data['count'] || current_count if target_count > current_count foreign_ids = @db[foreign_table].map(:id) first_row = @db[table].first first_row.delete(:id) n = target_count - current_count @db[table].multi_insert( (1..n).collect { |i| first_row.merge( Hash[foreign_key_col, foreign_ids[i % foreign_ids.size]] ) } ) elsif current_count > target_count cutoff_id = @db[table].map(:id)[target_count-1] @db[table].filter("id > #{cutoff_id}").delete end @db[table].count.to_s end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
hydroponics-0.3.5 | app/actions/foreigndupe.rb |
hydroponics-0.3.4 | app/actions/foreigndupe.rb |
hydroponics-0.3.3 | app/actions/foreigndupe.rb |