Module: ActiveRecord::Pickin::Relation

Defined in:
lib/activerecord-pickin/relation.rb

Instance Method Summary collapse

Instance Method Details

#convert_element(column, override) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/activerecord-pickin/relation.rb', line 44

def convert_element column, override
  if override.keys.include?(column)
    if override[column].class == Symbol
      @klass.connection.quote_column_name override[column]
    else
      "'#{override[column]}'"
    end
  else
    @klass.connection.quote_column_name column.to_s
  end
end

#get_select_column(target, override, ignore) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/activerecord-pickin/relation.rb', line 35

def get_select_column target, override, ignore
  target.map do |c|
    unless c.class == Symbol
      c = c.name.to_sym
    end
    convert_element(c, override) unless ignore.include? c
  end
end

#pickin(into_class, override = {}, ignore = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/activerecord-pickin/relation.rb', line 5

def pickin into_class, override = {}, ignore = []
  insert_arel = Arel::InsertManager.new(arel.engine)
  insert_arel.into into_class.table_name

  if self.select_values.empty?
    columns = into_class.columns
  else
    columns = self.select_values
  end

  insert_arel.ast.columns = columns.map do |c|
    unless c.class == Symbol
      c = c.name
    end
    Arel::Attributes::Attribute.new(insert_arel, c)
  end
  insert_sql = insert_arel.to_sql

  # Remove insert quote for only mysql adapters
  if ['MySql', 'Mysql2'].include? @klass.connection.adapter_name
    insert_sql = insert_arel.to_sql.delete!('`').gsub!('\'', '`')
  end

  select_column = self.get_select_column columns, override, ignore
  self.select_values = []
  select_arel = self.select(select_column)

  @klass.connection.execute "#{insert_sql} #{select_arel.to_sql}"
end