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, #tiny_mce_init_if_needed, #tiny_mce_js, #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



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

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



319
320
321
322
323
324
325
326
327
# File 'app/controllers/users_controller.rb', line 319

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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/users_controller.rb', line 158

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



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

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/users_controller.rb', line 241

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



172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/users_controller.rb', line 172

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



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

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

#deactivateObject



41
42
43
44
45
46
# File 'app/controllers/users_controller.rb', line 41

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

#delete_selectedObject



400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'app/controllers/users_controller.rb', line 400

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/users_controller.rb', line 142

def destroy
  unless @user.admin? || @user.featured_writer?
    @user.destroy
    flash[:notice] = :the_user_was_deleted.l
  else
    flash[:error] = :you_cant_delete_that_user.l
  end
  respond_to do |format|
    format.js   {
      render :inline => flash[:error], :status => 500 if flash[:error]
      render :nothing => true if flash[:notice]
    }
    format.html { redirect_to users_url }
  end
end

#editObject



104
105
106
107
# File 'app/controllers/users_controller.rb', line 104

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

#edit_accountObject



195
196
197
198
199
# File 'app/controllers/users_controller.rb', line 195

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

#edit_pro_detailsObject



218
219
220
# File 'app/controllers/users_controller.rb', line 218

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

#forgot_usernameObject



290
291
292
293
294
295
296
297
298
299
300
# File 'app/controllers/users_controller.rb', line 290

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



48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/users_controller.rb', line 48

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



281
282
283
# File 'app/controllers/users_controller.rb', line 281

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

#metro_area_updateObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/controllers/users_controller.rb', line 333

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



85
86
87
88
89
# File 'app/controllers/users_controller.rb', line 85

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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'app/controllers/users_controller.rb', line 302

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



329
330
331
# File 'app/controllers/users_controller.rb', line 329

def return_admin
  return_to_admin
end

#showObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/users_controller.rb', line 66

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



262
263
264
265
# File 'app/controllers/users_controller.rb', line 262

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

#statisticsObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/controllers/users_controller.rb', line 375

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


358
359
360
361
362
# File 'app/controllers/users_controller.rb', line 358

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

#toggle_moderatorObject



364
365
366
367
368
369
370
371
372
373
# File 'app/controllers/users_controller.rb', line 364

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/users_controller.rb', line 109

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/users_controller.rb', line 201

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/controllers/users_controller.rb', line 222

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



183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/users_controller.rb', line 183

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



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

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

#welcome_completeObject



285
286
287
288
# File 'app/controllers/users_controller.rb', line 285

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

#welcome_inviteObject



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

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

#welcome_photoObject



267
268
269
270
# File 'app/controllers/users_controller.rb', line 267

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