Skip to content

Commit

Permalink
allow editing of extdirect.yml wihtout reloading rails app
Browse files Browse the repository at this point in the history
  • Loading branch information
skeller1 committed May 12, 2013
1 parent ad4f600 commit 3e23d08
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
9 changes: 4 additions & 5 deletions lib/extr/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ class Config
cattr_accessor :controller_path

class << self
def controller_config
@@controller_config ||= Hash.new { |hash, key| hash[key] = {} }
end

def controller_path
@@controller_path ||= {}
def initialize!
@@controller_config = Hash.new { |hash, key| hash[key] = {} }
@@controller_path = {}
end

end
end
end
Expand Down
48 changes: 29 additions & 19 deletions lib/extr/engine.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
module Extr
class Engine < Rails::Engine

initializer "read_ext_config_from_yaml" do |app|
initializer "allow reloading of extdirect.yml file" do |app|
app.config.reload_classes_only_on_change = false if Rails.env.development?
end

config.to_prepare do
Rails.logger.info("Reload config from extdirect.yml file")
Config.initialize!
yaml_path = Rails.root.join("config", "extdirect.yml")
if File.exists? yaml_path
YAML.load_file(yaml_path).each do |ns, klasses|
klasses.each do |klass,options|
action = options["name"] || klass.gsub("_","")
Config.controller_path[action]=klass.gsub("_","::").to_s
Config.controller_config[ns][action]= [] if Config.controller_config[ns][action].nil?
keys = ["methods","formHandler"]

(options.keys & keys).each do |key|
unless options[key].nil?
options[key].stringify_keys!.each do |mtd, mcfg|
method_hash = mcfg.is_a?(Hash) ? {'name' => mtd}.merge!(mcfg) : {'name' => mtd, 'len' => mcfg || 1}
method_hash[key]= true if key == "formHandler"
Config.controller_config[ns][action] << method_hash
end
end
#END KEY LOOP
begin
YAML.load_file(yaml_path).each do |ns, klasses|
klasses.each do |klass,options|
action = options["name"] || klass.gsub("_","")
Config.controller_path[action]=klass.gsub("_","::").to_s
Config.controller_config[ns][action]= [] if Config.controller_config[ns][action].nil?
keys = ["methods","formHandler"]

(options.keys & keys).each do |key|
unless options[key].nil?
options[key].stringify_keys!.each do |mtd, mcfg|
method_hash = mcfg.is_a?(Hash) ? {'name' => mtd}.merge!(mcfg) : {'name' => mtd, 'len' => mcfg || 1}
method_hash[key]= true if key == "formHandler"
Config.controller_config[ns][action] << method_hash
end
end
#END KEY LOOP
end

end
end
end
rescue
Rails.logger.debug("Check your extdirect.yml file")
end

#END IF FILE
end
end


end
end
Expand Down

0 comments on commit 3e23d08

Please sign in to comment.