Skip to content

Commit c577a4a

Browse files
authored
Merge pull request #34 from dalvarez2596/lesson-79
Use ransack on enrollments table
2 parents 432c402 + 1e2ba41 commit c577a4a

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

app/controllers/enrollments_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class EnrollmentsController < ApplicationController
44

55
def index
66
# @enrollments = Enrollment.all
7-
@pagy, @enrollments = pagy(Enrollment.all)
7+
@q = Enrollment.ransack(params[:q])
8+
@pagy, @enrollments = pagy(@q.result)
89
authorize @enrollments
910
end
1011

app/models/enrollment.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class Enrollment < ApplicationRecord
1515
extend FriendlyId
1616
friendly_id :to_s, use: :slugged
1717

18+
def self.ransackable_associations(auth_object = nil)
19+
[ "course", "user" ]
20+
end
21+
22+
def self.ransackable_attributes(auth_object = nil)
23+
[ "price", "review", "rating", "updated_at", "created_at" ]
24+
end
25+
1826
def to_s
1927
user.to_s + " " + course.to_s
2028
end

app/views/enrollments/index.html.haml

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,55 @@
77
Enrollments
88
.badge.bg-info
99
= @enrollments.count
10-
10+
%p
11+
%form.row.g-3
12+
= search_form_for @q do |f|
13+
.col-auto
14+
= f.search_field :user_email_cont, placeholder: "User email", class: "form-control form-control-sm"
15+
.col-auto
16+
= f.search_field :course_title_cont, placeholder: "Course title", class: "form-control form-control-sm"
17+
.col-auto
18+
= f.search_field :price_eq, placeholder: "Price", class: "form-control form-control-sm"
19+
.col-auto
20+
= f.search_field :rating_eq, placeholder: "Rating", class: "form-control form-control-sm"
21+
.col-auto
22+
= f.search_field :review_cont, placeholder: "Review", class: "form-control form-control-sm"
23+
.col-auto
24+
= f.submit class: "btn btn-sm btn-secondary"
25+
.col-auto
26+
= link_to enrollments_path, class: "btn btn-sm btn-secondary" do
27+
.fa.fa-refresh
28+
%p
1129
.table-responsive
1230
%table.table.table-bordered.table-striped.table-sm
1331
%thead
1432
%tr
1533
%th ID
34+
%th
35+
= sort_link(@q, :user_email) do
36+
.fa.fa-user
37+
User
38+
%th
39+
= sort_link(@q, :created_at) do
40+
.fa.fa-clock
41+
.fa.fa-lock-open
42+
Bought
1643
%th
17-
.fa.fa-user
18-
User
19-
%th
20-
.fa.fa-clock
21-
.fa.fa-lock-open
22-
Bought
23-
%th
24-
.fa.fa-graduation-cap
25-
Course
44+
= sort_link(@q, :course_title) do
45+
.fa.fa-graduation-cap
46+
Course
2647
%th
27-
.fa.fa-dollar-sign
28-
Price
48+
= sort_link(@q, :price) do
49+
.fa.fa-dollar-sign
50+
Price
2951
%th
30-
.text-warning.fa.fa-clock
31-
Reviewed on
52+
= sort_link(@q, :updated_at) do
53+
.text-warning.fa.fa-clock
54+
Reviewed on
3255
%th
33-
.text-warning.fa.fa-star
34-
Rating
56+
= sort_link(@q, :rating) do
57+
.text-warning.fa.fa-star
58+
Rating
3559
%th
3660
.text-warning.fa.fa-comment
3761
Review

0 commit comments

Comments
 (0)