Browse Source

flash_message helper

- Reworked the way alerts work to support multiple messages per category
- Simplified queries for activities in DashboardController
- Fixed some tables
Maarten van den Berg 8 years ago
parent
commit
65e24a906c

+ 12 - 3
app/controllers/activities_controller.rb

@@ -38,7 +38,10 @@ class ActivitiesController < ApplicationController
38 38
 
39 39
     respond_to do |format|
40 40
       if @activity.save
41
-        format.html { redirect_to group_activity_url(@group, @activity), notice: 'Activity was successfully created.' }
41
+        format.html {
42
+          redirect_to group_activity_url(@group, @activity)
43
+          flash_message(:info, 'Activity was successfully created.')
44
+        }
42 45
         format.json { render :show, status: :created, location: @activity }
43 46
       else
44 47
         format.html { render :new }
@@ -52,7 +55,10 @@ class ActivitiesController < ApplicationController
52 55
   def update
53 56
     respond_to do |format|
54 57
       if @activity.update(activity_params)
55
-        format.html { redirect_to group_activity_url(@group, @activity), notice: 'Activity was successfully updated.' }
58
+        format.html {
59
+          redirect_to group_activity_url(@group, @activity)
60
+          flash_message(:info, 'Activity was successfully updated.')
61
+        }
56 62
         format.json { render :show, status: :ok, location: @activity }
57 63
       else
58 64
         format.html { render :edit }
@@ -66,7 +72,10 @@ class ActivitiesController < ApplicationController
66 72
   def destroy
67 73
     @activity.destroy
68 74
     respond_to do |format|
69
-      format.html { redirect_to group_activities_url(@group), notice: 'Activity was successfully destroyed.' }
75
+      format.html {
76
+        redirect_to group_activities_url(@group)
77
+        flash_message(:info, 'Activity was successfully destroyed.')
78
+      }
70 79
       format.json { head :no_content }
71 80
     end
72 81
   end

+ 1 - 0
app/controllers/application_controller.rb

@@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base
3 3
   rescue_from ActionController::InvalidAuthenticityToken, with: :invalid_auth_token
4 4
 
5 5
   include AuthenticationHelper
6
+  include ApplicationHelper
6 7
 
7 8
   private
8 9
   def invalid_auth_token

+ 14 - 14
app/controllers/authentication_controller.rb

@@ -6,7 +6,7 @@ class AuthenticationController < ApplicationController
6 6
 
7 7
   def login
8 8
     if params[:session][:email].blank? || params[:session][:password].blank?
9
-      flash[:warning] = "You forgot to add value"
9
+      flash_message(:warning, "You forgot to add value")
10 10
       redirect_to action: 'login_form'
11 11
     else
12 12
       u = User.find_by(email: params[:session][:email])
@@ -14,13 +14,13 @@ class AuthenticationController < ApplicationController
14 14
       if u && u.confirmed && u.authenticate(params[:session][:password])
15 15
         log_in(u, params[:session][:remember_me].to_i)
16 16
 
17
-        flash[:success] = "Hello, #{u.person.full_name}!"
17
+        flash_message(:success, "Hello, #{u.person.full_name}!")
18 18
         redirect_to root_path
19 19
       elsif u and not u.confirmed
20
-        flash[:warning] = "Your account has not been activated yet, please confirm using the email you have received."
20
+        flash_message(:warning, "Your account has not been activated yet, please confirm using the email you have received.")
21 21
         redirect_to action: 'login_form'
22 22
       else
23
-        flash[:danger] = "Invalid username/password combination!"
23
+        flash_message(:danger, "Invalid username/password combination!")
24 24
         redirect_to action: 'login_form'
25 25
       end
26 26
     end
@@ -47,14 +47,14 @@ class AuthenticationController < ApplicationController
47 47
     person = Person.find_by(email: params[:user][:email])
48 48
 
49 49
     if not person
50
-      flash[:warning] = "That email address is unknown!"
50
+      flash_message(:warning, "That email address is unknown!")
51 51
       redirect_to action: 'create_password_form'
52 52
       return
53 53
     end
54 54
 
55 55
     user = User.find_by(person: person)
56 56
     if user and user.confirmed
57
-      flash[:warning] = "Your account has already been activated, please use the login form if you have forgotten your password."
57
+      flash_message(:warning, "Your account has already been activated, please use the login form if you have forgotten your password.")
58 58
       redirect_to action: 'login'
59 59
       return
60 60
     end
@@ -69,7 +69,7 @@ class AuthenticationController < ApplicationController
69 69
     end
70 70
 
71 71
     AuthenticationMailer::password_confirm_email(user).deliver_now
72
-    flash[:success] = "An email has been sent, check your inbox!"
72
+    flash_message(:success, "An email has been sent, check your inbox!")
73 73
     redirect_to action: 'login'
74 74
   end
75 75
 
@@ -80,12 +80,12 @@ class AuthenticationController < ApplicationController
80 80
   def forgotten_password
81 81
     user = User.find_by(email: params[:password_reset][:email])
82 82
     if not user
83
-      flash[:danger] = "That email address is not associated with any user."
83
+      flash_message(:danger, "That email address is not associated with any user.")
84 84
       redirect_to action: 'forgotten_password_form'
85 85
       return
86 86
     end
87 87
     AuthenticationMailer::password_reset_email(user).deliver_later
88
-    flash[:success] = "An email has been sent, check your inbox!"
88
+    flash_message(:success, "An email has been sent, check your inbox!")
89 89
     redirect_to action: 'login'
90 90
   end
91 91
 
@@ -104,7 +104,7 @@ class AuthenticationController < ApplicationController
104 104
     end
105 105
 
106 106
     if not params[:password] == params[:password_confirmation]
107
-      flash[:warning] = "Password confirmation does not match your password!"
107
+      flash_message(:warning, "Password confirmation does not match your password!")
108 108
       redirect_to action: 'reset_password_form', token: params[:token]
109 109
       return
110 110
     end
@@ -116,7 +116,7 @@ class AuthenticationController < ApplicationController
116 116
 
117 117
     token.destroy!
118 118
 
119
-    flash[:success] = "Your password has been reset, you may now log in."
119
+    flash_message(:success, "Your password has been reset, you may now log in.")
120 120
     redirect_to action: 'login'
121 121
   end
122 122
 
@@ -140,7 +140,7 @@ class AuthenticationController < ApplicationController
140 140
 
141 141
     token.destroy!
142 142
 
143
-    flash[:success] = "Your account has been confirmed, you may now log in."
143
+    flash_message(:success, "Your account has been confirmed, you may now log in.")
144 144
     redirect_to action: 'login'
145 145
   end
146 146
 
@@ -151,12 +151,12 @@ class AuthenticationController < ApplicationController
151 151
 
152 152
   def token_valid?(token)
153 153
     if token.nil?
154
-      flash[:warning] = "No valid token specified!"
154
+      flash_message(:warning, "No valid token specified!")
155 155
       redirect_to action: 'login'
156 156
       return false
157 157
     end
158 158
     if token.expires and token.expires < DateTime.now
159
-      flash[:warning] = "That token has expired, please request a new one."
159
+      flash_message(:warning, "That token has expired, please request a new one.")
160 160
       redirect_to action: 'login'
161 161
       return false
162 162
     end

+ 4 - 10
app/controllers/dashboard_controller.rb

@@ -2,20 +2,14 @@ class DashboardController < ApplicationController
2 2
   before_action :require_login!
3 3
 
4 4
   def home
5
-    @user_organized = current_person
6
-      .participants
7
-      .joins(:activity)
8
-      .where(is_organizer: true)
9
-      .order('activities.start ASC')
10
-    @need_response = current_person
11
-      .participants
12
-      .joins(:activity)
13
-      .where(attending: nil)
14
-      .order('activities.start ASC')
15 5
     @upcoming = current_person
16 6
       .participants
17 7
       .joins(:activity)
18 8
       .where('activities.start >= ?', DateTime.now)
19 9
       .order('activities.start ASC')
10
+    @user_organized = @upcoming
11
+      .where(is_organizer: true)
12
+    @need_response = @upcoming
13
+      .where(attending: nil)
20 14
   end
21 15
 end

+ 12 - 3
app/controllers/groups_controller.rb

@@ -36,7 +36,10 @@ class GroupsController < ApplicationController
36 36
 
37 37
     respond_to do |format|
38 38
       if @group.save
39
-        format.html { redirect_to @group, notice: 'Group was successfully created.' }
39
+        format.html {
40
+          redirect_to @group
41
+          flash_message(:info, 'Group was successfully created.')
42
+        }
40 43
         format.json { render :show, status: :created, location: @group }
41 44
       else
42 45
         format.html { render :new }
@@ -50,7 +53,10 @@ class GroupsController < ApplicationController
50 53
   def update
51 54
     respond_to do |format|
52 55
       if @group.update(group_params)
53
-        format.html { redirect_to @group, notice: 'Group was successfully updated.' }
56
+        format.html {
57
+          redirect_to @group
58
+          flash_message(:info, 'Group was successfully updated.')
59
+        }
54 60
         format.json { render :show, status: :ok, location: @group }
55 61
       else
56 62
         format.html { render :edit }
@@ -64,7 +70,10 @@ class GroupsController < ApplicationController
64 70
   def destroy
65 71
     @group.destroy
66 72
     respond_to do |format|
67
-      format.html { redirect_to groups_url, notice: 'Group was successfully destroyed.' }
73
+      format.html {
74
+        redirect_to groups_url
75
+        flash_message(:info, 'Group was successfully destroyed.')
76
+      }
68 77
       format.json { head :no_content }
69 78
     end
70 79
   end

+ 15 - 3
app/controllers/members_controller.rb

@@ -8,6 +8,9 @@ class MembersController < ApplicationController
8 8
   # GET /members.json
9 9
   def index
10 10
     @members = @group.members
11
+      .joins(:person)
12
+      .order(is_leader: :desc)
13
+      .order('people.first_name ASC')
11 14
   end
12 15
 
13 16
   # GET /members/1
@@ -34,7 +37,10 @@ class MembersController < ApplicationController
34 37
 
35 38
     respond_to do |format|
36 39
       if @member.save
37
-        format.html { redirect_to group_member_url(@group, @member), notice: 'Member was successfully created.' }
40
+        format.html {
41
+          redirect_to group_member_url(@group, @member)
42
+          flash_message(:info, 'Member was successfully created.')
43
+        }
38 44
         format.json { render :show, status: :created, location: @member }
39 45
       else
40 46
         @possible_members = Person.where.not(id: @group.person_ids)
@@ -49,7 +55,10 @@ class MembersController < ApplicationController
49 55
   def update
50 56
     respond_to do |format|
51 57
       if @member.update(member_params)
52
-        format.html { redirect_to group_member_url(@group, @member), notice: 'Member was successfully updated.' }
58
+        format.html {
59
+          redirect_to group_member_url(@group, @member)
60
+          flash_message(:info, 'Member was successfully updated.')
61
+        }
53 62
         format.json { render :show, status: :ok, location: @member }
54 63
       else
55 64
         @possible_members = Person.where.not(id: @group.person_ids)
@@ -64,7 +73,10 @@ class MembersController < ApplicationController
64 73
   def destroy
65 74
     @member.destroy
66 75
     respond_to do |format|
67
-      format.html { redirect_to group_members_url(@group), notice: 'Member was successfully destroyed.' }
76
+      format.html {
77
+        redirect_to group_members_url(@group)
78
+        flash_message(:info, 'Member was successfully destroyed.')
79
+      }
68 80
       format.json { head :no_content }
69 81
     end
70 82
   end

+ 3 - 3
app/controllers/people_controller.rb

@@ -34,7 +34,7 @@ class PeopleController < ApplicationController
34 34
     respond_to do |format|
35 35
       if @person.save
36 36
         format.html do
37
-          flash[:success] = "Person was successfully created."
37
+          flash_message(:success, "Person was successfully created.")
38 38
           redirect_to @person
39 39
         end
40 40
         format.json { render :show, status: :created, location: @person }
@@ -51,7 +51,7 @@ class PeopleController < ApplicationController
51 51
     respond_to do |format|
52 52
       if @person.update(person_params)
53 53
         format.html do
54
-          flash[:success] = "Person was successfully updated."
54
+          flash_message(:success, "Person was successfully updated.")
55 55
           redirect_to @person
56 56
         end
57 57
 
@@ -69,7 +69,7 @@ class PeopleController < ApplicationController
69 69
     @person.destroy
70 70
     respond_to do |format|
71 71
       format.html do
72
-        flash[:success] = 'Person was successfully destroyed.'
72
+        flash_message(:success, 'Person was successfully destroyed.')
73 73
         redirect_to people_url
74 74
       end
75 75
 

+ 5 - 0
app/helpers/application_helper.rb

@@ -1,2 +1,7 @@
1 1
 module ApplicationHelper
2
+  def flash_message(category, message, **pairs)
3
+    flash[:alerts] ||= {}
4
+    flash[:alerts][category] ||= []
5
+    flash[:alerts][category] << message
6
+  end
2 7
 end

+ 2 - 2
app/helpers/authentication_helper.rb

@@ -114,14 +114,14 @@ module AuthenticationHelper
114 114
 
115 115
   def require_login!
116 116
     if !is_logged_in?
117
-      flash[:warning] = "You need to be logged in to do that."
117
+      flash_message(:warning, "You need to be logged in to do that.")
118 118
       redirect_to controller: 'authentication', action: 'login_form'
119 119
     end
120 120
   end
121 121
 
122 122
   def require_admin!
123 123
     if !current_person.is_admin?
124
-      flash[:danger] = "You need to be an administrator to do that."
124
+      flash_message(:danger, "You need to be an administrator to do that.")
125 125
       redirect_to '/dashboard'
126 126
     end
127 127
   end

+ 2 - 2
app/helpers/groups_helper.rb

@@ -2,7 +2,7 @@ module GroupsHelper
2 2
   def require_membership!
3 3
     require_login!
4 4
     if !(Member.exists?(group: @group, person: current_person) || current_person.is_admin?)
5
-      flash[:danger] = "You need to be a member of that group to do that."
5
+      flash_message(:danger, "You need to be a member of that group to do that.")
6 6
       redirect_to dashboard_url
7 7
     end
8 8
   end
@@ -12,7 +12,7 @@ module GroupsHelper
12 12
 
13 13
     if !(Member.exists?(group: @group, is_leader: true, person: current_person) ||
14 14
          current_person.is_admin?)
15
-      flash[:danger] = "You need to be a group leader to do that."
15
+      flash_message(:danger, "You need to be a group leader to do that.")
16 16
       redirect_to dashboard_url
17 17
     end
18 18
   end

+ 0 - 2
app/views/activities/index.html.erb

@@ -1,5 +1,3 @@
1
-<p id="notice"><%= notice %></p>
2
-
3 1
 <h1>Activities</h1>
4 2
 
5 3
 <table class="table">

+ 2 - 2
app/views/groups/index.html.erb

@@ -1,5 +1,3 @@
1
-<p id="notice"><%= notice %></p>
2
-
3 1
 <h1>Groups</h1>
4 2
 
5 3
 <table class="table">
@@ -16,6 +14,8 @@
16 14
     <% @groups.each do |group| %>
17 15
       <tr>
18 16
         <td><%= link_to group.name, group %></td>
17
+        <td><%= link_to group.members.count, group_members_path(group) %></td>
18
+        <td><%= link_to group.activities.count, group_activities_path(group) %></td>
19 19
         <td><%= link_to 'Edit', edit_group_path(group) %></td>
20 20
         <td><%= link_to 'Destroy', group, method: :delete, data: { confirm: 'Are you sure?' } %></td>
21 21
       </tr>

+ 10 - 7
app/views/members/index.html.erb

@@ -1,20 +1,23 @@
1
-<p id="notice"><%= notice %></p>
2
-
3 1
 <h1>Members</h1>
4 2
 
5
-<table>
3
+<table class="table table-striped">
6 4
   <thead>
7 5
     <tr>
8
-      <th colspan="3"></th>
6
+      <th>Name</th>
7
+      <th>Remove</th>
9 8
     </tr>
10 9
   </thead>
11 10
 
12 11
   <tbody>
13 12
     <% @members.each do |member| %>
14 13
       <tr>
15
-        <td><%= link_to member.person.full_name, group_member_path(@group, member) %></td>
16
-        <td><%= link_to 'Edit', edit_group_member_path(@group, member) %></td>
17
-        <td><%= link_to 'Destroy', group_member_path(@group, member), method: :delete, data: { confirm: 'Are you sure?' } %></td>
14
+        <td>
15
+          <%= link_to member.person.full_name, group_member_path(@group, member) %>
16
+          <% if member.is_leader %>
17
+            (<i class="fa fa-angle-up"></i>)
18
+          <% end %>
19
+        </td>
20
+        <td><%= link_to 'Remove', group_member_path(@group, member), method: :delete, data: { confirm: 'Are you sure?' } %></td>
18 21
       </tr>
19 22
     <% end %>
20 23
   </tbody>

+ 0 - 2
app/views/members/show.html.erb

@@ -1,5 +1,3 @@
1
-<p id="notice"><%= notice %></p>
2
-
3 1
 <%= @member.person.full_name %> is lid van <%= @group.name %>.
4 2
 <%= link_to 'Edit', edit_group_member_path(@group, @member) %> |
5 3
 <%= link_to 'Back', group_members_path(@group) %>

+ 0 - 2
app/views/people/index.html.erb

@@ -1,5 +1,3 @@
1
-<p id="notice"><%= notice %></p>
2
-
3 1
 <h1>People</h1>
4 2
 
5 3
 <table class="table">

+ 5 - 3
app/views/shared/_alerts.html.haml

@@ -1,5 +1,7 @@
1 1
 .container
2 2
   .alerts
3
-    - flash.each do |name, msg|
4
-      %div{class: "alert alert-#{name}"}
5
-        = msg
3
+    - if flash[:alerts]
4
+      - flash[:alerts].each do |category, msgs|
5
+        - msgs.each do |msg|
6
+          %div{class: "alert alert-#{category}"}
7
+            = msg