Class: Buttafly::Legend

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

Class Method Summary collapse

Class Method Details

.ancestors_hash(model) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/buttafly/legend.rb', line 28

def self.ancestors_hash(model)
  ancestors = self.get_dependencies(model)
  level_one = []
  ancestors[:parents].each_with_index do |p,i|
    level_one << {p => self.get_dependencies(p) }
    ancestors[:parents] = level_one 
    level_two = []
    ancestors[:parents][i][p][:parents].each_with_index do |g, n|
      level_two << { g => self.get_dependencies(g) }
      ancestors[:parents][i][p][:parents] = level_two
      level_three = []
      ancestors[:parents][i][p][:parents][n][g][:parents].each do |ggp|
        level_three << { ggp => self.get_dependencies(ggp) }
        ancestors[:parents][i][p][:parents][n][g][:parents] = level_three
      end
    end
  end
  ancestors
end

.get_dependencies(model) ⇒ Object



75
76
77
78
79
80
81
# File 'app/models/buttafly/legend.rb', line 75

def self.get_dependencies(model)
  {
    :attrs => self.get_target_keys(model),
    :parents => self.get_parent_models(model)
  }

end

.get_origin_keys(model, id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/buttafly/legend.rb', line 12

def self.get_origin_keys(model, id)
  json_columns = []
  hash_of_keys = {}
  model.columns_hash.each do |column_hash|
    if column_hash[1].type == :json
      json_columns << column_hash[1].name
    end
  end
  json_columns.each do |column|
    file = model.find(id)
    origin_keys = eval("file.#{column}")
    hash_of_keys[column] = origin_keys.first.keys
  end
  hash_of_keys
end

.get_parent_models(model) ⇒ Object



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

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

.get_target_keys(model) ⇒ Object



48
49
50
51
# File 'app/models/buttafly/legend.rb', line 48

def self.get_target_keys(model)
  model.to_s.classify.constantize.column_names - @ignored_columns
  # must also remove any column of _id, either pattern matching or by removing any foreign keys from 
end

.originable_modelsObject



66
67
68
69
70
71
72
73
# File 'app/models/buttafly/legend.rb', line 66

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



57
58
59
60
61
62
63
64
# File 'app/models/buttafly/legend.rb', line 57

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