Class: ProfilesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/profiles_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /profiles POST /profiles.json



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/profiles_controller.rb', line 105

def create
  if current_user.has_role?('Librarian')
    @profile = Profile.new(profile_params)
    if @profile.user
      password = @profile.user.set_auto_generated_password
    end
  else
    @profile = Profile.new(profile_params)
  end

  respond_to do |format|
    if @profile.save
      if @profile.user
        @profile.user.role = Role.where(name: 'User').first
        flash[:temporary_password] = password
      end
      format.html { redirect_to @profile, notice: t('controller.successfully_created', model: t('activerecord.models.profile')) }
      format.json { render json: @profile, status: :created, location: @profile }
    else
      prepare_options
      format.html { render action: "new" }
      format.json { render json: @profile.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /profiles/1 DELETE /profiles/1.json



156
157
158
159
160
161
162
163
# File 'app/controllers/profiles_controller.rb', line 156

def destroy
  @profile.destroy

  respond_to do |format|
    format.html { redirect_to profiles_url, notice: t('controller.successfully_deleted', model: t('activerecord.models.profile')) }
    format.json { head :no_content }
  end
end

#editObject

GET /profiles/1/edit



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/profiles_controller.rb', line 82

def edit
  if defined?(EnjuCirculation)
    if params[:mode] == 'feed_token'
      if params[:disable] == 'true'
        @profile.delete_checkout_icalendar_token
      else
        @profile.reset_checkout_icalendar_token
      end
      render partial: 'feed_token', locals: {profile: @profile}
      return
    end
  end
  if @profile.user == current_user
    redirect_to 
    return
  end
  if @profile.user.try(:locked_at?)
    @profile.user.locked = true
  end
end

#indexObject

GET /profiles GET /profiles.json



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
50
# File 'app/controllers/profiles_controller.rb', line 8

def index
  if params[:username].present?
    profile = User.where(username: params[:username]).first.try(:profile)
    if profile
      redirect_to profile
      return
    end
  end
  query = flash[:query] = params[:query].to_s
  @query = query.dup
  @count = {}

  sort = {sort_by: 'created_at', order: 'desc'}
  case params[:sort_by]
  when 'username'
    sort[:sort_by] = 'username'
  end
  case params[:order]
  when 'asc'
    sort[:order] = 'asc'
  when 'desc'
    sort[:order] = 'desc'
  end

  query = params[:query]
  page = params[:page] || 1
  role = current_user.try(:role) || Role.default_role

  search = Profile.search
  search.build do
    fulltext query if query
    with(:required_role_id).less_than_or_equal_to role.id
    order_by sort[:sort_by], sort[:order]
  end
  search.query.paginate(page.to_i, Profile.default_per_page)
  @profiles = search.execute!.results
  @count[:query_result] = @profiles.total_entries

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @profiles }
  end
end

#newObject

GET /profiles/new



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/profiles_controller.rb', line 68

def new
  @profile = Profile.new
  @profile.user = User.new
  @profile.user_group = current_user.profile.user_group
  @profile.library = current_user.profile.library
  @profile.locale = current_user.profile.locale

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @profile }
  end
end

#showObject

GET /profiles/1 GET /profiles/1.json



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/profiles_controller.rb', line 54

def show
  if @profile.user == current_user
    redirect_to 
    return
  end

  respond_to do |format|
    format.html # show.html.erb
    format.html.phone
    format.json { render json: @profile }
  end
end

#updateObject

PUT /profiles/1 PUT /profiles/1.json



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/profiles_controller.rb', line 133

def update
  @profile.update_attributes(profile_update_params)
  if @profile.user
    if @profile.user.auto_generated_password == "1"
      password = @profile.user.set_auto_generated_password
    end
  end

  respond_to do |format|
    if @profile.save
      flash[:temporary_password] = password
      format.html { redirect_to @profile, notice: t('controller.successfully_updated', model: t('activerecord.models.profile')) }
      format.json { head :no_content }
    else
      prepare_options
      format.html { render action: "edit" }
      format.json { render json: @profile.errors, status: :unprocessable_entity }
    end
  end
end