Class: Strongbolt::CapabilitiesController

Inherits:
StrongboltController show all
Defined in:
app/controllers/strongbolt/capabilities_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/strongbolt/capabilities_controller.rb', line 11

def create
  @capability = Capability.where(capability_params).first_or_create

  # If we have a role id, we add the capability to the role
  if params[:role_id].present?
    @role = Role.find params[:role_id]
    @role.capabilities << @capability

    respond_to do |format|
      format.html { redirect_to role_path(@role) }
      format.json { head :ok }
    end
  else
    redirect_to capabilities_path
  end
rescue ActionController::ParameterMissing => e
  flash[:danger] = "Permission could not be created: ERROR #{e}"
  redirect_to capabilities_path
end

#destroyObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/strongbolt/capabilities_controller.rb', line 31

def destroy
  # If we're passed a role id
  if params[:role_id].present?
    @role = Role.find params[:role_id]

    conditions = if params[:id].present?
                   { id: params[:id] }
                 else
                   capability_params
                 end

    @capability = @role.capabilities.find_by(conditions)
    @role.capabilities.delete @capability

    respond_to do |format|
      format.html { redirect_to role_path(@role) }
      format.json { head :ok }
    end
  else
    @capability = Capability.find params[:id]
    @capability.destroy

    redirect_to capabilities_path
  end
rescue ActiveRecord::DeleteRestrictionError
  flash[:danger] = 'Permission has roles using it, delete relationships before deleting it'

  redirect_to capability_path(@capability)
end

#indexObject



3
4
5
# File 'app/controllers/strongbolt/capabilities_controller.rb', line 3

def index
  @capabilities = Capability.all
end

#showObject



7
8
9
# File 'app/controllers/strongbolt/capabilities_controller.rb', line 7

def show
  @capability = Capability.find params[:id]
end