Module: Choices::Rails

Defined in:
lib/choices/rails.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/choices/rails.rb', line 5

def self.included(base)
  base.class_eval do
    def initialize_with_choices(*args, &block)
      initialize_without_choices(*args, &block)
      @choices = Hashie::Mash.new
    end
    
    alias :initialize_without_choices :initialize
    alias :initialize :initialize_with_choices
  end
end

Instance Method Details

#from_file(name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/choices/rails.rb', line 17

def from_file(name)
  root = self.respond_to?(:root) ? self.root : Rails.root
  file = root + 'config' + name
  
  settings = Choices.load_settings(file, Rails.respond_to?(:env) ? Rails.env : RAILS_ENV)
  @choices.update settings
  
  settings.each do |key, value|
    old_value = self.respond_to?(key) ? self.send(key) : nil

    if "Rails::OrderedOptions" == old_value.class.name
      # convert from Array to a real Hash
      old_value = old_value.inject({}) {|h,(k,v)| h[k]=v; h }
    end

    if Hash === value and Hash === old_value
      # don't overwrite existing Hash values; deep update them
      value = Hashie::Mash.new(old_value).update value
    end

    self.send("#{key}=", value)
  end
end