Class: SiteSettingsController

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

Overview

Web controller. Provides authenticated actions for use by administrators in controlling the site settings (which in turn control the operation and display of some aspects of the site).

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#access?(user, community_id) ⇒ Boolean

Checks if a given user has access to site settings on a given community

Parameters:

  • user (User)

    user to check access for

  • community_id (String, nil)

    id of the community to check access on

Returns:

  • (Boolean)


11
12
13
# File 'app/controllers/site_settings_controller.rb', line 11

def access?(user, community_id)
  community_id.present? || user.is_global_admin
end

#audit_update(user, before, after) ⇒ void

This method returns an undefined value.

Adds an audit log for a given site setting update event

Parameters:



42
43
44
45
46
47
# File 'app/controllers/site_settings_controller.rb', line 42

def audit_update(user, before, after)
  AuditLog.admin_audit(event_type: 'setting_update',
                       related: after,
                       user: user,
                       comment: "from <<SiteSetting #{before}>>\nto <<SiteSetting #{after.attributes_print}>>")
end

#clear_cache(setting, community_id) ⇒ Boolean

Deletes cache for a given site setting for a given community

Parameters:

  • setting (SiteSetting)

    site setting to clear cache for

  • community_id (String, nil)

    community id to clear cache for

Returns:

  • (Boolean)


53
54
55
# File 'app/controllers/site_settings_controller.rb', line 53

def clear_cache(setting, community_id)
  Rails.cache.delete("SiteSettings/#{community_id}/#{setting.name}", include_community: false)
end

#do_create(setting, community_id) ⇒ SiteSetting

Actually creates a given site setting

Parameters:

  • setting (SiteSetting)

    site setting to create

  • community_id (String, nil)

    community id to create a setting for

Returns:



61
62
63
64
65
66
67
68
# File 'app/controllers/site_settings_controller.rb', line 61

def do_create(setting, community_id)
  SiteSetting.create(name: setting.name,
                     community_id: community_id,
                     value: '',
                     value_type: setting.value_type,
                     category: setting.category,
                     description: setting.description)
end

#globalObject



23
24
25
26
# File 'app/controllers/site_settings_controller.rb', line 23

def global
  @settings = SiteSetting.where(community_id: nil).group_by(&:category).sort_by { |c, _ss| [c ? 0 : 1, c] }
  render :index
end

#indexObject



15
16
17
18
19
20
21
# File 'app/controllers/site_settings_controller.rb', line 15

def index
  # The weird argument to sort_by here sorts without throwing errors on nil values -
  # see https://stackoverflow.com/a/35539062/3160466. 0:1,c sorts nil last, to switch
  # round use 1:0,c
  @settings = SiteSetting.where(community_id: RequestContext.community_id).group_by(&:category)
                         .sort_by { |c, _ss| [c ? 0 : 1, c] }
end

#showObject



28
29
30
31
32
33
34
35
# File 'app/controllers/site_settings_controller.rb', line 28

def show
  @setting = if params[:community_id].present?
               SiteSetting.applied_setting(params[:name])
             else
               SiteSetting.global.where(name: params[:name]).priority_order.first
             end
  render json: @setting&.as_json&.merge(typed: @setting.typed)
end

#updateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/site_settings_controller.rb', line 70

def update
  unless access?(current_user, params[:community_id])
    not_found
    return
  end

  @setting = if params[:community_id].present?
               matches = SiteSetting.unscoped.where(community_id: RequestContext.community_id, name: params[:name])
               if matches.count.zero?
                 global = SiteSetting.unscoped.where(community_id: nil, name: params[:name]).first
                 do_create(global, RequestContext.community_id)
               else
                 matches.first
               end
             else
               SiteSetting.unscoped.where(community_id: nil, name: params[:name]).first
             end

  before = @setting.attributes_print

  @setting.update(setting_params)

  audit_update(current_user, before, @setting)

  if @setting.global?
    Community.all.each do |c|
      clear_cache(@setting, c.id)
    end
  else
    clear_cache(@setting, RequestContext.community_id)
  end

  render json: { status: 'OK', setting: @setting&.as_json&.merge(typed: @setting.typed) }
end