Class: Buttafly::Mapping

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/buttafly/mapping.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.originable_modelsObject



12
13
14
15
16
17
18
19
# File 'app/models/buttafly/mapping.rb', line 12

def self.originable_models      
  Rails.application.eager_load!
  models = ActiveRecord::Base.descendants.select do |c| 
    c.included_modules.include?(Originable)
  end
  model_names = models.map(&:name)
  model_names
end

.targetable_modelsObject



21
22
23
24
25
26
27
28
# File 'app/models/buttafly/mapping.rb', line 21

def self.targetable_models
  Rails.application.eager_load!
  models = ActiveRecord::Base.descendants.select do |c| 
    c.included_modules.include?(Targetable)
  end
  model_names = models.map(&:name)
  model_names
end

.targetable_order(targetable_model = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/buttafly/mapping.rb', line 39

def self.targetable_order(targetable_model=nil)
  dependency_hash = TsortableHash.new
  self.targetable_models.each do |m|
    dependency_hash[m.underscore.to_sym] = m.constantize.validators.map(&:attributes).flatten
  end
  sorted_dependencies = dependency_hash.tsort
  if targetable_model.nil?
    sorted_dependencies
  else
    sorted_dependencies[0..sorted_dependencies.index(targetable_model)]
  end
end

Instance Method Details

#get_origin_headersObject



52
53
54
55
# File 'app/models/buttafly/mapping.rb', line 52

def get_origin_headers
  data = CSV.read(self.originable.flat_file.path)
  data.first
end

#parent_modelsObject



57
58
59
60
# File 'app/models/buttafly/mapping.rb', line 57

def parent_models
  model = self.targetable_model.to_s.classify.constantize
  model.validators.map(&:attributes).flatten
end

#targetable_orderObject



30
31
32
33
34
35
36
37
# File 'app/models/buttafly/mapping.rb', line 30

def targetable_order
  dependency_hash = TsortableHash.new
  Mapping.targetable_models.each do |m|
    dependency_hash[m.underscore.to_sym] = m.constantize.validators.map(&:attributes).flatten
  end
  sorted_dependencies = dependency_hash.tsort
  sorted_dependencies[0..sorted_dependencies.index(self.targetable_model.underscore.to_sym)]
end