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
 
38
 
39
     respond_to do |format|
39
     respond_to do |format|
40
       if @activity.save
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
         format.json { render :show, status: :created, location: @activity }
45
         format.json { render :show, status: :created, location: @activity }
43
       else
46
       else
44
         format.html { render :new }
47
         format.html { render :new }
52
   def update
55
   def update
53
     respond_to do |format|
56
     respond_to do |format|
54
       if @activity.update(activity_params)
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
         format.json { render :show, status: :ok, location: @activity }
62
         format.json { render :show, status: :ok, location: @activity }
57
       else
63
       else
58
         format.html { render :edit }
64
         format.html { render :edit }
66
   def destroy
72
   def destroy
67
     @activity.destroy
73
     @activity.destroy
68
     respond_to do |format|
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
       format.json { head :no_content }
79
       format.json { head :no_content }
71
     end
80
     end
72
   end
81
   end

+ 1 - 0
app/controllers/application_controller.rb

3
   rescue_from ActionController::InvalidAuthenticityToken, with: :invalid_auth_token
3
   rescue_from ActionController::InvalidAuthenticityToken, with: :invalid_auth_token
4
 
4
 
5
   include AuthenticationHelper
5
   include AuthenticationHelper
6
+  include ApplicationHelper
6
 
7
 
7
   private
8
   private
8
   def invalid_auth_token
9
   def invalid_auth_token

+ 14 - 14
app/controllers/authentication_controller.rb

6
 
6
 
7
   def login
7
   def login
8
     if params[:session][:email].blank? || params[:session][:password].blank?
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
       redirect_to action: 'login_form'
10
       redirect_to action: 'login_form'
11
     else
11
     else
12
       u = User.find_by(email: params[:session][:email])
12
       u = User.find_by(email: params[:session][:email])
14
       if u && u.confirmed && u.authenticate(params[:session][:password])
14
       if u && u.confirmed && u.authenticate(params[:session][:password])
15
         log_in(u, params[:session][:remember_me].to_i)
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
         redirect_to root_path
18
         redirect_to root_path
19
       elsif u and not u.confirmed
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
         redirect_to action: 'login_form'
21
         redirect_to action: 'login_form'
22
       else
22
       else
23
-        flash[:danger] = "Invalid username/password combination!"
23
+        flash_message(:danger, "Invalid username/password combination!")
24
         redirect_to action: 'login_form'
24
         redirect_to action: 'login_form'
25
       end
25
       end
26
     end
26
     end
47
     person = Person.find_by(email: params[:user][:email])
47
     person = Person.find_by(email: params[:user][:email])
48
 
48
 
49
     if not person
49
     if not person
50
-      flash[:warning] = "That email address is unknown!"
50
+      flash_message(:warning, "That email address is unknown!")
51
       redirect_to action: 'create_password_form'
51
       redirect_to action: 'create_password_form'
52
       return
52
       return
53
     end
53
     end
54
 
54
 
55
     user = User.find_by(person: person)
55
     user = User.find_by(person: person)
56
     if user and user.confirmed
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
       redirect_to action: 'login'
58
       redirect_to action: 'login'
59
       return
59
       return
60
     end
60
     end
69
     end
69
     end
70
 
70
 
71
     AuthenticationMailer::password_confirm_email(user).deliver_now
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
     redirect_to action: 'login'
73
     redirect_to action: 'login'
74
   end
74
   end
75
 
75
 
80
   def forgotten_password
80
   def forgotten_password
81
     user = User.find_by(email: params[:password_reset][:email])
81
     user = User.find_by(email: params[:password_reset][:email])
82
     if not user
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
       redirect_to action: 'forgotten_password_form'
84
       redirect_to action: 'forgotten_password_form'
85
       return
85
       return
86
     end
86
     end
87
     AuthenticationMailer::password_reset_email(user).deliver_later
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
     redirect_to action: 'login'
89
     redirect_to action: 'login'
90
   end
90
   end
91
 
91
 
104
     end
104
     end
105
 
105
 
106
     if not params[:password] == params[:password_confirmation]
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
       redirect_to action: 'reset_password_form', token: params[:token]
108
       redirect_to action: 'reset_password_form', token: params[:token]
109
       return
109
       return
110
     end
110
     end
116
 
116
 
117
     token.destroy!
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
     redirect_to action: 'login'
120
     redirect_to action: 'login'
121
   end
121
   end
122
 
122
 
140
 
140
 
141
     token.destroy!
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
     redirect_to action: 'login'
144
     redirect_to action: 'login'
145
   end
145
   end
146
 
146
 
151
 
151
 
152
   def token_valid?(token)
152
   def token_valid?(token)
153
     if token.nil?
153
     if token.nil?
154
-      flash[:warning] = "No valid token specified!"
154
+      flash_message(:warning, "No valid token specified!")
155
       redirect_to action: 'login'
155
       redirect_to action: 'login'
156
       return false
156
       return false
157
     end
157
     end
158
     if token.expires and token.expires < DateTime.now
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
       redirect_to action: 'login'
160
       redirect_to action: 'login'
161
       return false
161
       return false
162
     end
162
     end

+ 4 - 10
app/controllers/dashboard_controller.rb

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

+ 12 - 3
app/controllers/groups_controller.rb

36
 
36
 
37
     respond_to do |format|
37
     respond_to do |format|
38
       if @group.save
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
         format.json { render :show, status: :created, location: @group }
43
         format.json { render :show, status: :created, location: @group }
41
       else
44
       else
42
         format.html { render :new }
45
         format.html { render :new }
50
   def update
53
   def update
51
     respond_to do |format|
54
     respond_to do |format|
52
       if @group.update(group_params)
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
         format.json { render :show, status: :ok, location: @group }
60
         format.json { render :show, status: :ok, location: @group }
55
       else
61
       else
56
         format.html { render :edit }
62
         format.html { render :edit }
64
   def destroy
70
   def destroy
65
     @group.destroy
71
     @group.destroy
66
     respond_to do |format|
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
       format.json { head :no_content }
77
       format.json { head :no_content }
69
     end
78
     end
70
   end
79
   end

+ 15 - 3
app/controllers/members_controller.rb

8
   # GET /members.json
8
   # GET /members.json
9
   def index
9
   def index
10
     @members = @group.members
10
     @members = @group.members
11
+      .joins(:person)
12
+      .order(is_leader: :desc)
13
+      .order('people.first_name ASC')
11
   end
14
   end
12
 
15
 
13
   # GET /members/1
16
   # GET /members/1
34
 
37
 
35
     respond_to do |format|
38
     respond_to do |format|
36
       if @member.save
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
         format.json { render :show, status: :created, location: @member }
44
         format.json { render :show, status: :created, location: @member }
39
       else
45
       else
40
         @possible_members = Person.where.not(id: @group.person_ids)
46
         @possible_members = Person.where.not(id: @group.person_ids)
49
   def update
55
   def update
50
     respond_to do |format|
56
     respond_to do |format|
51
       if @member.update(member_params)
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
         format.json { render :show, status: :ok, location: @member }
62
         format.json { render :show, status: :ok, location: @member }
54
       else
63
       else
55
         @possible_members = Person.where.not(id: @group.person_ids)
64
         @possible_members = Person.where.not(id: @group.person_ids)
64
   def destroy
73
   def destroy
65
     @member.destroy
74
     @member.destroy
66
     respond_to do |format|
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
       format.json { head :no_content }
80
       format.json { head :no_content }
69
     end
81
     end
70
   end
82
   end

+ 3 - 3
app/controllers/people_controller.rb

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

+ 5 - 0
app/helpers/application_helper.rb

1
 module ApplicationHelper
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
 end
7
 end

+ 2 - 2
app/helpers/authentication_helper.rb

114
 
114
 
115
   def require_login!
115
   def require_login!
116
     if !is_logged_in?
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
       redirect_to controller: 'authentication', action: 'login_form'
118
       redirect_to controller: 'authentication', action: 'login_form'
119
     end
119
     end
120
   end
120
   end
121
 
121
 
122
   def require_admin!
122
   def require_admin!
123
     if !current_person.is_admin?
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
       redirect_to '/dashboard'
125
       redirect_to '/dashboard'
126
     end
126
     end
127
   end
127
   end

+ 2 - 2
app/helpers/groups_helper.rb

2
   def require_membership!
2
   def require_membership!
3
     require_login!
3
     require_login!
4
     if !(Member.exists?(group: @group, person: current_person) || current_person.is_admin?)
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
       redirect_to dashboard_url
6
       redirect_to dashboard_url
7
     end
7
     end
8
   end
8
   end
12
 
12
 
13
     if !(Member.exists?(group: @group, is_leader: true, person: current_person) ||
13
     if !(Member.exists?(group: @group, is_leader: true, person: current_person) ||
14
          current_person.is_admin?)
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
       redirect_to dashboard_url
16
       redirect_to dashboard_url
17
     end
17
     end
18
   end
18
   end

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

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

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

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

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

1
-<p id="notice"><%= notice %></p>
2
-
3
 <h1>Members</h1>
1
 <h1>Members</h1>
4
 
2
 
5
-<table>
3
+<table class="table table-striped">
6
   <thead>
4
   <thead>
7
     <tr>
5
     <tr>
8
-      <th colspan="3"></th>
6
+      <th>Name</th>
7
+      <th>Remove</th>
9
     </tr>
8
     </tr>
10
   </thead>
9
   </thead>
11
 
10
 
12
   <tbody>
11
   <tbody>
13
     <% @members.each do |member| %>
12
     <% @members.each do |member| %>
14
       <tr>
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
       </tr>
21
       </tr>
19
     <% end %>
22
     <% end %>
20
   </tbody>
23
   </tbody>

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

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

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

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

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

1
 .container
1
 .container
2
   .alerts
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