Class: Wco::ProfilesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/wco/profiles_controller.rb', line 4

def create
  @profile = Wco::Profile.new params[:profile].permit!
  authorize! :create, @profile
  if @profile.save
    flash_notice @profile
    redirect_to action: :index
  else
    flash_alert @profile
    render action: 'new'
  end
end

#destroyObject



16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/wco/profiles_controller.rb', line 16

def destroy
  @profile = Wco::Profile.unscoped.find params[:id]
  authorize! :create, @profile

  if @profile.destroy
    flash_notice 'Deleted Wco::Profile'
  else
    flash_alert "Cannot destroy profile: #{@profile.errors.fill_messages.join(', ')}."
  end
  redirect_to request.referrer || { action: 'index' }
end

#editObject



28
29
30
31
# File 'app/controllers/wco/profiles_controller.rb', line 28

def edit
  @profile = Wco::Profile.find params[:id]
  authorize! :update, @profile
end

#indexObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/wco/profiles_controller.rb', line 33

def index
  @profiles = Wco::Profile.all
  authorize! :index, Wco::Profile
  if params[:q]
    q = URI.decode(params[:q])
    @profiles = @profiles.where({ email: /#{q}/i })

    if params[:q] == 'pi'
      profile = Wco::Profile.find_by email: 'piousbox@gmail.com'
      redirect_to action: 'edit', id: profile.id.to_s
      return
    end
  end
end

#newObject



48
49
50
51
# File 'app/controllers/wco/profiles_controller.rb', line 48

def new
  @new_profile = Wco::Profile.new
  authorize! :new, @new_profile
end

#updateObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/wco/profiles_controller.rb', line 53

def update
  @profile = Wco::Profile.find params[:id]
  authorize! :update, @profile

  # if params[:photo]
  #   photo = Photo.new :photo => params[:photo]
  #   @profile.profile_photo = photo
  # end

  flag = @profile.update params[:profile].permit!
  if flag
    flash_notice "Updated profile #{@profile.email}"
  else
    flash_alert "Cannot update profile: #{@profile.errors.full_messages}"
  end
  if params[:redirect_to]
    redirect_to params[:redirect_to]
  else
    redirect_to request.referrer
  end
end