Class: API::V1::GroupsController

Inherits:
APIController show all
Defined in:
app/controllers/faalis/api/v1/groups_controller.rb

Instance Method Summary collapse

Methods inherited from Faalis::APIController

#set_csrf_cookie_for_ng

Methods inherited from Faalis::ApplicationController

#set_locale

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/faalis/api/v1/groups_controller.rb', line 15

def create
  authorize! :create, Faalis::Group

  permissions = [];

  (params[:permissions] || []).each do |perm_string|
    perm, model = perm_string.split "|"
    permission = Permission.find_or_create_by_model_and_permission_type(model, perm)
    permissions << permission
  end

  @group = Group.new({
                       name: params[:name],
                       permissions: permissions,
                     })
  if @group.save
    respond_with(@group)
  else
    respond_to do |format|
      format.json { render :json => {:fields => @group.errors}, :status => :unprocessable_entity }
    end
  end
end

#destroyObject



67
68
69
70
71
72
# File 'app/controllers/faalis/api/v1/groups_controller.rb', line 67

def destroy
  ids = params[:id].split(",")
  @groups = Group.where(:id => ids)
  authorize! :destory, @groups
  @groups.destroy_all
end

#indexObject

GET /api/v1/groups



9
10
11
12
13
# File 'app/controllers/faalis/api/v1/groups_controller.rb', line 9

def index
  @groups = Group.includes(:permissions).to_a
  authorize! :read, Faalis::Group
  respond_with(@groups)
end

#showObject



39
40
41
42
43
# File 'app/controllers/faalis/api/v1/groups_controller.rb', line 39

def show
  @group = Group.find(params[:id])
  authorize! :read, @group
  respond_with(@group)
end

#updateObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/faalis/api/v1/groups_controller.rb', line 45

def update

  @group = Group.find(params[:id])
  authorize! :update, @group

  permissions = [];
  (params[:permissions] || []).each do |perm_string|
    perm, model = perm_string.split "|"
    permission = Permission.find_or_create_by_model_and_permission_type(model, perm)
    permissions << permission
  end

  if @group.update(:name => params[:name],
                :permissions => permissions)
    respond_with(@group)
  else
    respond_to do |format|
      format.json { render :json => {:fields => @group.errors}, :status => :unprocessable_entity }
    end
  end
end