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



87
88
89
90
91
92
93
94
# File 'lib/migratrix/extractions/active_record.rb', line 87

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

#handle_includes(source, clause) ⇒ Object



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

def handle_includes(source, clause)
  source.includes(clause)
end

#handle_joins(source, clause) ⇒ Object



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

def handle_joins(source, clause)
  source.joins(clause)
end

#handle_limit(source, clause) ⇒ Object



41
42
43
# File 'lib/migratrix/extractions/active_record.rb', line 41

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

#handle_offset(source, clause) ⇒ Object



45
46
47
# File 'lib/migratrix/extractions/active_record.rb', line 45

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

#handle_order(source, clause) ⇒ Object



49
50
51
# File 'lib/migratrix/extractions/active_record.rb', line 49

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

#handle_where(source, clause) ⇒ Object



37
38
39
# File 'lib/migratrix/extractions/active_record.rb', line 37

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
# File 'lib/migratrix/extractions/active_record.rb', line 16

def obtain_source(source, options={})
  raise ExtractionSourceUndefined unless source
  source
end

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



21
22
23
24
25
26
27
# File 'lib/migratrix/extractions/active_record.rb', line 21

def process_source(source, options={})
  options = @options.merge(options)
  source = super source, options
  source = handle_joins(source, options[:joins]) if options[:joins]
  source = handle_includes(source, options[:includes]) if options[:includes]
  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_sql(source = nil) ⇒ Object

Constructs the query

TODO: A bit of a POLS violation here. Let’s say you define a migration class that has an extraction, then instantiate that migration with m = MyMigration.new(where: ‘id=42’). You might expect to be abe to call m.extractions.to_sql and get the query, but the reality is that the extraction hasn’t actually seen the migration’s options yet. They are passed in to extract, but not to to_sql. Not sure how to resolve this cleanly. Some options: 1. have Components know who their Migration is and ask it for its options (blegh); 2. pass in options here to to_sql (also blegh, because now end-users have to know they need to pass in the migration’s options to to_sql); 3. have a proxy method on Migration that essentially works just like extract, but returns the query instead of the results. I dislike this mechanism the least but it’s still only applicable to certain subclasses of Extraction so I hesitate to clutter Migration’s API; 4. change Migration#extractions (et al) so it settles its options with the component before returning it. This seems like it would appear the cleanest to the end user but also seems like it would be excessively magical and a weird source of bugs–eg if you try to access migration.loads and it crashes because of an invalid option in loads.



77
78
79
80
81
82
83
84
85
# File 'lib/migratrix/extractions/active_record.rb', line 77

def to_sql(source=nil)
  source ||= @source
  source = process_source(obtain_source(source))
  if source.respond_to? :to_sql
    source.to_sql
  else
    handle_where(source, 1).to_sql
  end
end