Class: PyrRules::EventConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
app/rules/pyr_rules/event_config_loader.rb

Class Method Summary collapse

Class Method Details

.add_controller_eventsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/rules/pyr_rules/event_config_loader.rb', line 57

def self.add_controller_events
  results = {}
  r=Rails.application.routes.routes.to_a.dup
  i=ActionDispatch::Routing::RoutesInspector.new r
  m=i.send(:collect_routes,r)
  events = m.map do |a| 
    p=a[:reqs].split("#")
    class_name = "#{p[0].camelcase}Controller"
    c = (results[class_name] ||= {context:{"system" => :messaging_user, "actor" => :user}, actions:[]})
    if p[1]
      action = p[1].split(" ").first    # it might have extra routing info on the end (subdomain, format, etc...) - we don't use this yet
      c[:actions] << action unless c[:actions].include? action
    end
  end
  results
end

.events_dictionary(refresh = false) ⇒ Object



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
51
52
53
54
55
# File 'app/rules/pyr_rules/event_config_loader.rb', line 23

def self.events_dictionary(refresh = false)
  return @@events_dictionary if @@events_dictionary
  # Force load of models and controllers when eager class loading isn't enabled by default
  # http://stackoverflow.com/questions/516579/is-there-a-way-to-get-a-collection-of-all-the-models-in-your-rails-app      
  puts "DEBUG[icentris-rules] Building Events Dictionary"
  start_time = Time.new
  if PyrRules.active_record
    ActiveRecord::Base.send :include, PyrRules::ModelEventEmitter
    model_classes = ActiveRecord::Base.connection.tables.collect{|t| t.classify.constantize rescue nil }.compact
    model_classes.each do |mc| 
      puts "PyrRules::EventConfigLoader loading #{mc.to_s}"
      mc.class_eval {}
    end
  end
  
  if Rails.env.development? 
      PyrRules.development_model_paths.each do |path|
      puts "PyrRules::EventConfigLoader - scanning models via #{path}"
      Dir.glob(path).sort.each do |entry|
        puts "   -  Loading model: #{entry}"
        require_dependency "#{entry}"
      end
    end
  end

  puts "\n\nPyrRules::ModelEventEmitter.registered_classes = #{PyrRules::ModelEventEmitter.registered_classes}\n\n"
  @@events_dictionary = {
    model_events: add_model_events({},PyrRules::ModelEventEmitter.registered_classes),
    controller_events: add_controller_events
  }
  puts "DEBUG[icentris-rules] Events Dictionary Built in #{Time.new - start_time} seconds\n\n\n"
  @@events_dictionary
end

.events_list(refresh = false) ⇒ Object

A simple list containing every Event known to the system…



12
13
14
15
16
17
18
19
20
# File 'app/rules/pyr_rules/event_config_loader.rb', line 12

def self.events_list(refresh = false)
  PyrRules::EventConfigLoader.events_dictionary(refresh).map do |et,et_v| 
    et_v.map do |k,v| 
      v[:actions].map do |a| 
        "#{k}::#{a}"
      end
    end
  end.flatten
end

.reload_events_dictionaryObject



5
6
7
8
9
# File 'app/rules/pyr_rules/event_config_loader.rb', line 5

def self.reload_events_dictionary
  puts "Reloading EventsDictionary"
  @@events_dictionary = nil
  events_list(true)
end