Class: LicensesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/licenses_controller.rb', line 14

def create
  @license = License.new license_params
  if @license.save
    if @license.default?
      License.where(default: true).where.not(id: @license.id).update_all(default: false)
    end
    AuditLog.admin_audit(event_type: 'license_create', related: @license, user: current_user,
                         comment: "<<License #{@license.attributes_print}>>")
    redirect_to licenses_path
  else
    render :new, status: :bad_request
  end
end

#editObject



28
# File 'app/controllers/licenses_controller.rb', line 28

def edit; end

#indexObject



6
7
8
# File 'app/controllers/licenses_controller.rb', line 6

def index
  @licenses = License.all.order(enabled: :desc, default: :desc, name: :asc)
end

#newObject



10
11
12
# File 'app/controllers/licenses_controller.rb', line 10

def new
  @license = License.new
end

#toggleObject



44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/licenses_controller.rb', line 44

def toggle
  before = @license.enabled?
  if @license.enabled? && (@license.default? || Category.where(license_id: @license.id).any?)
    flash[:danger] = "You can't disable a license that's currently in use."
  else
    @license.update(enabled: !@license.enabled)
    AuditLog.admin_audit(event_type: 'license_toggle', related: @license, user: current_user,
                         comment: "enabled from #{before}\nto #{@license.enabled?}")
  end
  redirect_to licenses_path
end

#updateObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/licenses_controller.rb', line 30

def update
  before = @license.attributes_print
  if @license.update license_params
    if @license.default?
      License.where(default: true).where.not(id: @license.id).update_all(default: false)
    end
    AuditLog.admin_audit(event_type: 'license_update', related: @license, user: current_user,
                         comment: "from <<License #{before}>>\nto <<License #{@license.attributes_print}>>")
    redirect_to licenses_path
  else
    render :edit, status: :bad_request
  end
end