class Rapids::Batch::ColumnsHelper
Public Class Methods
new(model,batch)
click to toggle source
# File lib/rapids/batch/columns_helper.rb, line 7 def initialize(model,batch) @hash = update_columns_hash(model,batch.updates) @hash.merge!(generate_columns_hash(model,batch.find_or_creates)) end
Public Instance Methods
each(&block)
click to toggle source
# File lib/rapids/batch/columns_helper.rb, line 12 def each(&block) internal_each(@hash,[],&block) end
Private Instance Methods
generate_columns_hash(model,criteria_hash,skip_columns = [])
click to toggle source
# File lib/rapids/batch/columns_helper.rb, line 27 def generate_columns_hash(model,criteria_hash,skip_columns = []) hash = {} skip_columns_next_time = {} batch_association_primary_keys = if criteria_hash.is_a?(Array) criteria_hash.map do |find_or_create| name = find_or_create.name if model.reflections[name] if model.reflections[name].collection? skip_columns_next_time[name] = [model.reflections[name].primary_key_name] nil else model.reflections[name].primary_key_name end end end.compact else [] end columns(model).reject{|c|batch_association_primary_keys.include?(c.name) || skip_columns.include?(c.name)}.each do |column| hash[column.name.to_sym] = column end if criteria_hash.is_a?(Array) criteria_hash.each do |find_or_create| name,criteria_array = find_or_create.name,find_or_create.find_columns new_model = if criteria_array.is_a?(Array) if name.is_a?(String) && Kernel.const_get(name) Kernel.const_get(name) elsif model.reflections[name] model.reflections[name].klass end end if new_model hash[name] = criteria_array.inject({}) do |memo,criteria| if criteria.is_a?(Hash) new_find_or_creates = criteria.map do |n,c| FindOrCreate.new(n,c,[]) end memo.merge(generate_columns_hash(new_model,new_find_or_creates,skip_columns_next_time[name] || [])) else memo.merge(generate_columns_hash(new_model,[],skip_columns_next_time[name] || [])) end end end end end hash end
internal_each(columns_hash,hash_path) { |column_or_hash,hash_path| ... }
click to toggle source
# File lib/rapids/batch/columns_helper.rb, line 17 def internal_each(columns_hash,hash_path,&block) columns_hash.sort{|a,b| a[0].to_s <=> b[0].to_s}.each do |key,column_or_hash| if column_or_hash.is_a?(Hash) internal_each(column_or_hash,hash_path + [key],&block) else yield(column_or_hash,hash_path) end end end
update_columns_hash(model,updates)
click to toggle source
# File lib/rapids/batch/columns_helper.rb, line 81 def update_columns_hash(model,updates) updates.inject({}) do |hash,update| if model.reflections[update.name] hash[{:type => :update, :name => update.name}] = generate_columns_hash(model.reflections[update.name].klass,[]) hash else hash end end end