Class: UsersController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#check_maintenance_mode, #redirect_if_maintenance_mode

Instance Method Details

#createObject

POST /users POST /users.json



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/users_controller.rb', line 43

def create
  @user = User.new(user_params)
  authorize! :create, @user
  authorize! :update, (user_params[:role_id].present? ? Role.find(user_params[:role_id]) : Role)

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: I18n.translate('users.flash.create.success', users: @user.fullname) }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new, notice: I18n.translate('users.flash.create.fail') , status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /users/1 DELETE /users/1.json



78
79
80
81
82
83
84
85
86
# File 'app/controllers/users_controller.rb', line 78

def destroy
  authorize! :destroy, @user
  username=@user.fullname
  @user.destroy
  respond_to do |format|
    format.html { redirect_to users_url, notice:  I18n.translate('users.flash.destroy.success', users: username) }
    format.json { head :no_content }
  end
end

#editObject

GET /users/1/edit



36
37
38
39
# File 'app/controllers/users_controller.rb', line 36

def edit
  authorize! :update, @user
  @roles=Role.accessible_by(current_ability)
end

#indexObject

GET /users GET /users.json



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/users_controller.rb', line 8

def index
  @query= params[:query]
  @users = User.accessible_by(current_ability).search(@query).paginate(:page => params[:page])
  authorize! :update, @users
  respond_to do |format|
    format.html do
      if @users.count==1
        return redirect_to(user_path(@users.first.id))
      end
    end
    format.json
  end
end

#newObject

GET /users/new



29
30
31
32
33
# File 'app/controllers/users_controller.rb', line 29

def new
  @user = User.new
  authorize! :create, @user
  @roles=Role.accessible_by(current_ability)
end

#showObject

GET /users/1 GET /users/1.json



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

def show
  authorize! :read, @user
end

#sync_with_gramObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/users_controller.rb', line 88

def sync_with_gram
  authorize! :sync, @user
  respond_to do |format|
    if @user.syncable? && ( @user.next_sync_allowed_at <= Time.now)
      if @user.update_from_gram
        format.html { redirect_to @user, notice: I18n.translate('users.flash.sync.success', users: @user.fullname) }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { redirect_to @user, notice: I18n.translate('users.flash.sync.fail', users: @user.fullname)}
        format.json { render json: '{"error": "Problems occured during syncronization"}', status: :unprocessable_entity }
      end
    else
      format.html { redirect_to @user, notice: I18n.translate('users.flash.sync.too_soon', users: @user.fullname, eta: (@user.next_sync_allowed_at-Time.now).round)}
      format.json { render json: "{\"error\": \"Try again in #{(@user.next_sync_allowed_at-Time.now).round} seconds\"}", status: :unprocessable_entity }
    end
  end
end

#updateObject

PATCH/PUT /users/1 PATCH/PUT /users/1.json



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/users_controller.rb', line 61

def update
  authorize! :update, @user
  authorize! :update, (user_params[:role_id].present? ? Role.find(user_params[:role_id]) : Role)

  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: I18n.translate('users.flash.update.success', users: @user.fullname) }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render :edit, notice: I18n.translate('users.flash.update.fail', users: @user.fullname) , status: :unprocessable_entity}
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end