Class: Caboose::SettingsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#before_actionObject



5
6
7
# File 'app/controllers/caboose/settings_controller.rb', line 5

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end

#createObject

POST /admin/settings



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/caboose/settings_controller.rb', line 39

def create
  return if !user_is_allowed('settings', 'add')
  
  resp = StdClass.new({
      'error' => nil,
      'redirect' => nil
  })
  
  setting = Setting.new()
  setting.name  = params[:name]
  setting.value = params[:value]
  
  if (setting.name.strip.length == 0)
    resp.error = "The setting name is required."
  else      
    setting.save
    resp.redirect = "/admin/settings/#{setting.id}/edit"
  end
  render json: resp
end

#destroyObject

DELETE /admin/settings/1



80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/caboose/settings_controller.rb', line 80

def destroy
  return if !user_is_allowed('settings', 'delete')
  setting = Setting.find(params[:id])
  setting.destroy
  
  resp = StdClass.new({
    'redirect' => '/admin/settings'
  })
  render json: resp
end

#editObject

GET /admin/settings/1/edit



33
34
35
36
# File 'app/controllers/caboose/settings_controller.rb', line 33

def edit
  return if !user_is_allowed('settings', 'edit')
  @setting = Setting.find(params[:id])
end

#indexObject

GET /admin/settings



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

def index
  return if !user_is_allowed('settings', 'view')
  
  @gen = PageBarGenerator.new(params, {
		  'name'        => nil,
		  'value'       => nil
		},{
		  'model'       => 'Caboose::Setting',
	    'sort'			  => 'name',
		  'desc'			  => false,
		  'base_url'		=> '/admin/settings',
		  'use_url_params' => false
	})
	@settings = @gen.items    	
end

#newObject

GET /admin/settings/new



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

def new
  return if !user_is_allowed('settings', 'add')
  @setting = Setting.new
end

#optionsObject

GET /admin/settings/options



92
93
94
95
96
97
# File 'app/controllers/caboose/settings_controller.rb', line 92

def options
  return if !user_is_allowed('settings', 'view')
  settings = Setting.reorder('name').all
  options = settings.collect { |s| { 'value' => s.id, 'text' => s.name }}
  render json: options
end

#updateObject

PUT /admin/settings/1



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

def update
  return if !user_is_allowed('settings', 'edit')

  resp = StdClass.new     
  setting = Setting.find(params[:id])

  save = true
  params.each do |name,value|
    case name
	  	when "name"  then setting.name  = value
	  	when "value" then setting.value = value
	  end
	end
	
	resp.success = save && setting.save
	render :json => resp
end