Class: RailsInfo::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_info/model.rb

Instance Method Summary collapse

Instance Method Details

#associations(key_constant, options = { parent: ''}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails_info/model.rb', line 52

def associations(key_constant, options = { parent: ''})
  options.assert_valid_keys(:parent)
  
  key_constant = key_constant(key_constant, options[:parent]) if key_constant.is_a?(String)
   
  association_collection = []
  columns = columns_for(key_constant)
  columns.any? and association_collection << "belongs_to: #{columns.join(', ')}"    
  super_active_record_ancestor_column = "#{get_super_active_record_ancestor(key_constant).split('::').last.underscore}_id"
  has_many = {}
  
  if models_with_columns[super_active_record_ancestor_column]
    has_many[:standard] = models_with_columns[super_active_record_ancestor_column].uniq      
  else
    has_many[:alias] = {}
    
    models_with_columns.keys.select{|c| c.match("#{super_active_record_ancestor_column}")}.each do |column|
      has_many[:alias][column] = models_with_columns[column]
    end
    
    has_many[:alias].empty? and has_many = {}
  end
  
  has_many_polymorphs = {}
  
  if key_constant.respond_to?('reflections')
    key_constant.reflections.values.select{|r| [:has_one, :has_many].include?(r.macro) && r.options.has_key?(:as) }.each do |reflection|        
      association = reflection.options[:class_name] ? reflection.options[:class_name] : reflection.name.to_s.classify
      has_many_polymorphs[association] = reflection.options[:as].to_s
    end
  end
  
  !has_many_polymorphs.empty? and has_many[:polymorphic] = has_many_polymorphs
  
  !has_many.empty? and association_collection << "has_many: #{has_many.inspect}"
  
  association_collection.any? ? " (#{association_collection.join(';')})" : ""
end

#classesObject

TODO: Live reload of ruby code as maybe seen here with require_or_load path github.com/Tho85/sunspot/blob/f9f6f9666e6df1a0e92c0d8070f7007075f75071/sunspot_rails/lib/sunspot/rails/tasks.rb



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rails_info/model.rb', line 3

def classes
  return @classes if @classes
  
  #model_names = Dir.chdir(File.join(Rails.root, 'app', 'models')) { Dir["*.rb"] }
  #model_names = Dir.chdir(File.expand_path('../../../../../../../reqorder2/app/models', __FILE__)) { Dir["*.rb"] }
  @classes = []

  # Dir['app/models/**/*.*'].map{|f| f.gsub('app/models/', '')}
  Dir["#{Rails.root}/app/models/**/*.*"].each do |class_name|
    @classes << class_name.split('/').last.sub(/\.rb$/,'').camelize.constantize
  end
  
  #select { |k| k.ancestors.include?(ActiveRecord::Base) && k.connection.table_exists?(k.table_name) }
  @classes.select! { |c| c.respond_to?('table_name') }
  
  @classes
end

#columns_for(key_constant, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rails_info/model.rb', line 91

def columns_for(key_constant, options = {})
  options.assert_valid_keys(:parent)
  
  key_constant = key_constant(key_constant, options[:parent]) if key_constant.is_a?(String)
  
  columns = []
  
  begin
    columns = key_constant.columns.map(&:name).select{|c| c.match('_id') || c.match('type')}.sort
  rescue NoMethodError
    columns = []
  end
  
  columns
end

#listObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_info/model.rb', line 21

def list
  models = {}
  
  classes.each do |klass| 
    ar_ancestors = active_record_ancestors(klass)
    
    path_until_here = []
    
    klass_path = ar_ancestors.any? ? ar_ancestors.reverse : [] 
    
    if ar_ancestors.any?
      if ar_ancestors.first.match('::') && ar_ancestors.length == 1
        klass_path = (ar_ancestors.first + "::" + klass.to_s.split('::').last).split('::')
      elsif ar_ancestors.first.match('::')    
        raise 'unimplemented'
      else
        klass_path += klass.to_s.split('::') 
      end
    else
      klass_path += klass.to_s.split('::')   
    end   
         
    klass_path.each do |element|
      path_until_here << element           
      eval("models['" + path_until_here.join("']['") + "'] ||= {}")
    end
  end
  
  models
end