Module: Golden::Setting::Action::ClassMethods

Defined in:
lib/golden/setting/action.rb

Instance Method Summary collapse

Instance Method Details

#add(group, name, value, field_type = nil, field_values = nil) ⇒ Object

Setting.add ‘theme’, ‘name’, ‘golden’, :select, %w(golden white)



10
11
12
13
14
15
16
17
18
19
# File 'lib/golden/setting/action.rb', line 10

def add group, name, value, field_type = nil, field_values = nil
  object(name) ||
  new_object(name).tap do |s|
    s.group = group
    s.value = value
    s.field_type = field_type if field_type
    s.field_values = field_values if field_values
    s.save!
  end
end

#destroy(name) ⇒ Object



21
22
23
24
# File 'lib/golden/setting/action.rb', line 21

def destroy name
  raise Error::SettingNotFound, name unless self[name]
  object(name).destroy
end

#list(starting_with = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/golden/setting/action.rb', line 31

def list starting_with = nil
  result = {}
  records = select_object
  if starting_with
    records = records.where('name LIKE ?', "#{starting_with}%")
  end
  records.each do |record|
    result[record.name] = record.value
  end
  result.with_indifferent_access
end

#merge!(name, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/golden/setting/action.rb', line 43

def merge! name, value
  raise Error::SettingNotFound, name unless self[name]
  raise Error::SettingNotHash, value unless value.is_a? Hash

  old_value = self[name] || {}
  raise Error::SettingNotHash, old_value unless old_value.is_a? Hash

  new_value = old_value.merge(value)
  self[name] = new_value if new_value != old_value

  new_value
end

#select_objectObject



26
27
28
29
# File 'lib/golden/setting/action.rb', line 26

def select_object
  raise Error::SettingNoMethod, :select_object unless defined? super
  super
end