Module: Cockpit::ClassMethods

Defined in:
lib/cockpit/cockpit.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_configurable(*args, &block) ⇒ Object Also known as: configurable, settings

can be “unique_by_key” settings :text do

...

settings :social do

...


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cockpit/cockpit.rb', line 13

def acts_as_configurable(*args, &block)
  options = args.extract_options!
  settings_name = (args.shift || "settings").to_s
  clazz_name = self.to_s.downcase.split("::").last

  class_inheritable_accessor settings_name
  has_many settings_name, :class_name => "Setting", :as => :configurable
  
  Settings { send(clazz_name, &block) }
  
  self.send("#{settings_name}=", ::Settings.for(clazz_name))

  define_method settings_name do |*value|
    unless @settings
      @settings = self.class.send(settings_name).dup
      @settings.configurable = self
    end
    
    unless value.empty?
      @settings[value.first]
    else
      @settings
    end
    # model-dependent settings.
    # requires refactoring the Settings module
    # so none of it uses class methods...
  end
  
end

#acts_as_settableObject



45
46
47
# File 'lib/cockpit/cockpit.rb', line 45

def acts_as_settable
  belongs_to :configurable, :polymorphic => true
end