class HasMeta::DataMover

Attributes

abort[RW]
attribute[R]
key[R]
table[R]
type[R]

Public Class Methods

new(table, attribute, type, key) click to toggle source
# File lib/has_meta/data_mover.rb, line 3
def initialize table, attribute, type, key
  @table      = table
  @attribute  = attribute
  @key        = key
  @type       = type
  resolve_type!
end

Public Instance Methods

execute() click to toggle source
# File lib/has_meta/data_mover.rb, line 11
def execute
  
  insert          = Arel::Nodes::InsertStatement.new
  insert.relation = destination_table
  insert.columns  = destination_columns
  insert.values   = Arel::Nodes::SqlLiteral.new source_values
  # There seems to be a bug with earlier versions of Arel where multiple values aren't supported
  # insert.select   = source_select.where(source_conditions)
  
  ActiveRecord::Base.connection.execute insert.to_sql if migrateable?

  self
end

Private Instance Methods

destination_columns() click to toggle source
# File lib/has_meta/data_mover.rb, line 98
def destination_columns
  [
    destination_table[:meta_model_type],
    destination_table[:meta_model_id],
    destination_table[:key],
    destination_table[:"#{type}_value"],
    destination_table[:created_at],
  ]
end
destination_table() click to toggle source
# File lib/has_meta/data_mover.rb, line 94
def destination_table
  @destination_table ||= HasMeta::MetaData.arel_table
end
escape(value) click to toggle source
# File lib/has_meta/data_mover.rb, line 55
def escape value
  if ['text', 'date'].include? type.to_s
    "'#{value}'"
  else
    value
  end
end
format_values(row) click to toggle source
# File lib/has_meta/data_mover.rb, line 51
def format_values row
  "('#{table.classify.constantize.name}', #{row.id}, '#{key}', #{escape(row.send(:"#{attribute}"))}, #{timestamp}),"
end
migrateable?() click to toggle source
# File lib/has_meta/data_mover.rb, line 47
def migrateable?
  migrateable_source_values.present?
end
migrateable_source_values() click to toggle source
# File lib/has_meta/data_mover.rb, line 39
def migrateable_source_values
  if type.to_s == 'text'      
    source_model.where.not(:"#{attribute}" => [nil, ''])
  else
    source_model.where(source_table[:"#{attribute}"].not_eq(nil))
  end
end
resolve_type!() click to toggle source
# File lib/has_meta/data_mover.rb, line 30
def resolve_type!
  @type = 'text' if @type == 'string'
end
source_conditions() click to toggle source
# File lib/has_meta/data_mover.rb, line 90
def source_conditions
  source_table[:"#{attribute}"].not_in([nil, ''])
end
source_model() click to toggle source
# File lib/has_meta/data_mover.rb, line 72
def source_model
  table.classify.constantize
end
source_select() click to toggle source
# File lib/has_meta/data_mover.rb, line 80
def source_select
  source_table.project(
    Arel.sql("\'#{table.classify.constantize.name}\'"),   # :meta_model_type
    source_table[:id],                                    # :meta_model_id
    Arel.sql("\'#{key}\'"),                               # :key
    source_table[:"#{attribute}"],                        # :integer_value (ex.)
    Arel::Nodes::NamedFunction.new("NOW", [])             # :created_at
  )
end
source_table() click to toggle source
# File lib/has_meta/data_mover.rb, line 76
def source_table
  @source_table ||= Arel::Table.new(table) 
end
source_values() click to toggle source
# File lib/has_meta/data_mover.rb, line 34
def source_values
  migrateable_source_values
    .reduce(" VALUES ") {|acc, row| acc += format_values row}[0...-1]
end
timestamp() click to toggle source
# File lib/has_meta/data_mover.rb, line 63
def timestamp
  case ::ActiveRecord::Base.connection.adapter_name
  when 'Mysql2'
    'NOW()'
  when 'SQLite'
    "'#{Time.now}'"
  end
end