Class: API::V1::UsersController

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

Instance Method Summary collapse

Methods inherited from Faalis::APIController

allow_query_on, #allowed_fields, #authenticate_filter, #load_resource_by_query, #set_csrf_cookie_for_ng

Methods inherited from Faalis::ApplicationController

#set_locale

Instance Method Details

#createObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/faalis/api/v1/users_controller.rb', line 51

def create
  authorize! :create, Faalis::User

  @user = User.new({
                     first_name: params[:first_name],
                     last_name: params[:last_name],
                     email: params[:email],
                     password: params[:password],
                   })

  if params.include? :groups
    group = Group.find(params[:groups]) || nil
    @user.groups = group
  end

  if @user.save
    respond_with(@user)
  else
    respond_to do |format|
      format.json { render :json => {:fields => @user.errors}, :status => :unprocessable_entity }
    end
  end
end

#destroyObject



18
19
20
21
22
23
# File 'app/controllers/faalis/api/v1/users_controller.rb', line 18

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

#indexObject



6
7
8
9
10
# File 'app/controllers/faalis/api/v1/users_controller.rb', line 6

def index
  @users = User.joins(:groups).all
  #authorize! :read, @users
  respond_with(@users)
end

#showObject



12
13
14
15
16
# File 'app/controllers/faalis/api/v1/users_controller.rb', line 12

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

#updateObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/faalis/api/v1/users_controller.rb', line 25

def update
  @user = User.find(params[:id])
  authorize! :update, @user
  user_fields = {
    :first_name => params[:first_name],
    :last_name => params[:last_name],
    :email => params[:email],
  }

  if params.include? :password and params[:password]
    user_fields[:password] =  params[:password]
  end

  if params.include? :groups and params[:groups]
    user_fields[:groups] =  Group.find(params[:groups]) || nil
  end

  if @user.update(user_fields)
    respond_with(@user)
  else
    respond_to do |format|
      format.json { render :json => {:fields => @user.errors}, :status => :unprocessable_entity }
    end
  end
end