Class: UsersController

Inherits:
BaseController show all
Includes:
Viewable
Defined in:
app/controllers/users_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#advertise, #cache_action?, #footer_content, #homepage_features, #plaxo, #rss_site_index, #site_index

Methods included from BaseHelper

#add_friend_link, #ajax_spinner_for, #avatar_for, #block_to_partial, #box, #city_cloud, #clippings_link, #commentable_url, #container_title, #excerpt_with_jump, #flash_class, #forum_page?, #is_current_user_and_featured?, #jumbotron, #last_active, #more_comments_links, #page_title, #paginating_links, #possesive, #profile_completeness, #render_jumbotron, #render_widgets, #rounded, #search_posts_title, #search_user_posts_path, #show_footer_content?, #tag_auto_complete_field, #time_ago_in_words, #time_ago_in_words_or_date, #topnav_tab, #truncate_words, #truncate_words_with_highlight, #widget

Methods included from LocalizedApplication

#get_matching_ui_locale, #get_sorted_langs_from_accept_header, #get_valid_lang_from_accept_header, #set_locale

Methods included from AuthenticatedSystem

#login_by_token, #update_last_seen_at

Instance Method Details

#activateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/users_controller.rb', line 18

def activate
  redirect_to  and return if params[:id].blank?
  @user = User.find_by_activation_code(params[:id])
  if @user and @user.activate
    self.current_user = @user
    @user.track_activity(:joined_the_site)
    flash[:notice] = :thanks_for_activating_your_account.l
    redirect_to welcome_photo_user_path(@user) and return
  end

  flash[:error] = :account_activation_error.l_with_args(:email => configatron.support_email)
  redirect_to 
end

#assumeObject



312
313
314
315
316
317
318
319
320
# File 'app/controllers/users_controller.rb', line 312

def assume
  user = User.find(params[:id])

  if assumed_user_session = self.assume_user(user)
    redirect_to user_path(assumed_user_session.record)
  else
    redirect_to users_path
  end
end

#change_profile_photoObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/users_controller.rb', line 151

def change_profile_photo
  @user   = User.find(params[:id])
  @photo  = Photo.find(params[:photo_id])
  @user.avatar = @photo

  if @user.save!
    flash[:notice] = :your_changes_were_saved.l
    redirect_to user_photo_path(@user, @photo)
  end
rescue ActiveRecord::RecordInvalid
  @metro_areas, @states = setup_locations_for(@user)
  render :action => 'edit'
end

#createObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/users_controller.rb', line 82

def create
  @user       = User.new(user_params)
  @user.role  = Role[:member]

  if (!configatron. || verify_recaptcha(@user)) && @user.save
    create_friendship_with_inviter(@user, params)
    flash[:notice] = :email_signup_thanks.l_with_args(:email => @user.email)
    redirect_to (@user)
  else
    render :action => 'new'
  end
end

#create_friendship_with_inviter(user, options = {}) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'app/controllers/users_controller.rb', line 234

def create_friendship_with_inviter(user, options = {})
  unless options[:inviter_code].blank? or options[:inviter_id].blank?
    friend = User.find(options[:inviter_id])

    if friend && friend.valid_invite_code?(options[:inviter_code])
      accepted    = FriendshipStatus[:accepted]
      @friendship = Friendship.new(:user_id => friend.id,
        :friend_id => user.id,
        :friendship_status => accepted,
        :initiator => true)

      reverse_friendship = Friendship.new(:user_id => user.id,
        :friend_id => friend.id,
        :friendship_status => accepted )

      @friendship.save!
      reverse_friendship.save!
    end
  end
end

#crop_profile_photoObject



165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/users_controller.rb', line 165

def crop_profile_photo
  unless @photo = @user.avatar
    flash[:notice] = :no_profile_photo.l
    redirect_to upload_profile_photo_user_path(@user) and return
  end
  return unless request.put? || request.patch?

  @photo.update_attributes(:crop_x => params[:crop_x], :crop_y => params[:crop_y], :crop_w => params[:crop_w], :crop_h => params[:crop_h])
  redirect_to user_path(@user)
end

#dashboardObject



51
52
53
54
55
# File 'app/controllers/users_controller.rb', line 51

def dashboard
  @user = current_user
  @network_activity = @user.network_activity
  @recommended_posts = @user.recommended_posts
end

#deactivateObject



32
33
34
35
36
37
# File 'app/controllers/users_controller.rb', line 32

def deactivate
  @user.deactivate
  current_user_session.destroy if current_user_session
  flash[:notice] = :deactivate_completed.l
  redirect_to 
end

#delete_selectedObject



393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'app/controllers/users_controller.rb', line 393

def delete_selected
  if params[:delete]
    params[:delete].each { |id|
      user = User.find(id)
      unless user.admin? || user.featured_writer?
        user.spam! if params[:spam] && configatron.has_key?(:akismet_key)
        user.destroy
      end
    }
  end
  flash[:notice] = :the_selected_users_were_deleted.l
  redirect_to admin_users_path
end

#destroyObject



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

def destroy
  @user = User.find(params[:id])
  unless @user.admin? || @user.featured_writer?
    @user.spam! if params[:spam] && configatron.has_key?(:akismet_key)
    @user.destroy
    flash[:notice] = :the_user_was_deleted.l
  else
    flash[:error] = :you_cant_delete_that_user.l
  end
  respond_to do |format|
    format.html { redirect_to users_url }
    format.js   {
      render :inline => flash[:error], :status => 500 if flash[:error]
      render if flash[:notice]
    }
  end
end

#editObject



95
96
97
98
# File 'app/controllers/users_controller.rb', line 95

def edit
  @metro_areas, @states = setup_locations_for(@user)
  @avatar     = (@user.avatar || @user.build_avatar)
end

#edit_accountObject



188
189
190
191
192
# File 'app/controllers/users_controller.rb', line 188

def 
  @user             = current_user
  @authorizations   = current_user.authorizations
  @is_current_user  = true
end

#edit_pro_detailsObject



211
212
213
# File 'app/controllers/users_controller.rb', line 211

def edit_pro_details
  @user = User.find(params[:id])
end

#forgot_usernameObject



283
284
285
286
287
288
289
290
291
292
293
# File 'app/controllers/users_controller.rb', line 283

def forgot_username
  return unless request.post?

  if @user = User.active.find_by_email(params[:email])
    UserNotifier.forgot_username(@user).deliver
    redirect_to 
    flash[:info] = :your_username_was_emailed_to_you.l
  else
    flash[:error] = :sorry_we_dont_recognize_that_email_address.l
  end
end

#indexObject



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

def index
  @users, @search, @metro_areas, @states = User.search_conditions_with_metros_and_states(params)

  @users = @users.active.recent.includes(:tags).page(params[:page]).per(20)

  @metro_areas, @states = User.find_country_and_state_from_search_params(params)

  @tags = User.tag_counts :limit => 10

  setup_metro_areas_for_cloud
end

#inviteObject



274
275
276
# File 'app/controllers/users_controller.rb', line 274

def invite
  @user = User.find(params[:id])
end

#metro_area_updateObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/controllers/users_controller.rb', line 326

def metro_area_update

  country = Country.find(params[:country_id]) unless params[:country_id].blank?
  state   = State.find(params[:state_id]) unless params[:state_id].blank?
  states  = country ? country.states : []

  if states.any?
    metro_areas = state ? state.metro_areas.order("name ASC") : []
  else
    metro_areas = country ? country.metro_areas.order("name ASC") : []
  end

  respond_to do |format|
    format.js {
      render :partial => 'shared/location_chooser', :locals => {
        :states => states,
        :metro_areas => metro_areas,
        :selected_country => params[:country_id].to_i,
        :selected_state => params[:state_id].to_i,
        :selected_metro_area => nil,
        :js => true }
    }
  end
end

#newObject



76
77
78
79
80
# File 'app/controllers/users_controller.rb', line 76

def new
  @user         = User.new(:birthday => Date.parse((Time.now - 25.years).to_s))
  @inviter_id   = params[:id]
  @inviter_code = params[:code]
end

#resend_activationObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'app/controllers/users_controller.rb', line 295

def resend_activation

  if params[:email]
    @user = User.find_by_email(params[:email])
  else
    @user = User.find(params[:id])
  end

  if @user && !@user.active?
    flash[:notice] = :activation_email_resent_message.l
    UserNotifier.(@user).deliver
    redirect_to  and return
  else
    flash[:notice] = :activation_email_not_sent_message.l
  end
end

#return_adminObject



322
323
324
# File 'app/controllers/users_controller.rb', line 322

def return_admin
  return_to_admin
end

#showObject



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

def show
  @friend_count               = @user.accepted_friendships.count
  @accepted_friendships       = @user.accepted_friendships.limit(5).to_a.collect{|f| f.friend }
  @pending_friendships_count  = @user.pending_friendships.count()

  @comments       = @user.comments.limit(10).order('created_at DESC')
  @photo_comments = Comment.find_photo_comments_for(@user)
  @users_comments = Comment.find_comments_by_user(@user).limit(5)

  @recent_posts   = @user.posts.recent.limit(2)
  @clippings      = @user.clippings.limit(5)
  @photos         = @user.photos.limit(5)
  @comment        = Comment.new

  @my_activity = Activity.recent.by_users([@user.id]).limit(10)

  update_view_count(@user) unless current_user && current_user.eql?(@user)
end

#signup_completedObject



255
256
257
258
# File 'app/controllers/users_controller.rb', line 255

def 
  @user = User.find(params[:id])
  redirect_to home_path and return unless @user
end

#statisticsObject



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'app/controllers/users_controller.rb', line 368

def statistics
  if params[:date]
    date = Date.new(params[:date][:year].to_i, params[:date][:month].to_i)
    @month = Time.parse(date.to_s)
  else
    @month = Date.today
  end

  start_date  = @month.beginning_of_month
  end_date    = @month.end_of_month.end_of_day

  @posts = @user.posts.where('? <= published_at AND published_at <= ?', start_date, end_date)

  @estimated_payment = @posts.to_a.sum do |p|
    7
  end

  respond_to do |format|
    format.html
    format.xml {
      render :xml => @posts.to_xml(:include => :category)
    }
  end
end


351
352
353
354
355
# File 'app/controllers/users_controller.rb', line 351

def toggle_featured
  @user = User.find(params[:id])
  @user.toggle!(:featured_writer)
  redirect_to user_path(@user)
end

#toggle_moderatorObject



357
358
359
360
361
362
363
364
365
366
# File 'app/controllers/users_controller.rb', line 357

def toggle_moderator
  @user = User.find(params[:id])
  if not @user.admin?
    @user.role = @user.moderator? ? Role[:member] : Role[:moderator]
    @user.save!
  else
    flash[:error] = :you_cannot_make_an_administrator_a_moderator.l
  end
  redirect_to user_path(@user)
end

#updateObject



100
101
102
103
104
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
130
131
# File 'app/controllers/users_controller.rb', line 100

def update
  @metro_areas, @states = setup_locations_for(@user)

  unless params[:metro_area_id].blank?
    @user.metro_area  = MetroArea.find(params[:metro_area_id])
    @user.state       = (@user.metro_area && @user.metro_area.state) ? @user.metro_area.state : nil
    @user.country     = @user.metro_area.country if (@user.metro_area && @user.metro_area.country)
  else
    @user.metro_area = @user.state = @user.country = nil
  end

  @user.tag_list = params[:tag_list] || ''

  if user_params
    attributes = user_params.permit!
    attributes[:avatar_attributes][:user_id] = @user.id if attributes[:avatar_attributes]
    if @user.update_attributes(attributes)
      @user.track_activity(:updated_profile)

      flash[:notice] = :your_changes_were_saved.l
      unless params[:welcome]
        redirect_to user_path(@user)
      else
        redirect_to :action => "welcome_#{params[:welcome]}", :id => @user
      end
    else
      render :action => 'edit'
    end
  else
    render :action => 'edit'
  end
end

#update_accountObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/users_controller.rb', line 194

def 
  @user             = current_user

  if @user.update_attributes(user_params)
    flash[:notice] = :your_changes_were_saved.l
    respond_to do |format|
      format.html {redirect_to user_path(@user)}
      format.js
    end
  else
    respond_to do |format|
      format.html {render :action => 'edit_account'}
      format.js
    end
  end
end

#update_pro_detailsObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/controllers/users_controller.rb', line 215

def update_pro_details
  @user = User.find(params[:id])

  if @user.update_attributes(user_params)
    respond_to do |format|
      format.html {
        flash[:notice] = :your_changes_were_saved.l
        redirect_to edit_pro_details_user_path(@user)
      }
      format.js {
        render :text => 'success'
      }
    end

  end
rescue ActiveRecord::RecordInvalid
  render :action => 'edit_pro_details'
end

#upload_profile_photoObject



176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/users_controller.rb', line 176

def upload_profile_photo
  @avatar       = Photo.new(avatar_params)
  return unless request.put? || request.patch?

  @avatar.user  = @user
  if @avatar.save
    @user.avatar  = @avatar
    @user.save
    redirect_to crop_profile_photo_user_path(@user)
  end
end

#welcome_aboutObject



265
266
267
268
# File 'app/controllers/users_controller.rb', line 265

def welcome_about
  @user = User.find(params[:id])
  @metro_areas, @states = setup_locations_for(@user)
end

#welcome_completeObject



278
279
280
281
# File 'app/controllers/users_controller.rb', line 278

def welcome_complete
  flash[:notice] = :walkthrough_complete.l_with_args(:site => configatron.community_name)
  redirect_to user_path
end

#welcome_inviteObject



270
271
272
# File 'app/controllers/users_controller.rb', line 270

def welcome_invite
  @user = User.find(params[:id])
end

#welcome_photoObject



260
261
262
263
# File 'app/controllers/users_controller.rb', line 260

def welcome_photo
  @user = User.find(params[:id])
  @avatar = (@user.avatar || @user.build_avatar)
end