Module: Appoxy::Sessions::UsersController

Defined in:
lib/sessions/users_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
# File 'lib/sessions/users_controller.rb', line 6

def self.included(base)
  puts 'UsersController included'
  # Initialize module.
  base.protect_from_forgery :except => [:location, :timezone]

end

Instance Method Details

#activateObject

Usually a user gets here via an activation link in email.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sessions/users_controller.rb', line 104

def activate
  logout_keeping_session!
  @user = ::User.find_by_activation_code(params[:ac]) unless params[:ac].blank?
  case
    when (!params[:ac].blank?) && @user && !@user.is_active?
      flash[:info] = "Account activated. please login."
      @user.activate!
      redirect_to 
    when params[:ac].blank?
      flash[:error] = "The activation code was missing.  Please follow the URL from your email."
      redirect_to(root_url)
    else
      flash[:error] = "We couldn't find a user with that activation code -- check your email? Or maybe you've already activated -- try signing in."
      redirect_to(root_url)
  end
end

#after_createObject



98
99
100
# File 'lib/sessions/users_controller.rb', line 98

def after_create

end

#after_newObject



29
30
31
# File 'lib/sessions/users_controller.rb', line 29

def after_new

end

#after_save_in_createObject



94
95
96
# File 'lib/sessions/users_controller.rb', line 94

def after_save_in_create

end

#before_createObject



86
87
88
# File 'lib/sessions/users_controller.rb', line 86

def before_create

end

#before_newObject



25
26
27
# File 'lib/sessions/users_controller.rb', line 25

def before_new

end

#before_save_in_createObject



90
91
92
# File 'lib/sessions/users_controller.rb', line 90

def before_save_in_create

end

#createObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sessions/users_controller.rb', line 33

def create

  before_create

  @user = ::User.new(params[:user])

  if @user.password != params[:password_confirmation]
    flash[:error] = "Confirmation password does not match. Please try again."
    render :action=>"new"
    return
  end

  if params[:user][:password].length < 6
    flash[:error] = "Password can not be less than 6 characters."
    render :action=>"new"
    return
  end

  existing_user = ::User.find_by_email(@user.email)

  if existing_user
    if params[:ac]

    end
    # todo: remove activation_code on user
    if @user.activation_code.present?
      # hasn't logged in yet, probably invited, need to check access key
      if existing_user.activation_code == @user.activation_code
        existing_user.activate!
        existing_user.password = @user.password
        @user                  = existing_user
      end
    else
      flash[:error] = "The email you entered already exists in our system. You might want to try logging in if you already have an account."
      render :action=>"new"
      return
    end
  else
    @user.status = "active"
  end

  before_save_in_create
  if @user.save
    self.current_user = @user
    flash[:success]   = "Your account was created successfully."
    after_save_in_create
    after_create
  else
    render :action => "new"
  end

end

#geo_locationObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sessions/users_controller.rb', line 138

def geo_location
  puts 'updating users location'
  ret = {}
  if current_user
    current_user.update_position(params[:lat], params[:lng])
    ret[:status]="success"
  else
    logger.info("Could not update user location because no current_user")
    ret[:status]="failed"
    ret[:msg] = "No user"
  end
  render :json=>ret
end

#newObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sessions/users_controller.rb', line 13

def new
  before_new
  if params[:id]
    @user = ::User.find params[:id]
  else
    @user = ::User.new
    @user.email = params[:email] if params[:email]
  end
  @user.activation_code = params[:ac] if params[:ac]
  after_new
end

#timezoneObject

To use this, add “post ‘timezone’” to a member resources :users in routes.rb Also, be sure you have <%= appoxy_footer in view %>



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sessions/users_controller.rb', line 123

def timezone
  ret = {}
  if logged_in?
    puts 'SET TIMEZONE ' + params.inspect
    tz = ActiveSupport::TimeZone[params[:offset].to_i]
    if tz
      puts 'tz=' + tz.name
      current_user.time_zone = tz.name
      current_user.save(:dirty=>true)
      ret[:timezone] = tz.name
    end
  end
  render :json=>ret
end