Class: Migratrix::Extractions::ActiveRecord

Inherits:
Extraction show all
Defined in:
lib/migratrix/extractions/active_record.rb

Overview

Extraction that expects to be pointed at an ActiveRecord class.

Instance Attribute Summary

Attributes inherited from Extraction

#name, #options, #source

Instance Method Summary collapse

Methods inherited from Extraction

#extract, #initialize

Constructor Details

This class inherits a constructor from Migratrix::Extractions::Extraction

Instance Method Details

#execute_extract(src, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/migratrix/extractions/active_record.rb', line 47

def execute_extract(src, options={})
  return src.all if options['fetchall']
  ret = if src.is_a?(::ActiveRecord::Relation)
          src
        else
          handle_where(src, 1)
        end
end

#handle_limit(source, clause) ⇒ Object



26
27
28
# File 'lib/migratrix/extractions/active_record.rb', line 26

def handle_limit(source, clause)
  source.limit(clause.to_i)
end

#handle_offset(source, clause) ⇒ Object



30
31
32
# File 'lib/migratrix/extractions/active_record.rb', line 30

def handle_offset(source, clause)
  source.offset(clause.to_i)
end

#handle_order(source, clause) ⇒ Object



34
35
36
# File 'lib/migratrix/extractions/active_record.rb', line 34

def handle_order(source, clause)
  source.order(clause)
end

#handle_where(source, clause) ⇒ Object



22
23
24
# File 'lib/migratrix/extractions/active_record.rb', line 22

def handle_where(source, clause)
  source.where(clause)
end

#is_ar?(source) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/migratrix/extractions/active_record.rb', line 12

def is_ar?(source)
  source.is_a?(Class) && source.ancestors.include?(::ActiveRecord::Base)
end

#obtain_source(source, options = {}) ⇒ Object



16
17
18
19
20
# File 'lib/migratrix/extractions/active_record.rb', line 16

def obtain_source(source, options={})
  raise ExtractionSourceUndefined unless source
  raise TypeError.new(":source is of type must be an ActiveRecord model class (must inherit from ActiveRecord::Base)") unless is_ar?(source)
  source
end

#source=(new_source) ⇒ Object

Raises:

  • (TypeError)


7
8
9
10
# File 'lib/migratrix/extractions/active_record.rb', line 7

def source=(new_source)
  raise TypeError.new(":source is of type must be an ActiveRecord model class (must inherit from ActiveRecord::Base)") unless is_ar?(new_source)
  @source = new_source
end

#to_query(source) ⇒ Object

Constructs the query



39
40
41
42
43
44
45
# File 'lib/migratrix/extractions/active_record.rb', line 39

def to_query(source)
  if source.is_a?(::ActiveRecord::Relation)
    source.to_sql
  else
    handle_where(source, 1).to_sql
  end
end