Class: Settr::SettingsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/settr/settings_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/settr/settings_controller.rb', line 8

def create
  @setting = SettrSetting.new(params[:settr_setting])
  if @setting.select?
    unless @setting.options.include?(@setting.value)
      flash[:error] = t('settr.setting.not_created_value_not_in_options')
      redirect_to settr_settings_path
      return
    end
  end
  
  if @setting.save 
    flash[:success] = t('settr.setting.created')
  else
    flash[:error] = t('settr.setting.not_created')
  end
  
  redirect_to settr_settings_path
end

#destroyObject



54
55
56
57
58
59
60
61
62
# File 'app/controllers/settr/settings_controller.rb', line 54

def destroy
  @setting = SettrSetting.find(params[:id])
  if @setting.destroy
    flash[:success] = t('settr.setting.deleted')
  else
    flash[:error] = t('settr.setting.not_deleted')
  end
  redirect_to settr_settings_path
end

#editObject



27
28
29
# File 'app/controllers/settr/settings_controller.rb', line 27

def edit
  @setting = SettrSetting.find(params[:id])
end

#indexObject



50
51
52
# File 'app/controllers/settr/settings_controller.rb', line 50

def index
  @settings = SettrSetting.order(:key)
end

#newObject



4
5
6
# File 'app/controllers/settr/settings_controller.rb', line 4

def new
  @setting = SettrSetting.new
end

#settingsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/settr/settings_controller.rb', line 64

def settings
  @settings = Settr.alterable
  
  if request.post?
    params[:settr].each do |k, v|
      setting = SettrSetting.find_by_key(k)
      setting.value = (setting.typ == 'boolean' ? (v == '1').to_s : v)
      setting.save!
    end      
    flash[:success] = t('settr.settings.saved')
    redirect_to settings_settr_settings_path
  end
end

#updateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/settr/settings_controller.rb', line 31

def update
  @setting = SettrSetting.find(params[:id])
  @new_setting = SettrSetting.new(params[:settr_setting])
  if @new_setting.select?
    unless @new_setting.options.include?(@setting.value)
      flash[:error] = t('settr.setting.not_updated_value_not_in_options')
      redirect_to settr_settings_path
      return
    end
  end
  
  if @setting.update_attributes(params[:settr_setting])
    flash[:success] = t('settr.setting.updated')
  else
    flash[:error] = t('settr.setting.not_updated')
  end
  redirect_to settr_settings_path
end