Переглянути джерело

Add courses action to users controller

Frans Bergman 7 роки тому
батько
коміт
4075829aa8

+ 1 - 0
Gemfile

@@ -34,6 +34,7 @@ gem "font-awesome-rails"
 # Bootstrap Forms
 gem 'bootstrap_form', '~> 2.7.0'
 gem 'select2-rails', '~> 4.0.3'
+gem 'possessive'
 # Use Redis adapter to run Action Cable in production
 # gem 'redis', '~> 3.0'
 # Use ActiveModel has_secure_password

+ 3 - 1
Gemfile.lock

@@ -112,6 +112,7 @@ GEM
     nio4r (2.1.0)
     nokogiri (1.8.1)
       mini_portile2 (~> 2.3.0)
+    possessive (1.0.1)
     public_suffix (3.0.1)
     puma (3.11.0)
     rack (2.0.3)
@@ -217,6 +218,7 @@ DEPENDENCIES
   jquery-rails (~> 4.3.1)
   listen (>= 3.0.5, < 3.2)
   mini_magick (= 4.7.0)
+  possessive
   puma (~> 3.7)
   rails (~> 5.1.4)
   rails-controller-testing
@@ -232,4 +234,4 @@ DEPENDENCIES
   web-console (>= 3.3.0)
 
 BUNDLED WITH
-   1.15.4
+   1.16.1

+ 7 - 1
app/controllers/users_controller.rb

@@ -1,6 +1,6 @@
 class UsersController < ApplicationController
 
-  before_action :set_user, only: [:show, :edit, :update, :destroy]
+  before_action :set_user, only: [:show, :edit, :update, :destroy, :courses]
   before_action :check_permission, only: [:edit, :update]
 
   def index
@@ -29,6 +29,12 @@ class UsersController < ApplicationController
     end
   end
 
+  def courses
+    @title = "#{@user.name.possessive} courses"
+    @courses = @user.courses
+    render 'show_courses'
+  end
+
   private
     # Use callbacks to share common setup or constraints between actions.
     def set_user

+ 3 - 0
app/models/user.rb

@@ -39,6 +39,9 @@ class User < ApplicationRecord
 
   belongs_to :school, optional: true
 
+  has_many :course_participations
+  has_many :courses, through: :course_participations
+
   # Returns the hash digest of the given string.
   def User.digest(string)
     cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :

+ 3 - 0
app/views/layouts/_navigation.html.erb

@@ -88,6 +88,9 @@
             <%= link_to fa_icon("building fw", text: school.name), school_path(school) %>
           </li>
         <% end %>
+        <li>
+          <%= link_to fa_icon("book fw", text: "Courses"), courses_user_path(current_user) %>
+        </li>
       </ul>
     </div>
   </div>

+ 20 - 0
app/views/users/show_courses.html.erb

@@ -0,0 +1,20 @@
+<%= provide(:title, @title) %>
+<h1><%= @title %></h1>
+<div class="row">
+  <table class="table table-hover table-striped table-bordered">
+    <thead>
+      <th>
+        Course
+      </th>
+    </thead>
+    <tbody>
+      <% for course in @courses %>
+        <tr>
+          <td>
+            <%= link_to course.name, course %>
+          </td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+</div>

+ 5 - 1
config/routes.rb

@@ -8,7 +8,11 @@ Rails.application.routes.draw do
   post   '/login',   to: 'sessions#create'
   delete '/logout',  to: 'sessions#destroy'
 
-  resources :users
+  resources :users do
+    member do
+      get :courses
+    end
+  end
   resources :conversations
   resources :conversation_participations
   resources :messages