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.



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

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])
  properties = {}
  ids = ids.join(',')
  properties['property_ids'] = ids
  user.properties = properties
  user.save!
  redirect_to admin_user_path(user)
end

#assign_properties_formObject

Displays the form for assigning properties to a user.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/admin/users_controller.rb', line 9

def assign_properties_form
  @user = User.find(params[:user_id])
  @properties = Property.where("properties->>'is_archived' = ?", 'No').collect { |p| [p.property_name, p.id] }
  if @user.properties
    @ids = @user.properties['property_ids'].to_s.split(',')

    @ids = @ids.map(&:to_i)
  else
    @ids = nil
  end

  if @ids
    @properties = Property.where(id: @ids)
  else
    @properties = []
  end
  render 'admin/users/assign_properties'
end

#createObject

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



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

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.



133
134
135
136
137
138
139
# File 'app/controllers/admin/users_controller.rb', line 133

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.



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

def detach_property
  user_id = params[:user_id]
  property_id = params[:property_id]
  user = User.find_by_id(user_id)
  if user&.properties
    ids = user.properties['property_ids'].to_s.split(',')
    ids = ids.uniq
    ids.delete(property_id) if ids.include? property_id
    user.properties['property_ids'] = ids.join(',')
    user.save
  end
  redirect_to admin_user_path(user)
end

#editObject

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



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

def edit; end

#indexObject

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



70
71
72
73
74
# File 'app/controllers/admin/users_controller.rb', line 70

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

  render action: :index, layout: request.xhr?.nil?
end

#newObject

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



89
90
91
# File 'app/controllers/admin/users_controller.rb', line 89

def new
  @user = User.new
end

#property_searchObject

Searches for properties based on property name.



58
59
60
61
62
63
64
65
# File 'app/controllers/admin/users_controller.rb', line 58

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.



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

def show
  if @user.properties
    user_ids = @user.properties['property_ids'].to_s.split(',')
    user_ids = user_ids.map(&:to_i)
    @properties = Property.where(id: user_ids)
  end
end

#updateObject

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



118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/admin/users_controller.rb', line 118

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