Class: AdminSetting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/admin_setting.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_entity(name, entity, group = 'General', display_name = name.titleize) ⇒ Object



16
17
18
19
20
21
# File 'app/models/admin_setting.rb', line 16

def self.create_from_entity(name, entity, group='General', display_name=name.titleize)
  self.create(name: name, display_name: display_name,
              entity_type: entity.class.name,
              entity_id: entity.id,
              group: group)
end

.group_settings(group) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/models/admin_setting.rb', line 49

def self.group_settings(group)
  group_settings = AdminSetting.where(group: group)
  settings_hash = Hash.new
  group_settings.each do |setting|
    entity_value = setting.entity.entity_value
    settings_hash[setting.name.to_sym] = entity_value
  end
  settings_hash
end

.initialize_group(group, fields) ⇒ Object

convenience method for initializing a settings group with empty values fields should be a hash { <field_type(string))> : <field_names(string_array)> } Only fields (model classes) having a name/value properties are supported (Text, ShortText etc.) returns a hash of type { <field_name> : nil }



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/admin_setting.rb', line 36

def self.initialize_group(group, fields)
  group_settings = Hash.new
  fields.each do |field_type, field_names|
    field_names.each do |field_name|
      field_class = eval field_type
      field = field_class.create(name: field_name, value: nil)
      group_settings[field_name] = nil
      AdminSetting.create_from_entity(field_name, field, group, field_name.humanize)
    end
  end
  group_settings
end

.update_group_settings(group, settings_hash) ⇒ Object



59
60
61
62
63
64
# File 'app/models/admin_setting.rb', line 59

def self.update_group_settings(group, settings_hash)
  settings_hash.each do |setting_name, setting_value|
    setting = AdminSetting.find_by_group_and_name(group, setting_name)
    setting.entity.entity_value = setting_value
  end
end

.value_of(setting_name) ⇒ Object

finds a setting by name and returns the value of the setting’s entity. The entity should implement a entity_value method if no setting exists for the name then returns nil



25
26
27
28
29
30
# File 'app/models/admin_setting.rb', line 25

def self.value_of(setting_name)
  setting = AdminSetting.find_by_name(setting_name)
  if setting && setting.entity
    setting.entity.entity_value
  end
end

Instance Method Details

#entityObject



11
12
13
14
# File 'app/models/admin_setting.rb', line 11

def entity
  type_class = eval self.entity_type
  type_class.find self.entity_id
end