Class: Admin::UsersController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/users_controller.rb

Overview

Controller for managing users.

Instance Method Summary collapse

Instance Method Details

#assign_propertiesObject

Assigns properties to a user.



16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/admin/users_controller.rb', line 16

def assign_properties
  old_ids = params[:old_property_ids].to_s.split(" ")
  ids = params[:property_ids].reject(&:empty?) + old_ids
  ids = ids.uniq
  user = User.find(params[:user_id])
  ids.each do |id|
    user.user_properties.create(property_id: id)
  end
  redirect_to admin_user_path(user)
end

#assign_properties_formObject

Displays the form for assigning properties to a user.



9
10
11
12
13
# File 'app/controllers/admin/users_controller.rb', line 9

def assign_properties_form
  @user = User.find(params[:user_id])
  @properties = @user.properties
  render 'admin/users/assign_properties'
end

#createObject

POST /users POST /users.json Creates a new user.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/admin/users_controller.rb', line 84

def create
  @user = User.new(user_params)

  respond_to do |format|
    if @user.valid?
      @user.save
      format.html { redirect_to admin_users_path, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /users/1 DELETE /users/1.json Deletes an existing user.



117
118
119
120
121
122
123
# File 'app/controllers/admin/users_controller.rb', line 117

def destroy
  @user.destroy
  respond_to do |format|
    format.html { redirect_to admin_users_url, notice: 'User was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#detach_propertyObject

Detaches a property from a user.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/admin/users_controller.rb', line 28

def detach_property
  user_id = params[:user_id]
  property_id = params[:property_id]
  user = User.find_by_id(user_id)
  if user
    user_property = user.user_properties.find_by(property_id: property_id)
    if user_property
      user_property.destroy
    end
  end
  redirect_to admin_user_path(user)
end

#editObject

GET /users/1/edit Displays a form for editing a user.



79
# File 'app/controllers/admin/users_controller.rb', line 79

def edit; end

#indexObject

GET /users GET /users.json Displays a list of users.



54
55
56
57
58
59
60
61
62
# File 'app/controllers/admin/users_controller.rb', line 54

def index
  @users = User.search(params[:username], params[:user_type]).page params[:page]

  if request.xhr?
    render action: :index ,layout: false
  else
    render action: :index, layout: true
  end
end

#newObject

GET /users/new Displays a form for creating a new user.



73
74
75
# File 'app/controllers/admin/users_controller.rb', line 73

def new
  @user = User.new
end

#property_searchObject

Searches for properties based on property name.



42
43
44
45
46
47
48
49
# File 'app/controllers/admin/users_controller.rb', line 42

def property_search
  property_name = params[:property_name]

  @properties = Property.where('property_name ILIKE ?', "%#{property_name}%")
                        .where('system_id = ?',2)
                        .order(updated_at: :desc)
  render json: @properties
end

#showObject

GET /users/1 GET /users/1.json Displays the details of a specific user.



67
68
69
# File 'app/controllers/admin/users_controller.rb', line 67

def show
  @properties = @user.properties
end

#updateObject

PATCH/PUT /users/1 PATCH/PUT /users/1.json Updates an existing user.



102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/admin/users_controller.rb', line 102

def update
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to admin_users_path, notice: 'User was successfully updated.' }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render :edit }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end