Module: ConfigFindAllAsTree

Defined in:
lib/config_find_all_as_tree.rb

Instance Method Summary collapse

Instance Method Details

#find_all_as_treeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/config_find_all_as_tree.rb', line 3

def find_all_as_tree
  returning(ActiveSupport::OrderedHash.new) do |result|
    
    db_key = (ActiveRecord::Base.connection.adapter_name.downcase == 'mysql' ? '`key`' : 'key')
    
    # For all settings
    find(:all, :order => db_key).each do |setting|
      
      # Split the setting path into an array
      path = setting.key.split('.')
      
      # Set the current level to the root of the hash
      current_level = result
      
      # iterate through all path levels
      path.each do |path_element|
        if path_element.equal?(path.last)
          # We are at the end of the path, so set the settting object as the value
          current_level[path_element] = setting
          
        else
          # Not at the end yet, so first make sure that there is a hash for this key
          current_level[path_element] ||= ActiveSupport::OrderedHash.new
          
          # Reset the curent level to this hash object for this key
          current_level = current_level[path_element]
        end
      end # if
    end # each
  end # returning
end