Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ka pagination #43

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Ruby

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
test:
Expand All @@ -17,22 +17,23 @@ jobs:
ports:
- 5432:5432
steps:
- uses: actions/checkout@v2
- name: Install PostgreSQL client
run: sudo apt-get -yqq install libpq-dev
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Set up yarn
run: |
yarn install --pure-lockfile
- name: Prepare database
env:
PGHOST: localhost
RAILS_ENV: test
run: bin/rails db:setup
- name: Run tests
env:
PGHOST: localhost
run: bin/rails test
- uses: actions/checkout@v2
- name: Install PostgreSQL client
run: sudo apt-get -yqq install libpq-dev
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: "3.0.4"
- name: Set up yarn
run: |
yarn install --pure-lockfile
- name: Prepare database
env:
PGHOST: localhost
RAILS_ENV: test
run: bin/rails db:setup
- name: Run tests
env:
PGHOST: localhost
run: bin/rails test
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ruby:3.0
FROM ruby:3.0.4

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
Expand Down
9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"

# Pagination
gem 'kaminari'

# Reset the id's after deleting the elements in a table
gem 'activerecord-reset-pk-sequence'


# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails", '~> 1.0.0'
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
Expand All @@ -36,7 +43,7 @@ gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
gem "bootsnap", require: false

gem 'bootstrap-sass', '~> 3.4.1'
gem 'sassc-rails', '>= 2.1.0'
gem 'sassc-rails', '~> 2.1.0'
# Use Sass to process CSS
# gem "sassc-rails"

Expand Down
31 changes: 24 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
activerecord (7.0.4)
activemodel (= 7.0.4)
activesupport (= 7.0.4)
activerecord-reset-pk-sequence (0.2.1)
activestorage (7.0.4)
actionpack (= 7.0.4)
activejob (= 7.0.4)
Expand All @@ -69,7 +70,7 @@ GEM
autoprefixer-rails (10.4.7.0)
execjs (~> 2)
bindex (0.8.1)
bootsnap (1.13.0)
bootsnap (1.15.0)
msgpack (~> 1.2)
bootstrap-sass (3.4.1)
autoprefixer-rails (>= 5.2.1)
Expand All @@ -91,8 +92,20 @@ GEM
actionpack (>= 6.0.0)
railties (>= 6.0.0)
io-console (0.5.11)
irb (1.4.2)
irb (1.5.1)
reline (>= 0.3.0)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
loofah (2.19.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand All @@ -109,7 +122,7 @@ GEM
net-protocol
net-protocol (0.1.3)
timeout
net-smtp (0.3.2)
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.13.9-aarch64-linux)
Expand Down Expand Up @@ -152,7 +165,9 @@ GEM
rake (13.0.6)
reline (0.3.1)
io-console (~> 0.5)
sassc (2.4.0)
sassc (2.1.0)
ffi (~> 1.9)
sassc (2.1.0-x86_64-linux)
ffi (~> 1.9)
sassc-rails (2.1.2)
railties (>= 4.0.0)
Expand All @@ -167,7 +182,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
stimulus-rails (1.1.0)
stimulus-rails (1.2.0)
railties (>= 6.0.0)
thor (1.2.1)
tilt (2.0.11)
Expand All @@ -185,21 +200,23 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.1)
zeitwerk (2.6.6)

PLATFORMS
aarch64-linux
x86_64-linux

DEPENDENCIES
activerecord-reset-pk-sequence
bootsnap
bootstrap-sass (~> 3.4.1)
debug
importmap-rails
kaminari
pg (~> 1.1.0)
puma (~> 5.0)
rails (~> 7.0.4)
sassc-rails (>= 2.1.0)
sassc-rails (~> 2.1.0)
sprockets-rails
stimulus-rails
turbo-rails (~> 1.0.0)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ class CoursesController < AuthenticatedController

# GET /posts or /posts.json
def index
@courses = Course.all
@courses = Course.order(:name).page params[:page]
#Temporary variable for the navbar, until we find a better solution
@user = @current_user
end

def show
@roles = Role.where(course_id: @course.id)
@roles = Role.where(course_id: @course.id).order(:user_id).page params[:mem_pagina]
#Temporary variable for the navbar, until we find a better solution
@user = @current_user

@role = Role.where(user_id: @user, course_id:@course.id).first
@posts = Post.where(course_id: @course.id)
@posts = Post.where(course_id: @course.id).order(:created_at).page params[:posts_pagina]

end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class PostsController < AuthenticatedController


def index
@posts = Post.all
@posts = Post.order(:title).page params[:page]
end

def show
@comments = Comment.where(post_id: @post.id)
@comments = Comment.where(post_id: @post.id).order(:created_at).page params[:comments_pagina]
@post_user = User.find(@post.user_id)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index

def show
@user = User.find(params[:id])
@courses = @user.courses
@courses = @user.courses.order(:name).page params[:courses_pagina]
end

def login
Expand Down
1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Comment < ApplicationRecord
# belongs_to :user
# belongs_to :post
paginates_per 15
belongs_to :commentable, polymorphic: true, optional: true

validates :body, presence: true
Expand Down
1 change: 1 addition & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Course < ApplicationRecord
paginates_per 15
has_many :roles, autosave: true, dependent: :destroy
has_many :users, through: :roles
has_many :posts, dependent: :destroy
Expand Down
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Post < ApplicationRecord
# belongs_to :user
# belongs_to :class
paginates_per 15
belongs_to :postable, polymorphic: true, optional: true
has_many :comments, dependent: :destroy, as: :commentable

Expand Down
1 change: 1 addition & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Role < ApplicationRecord
paginates_per 15
belongs_to :user
belongs_to :course
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class User < ApplicationRecord
paginates_per 15
has_many :roles, autosave: true, dependent: :destroy
has_many :courses, through: :roles
has_many :posts, dependent: :destroy, as: :postable
Expand Down
1 change: 1 addition & 0 deletions app/views/courses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@
<% end %>
</table>
</div>
<%= paginate @courses %>
</div>
79 changes: 41 additions & 38 deletions app/views/courses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,52 @@
<li class="nav-link"><%= link_to "Delete Course", course_path(@course), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
</li>
<% end %>
} %>
</li>
<% end %>
<li><%= link_to "Logout", logout_path%> </li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class='main container' style='text-align:left;'>
<h3><%=@course.name %></h3>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Posts</div>
<% if @posts.length() < 1%>
<div class='container'>
<h5>No posts yet :/ Make one <%= link_to "here!", new_course_post_path(@course) %> </h5>
</div>
<% else %>
<!-- Table -->
<table class="table">
<tr>
<th>Author</th>
<th>Title</th>
<th>Body</th>
<th></th>
</tr>
<% @posts.each do |post|%>
<tr>
<td><%= User.find(post.user_id).name %></td>
<td><%= link_to post.title, course_post_path(@course,post) %></td>
<td><%= post.body %></td>
<td>
<div class='button-div'>
<button class='btn btn-primary'><%= link_to "Edit", edit_course_post_path(@course,post) %></button>
<button class='btn btn-primary'><%= link_to "Delete", course_post_path(@course,post.id), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %></button>
</div>
</td>
</tr>
<h3><%=@course.name %></h3>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Posts</div>
<% if @posts.length() < 1%>
<div class='container'>
<h5>No posts yet :/ Make one <%= link_to "here!", new_course_post_path(@course) %> </h5>
</div>
<% else %>
<!-- Table -->
<table class="table">
<tr>
<th>Author</th>
<th>Title</th>
<th>Body</th>
<th></th>
</tr>
<% @posts.each do |post|%>
<tr>
<td><%= User.find(post.user_id).name %></td>
<td><%= link_to post.title, course_post_path(@course,post) %></td>
<td><%= post.body %></td>
<td>
<div class='button-div'>
<button class='btn btn-primary'><%= link_to "Edit", edit_course_post_path(@course,post) %></button>
<button class='btn btn-primary'><%= link_to "Delete", course_post_path(@course,post.id), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %></button>
</div>
</td>
</tr>
<% end %>
</table>
<% end %>
</table>
<% end %>
</div>
</div>
<%= paginate @posts, param_name: :posts_pagina %>

<div class="panel panel-default">
<!-- Default panel contents -->
Expand Down Expand Up @@ -97,6 +98,8 @@
<% end %>
</table>
</div>
<%= paginate @roles, param_name: :mem_pagina %>

<div>
<% if @role.role ==0 %>
<%= link_to "Add a student", new_student_course_path%>
Expand Down
Loading