Skip to content

Use ransack on enrollments table #34

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

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
3 changes: 2 additions & 1 deletion app/controllers/enrollments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class EnrollmentsController < ApplicationController

def index
# @enrollments = Enrollment.all
@pagy, @enrollments = pagy(Enrollment.all)
@q = Enrollment.ransack(params[:q])
@pagy, @enrollments = pagy(@q.result)
authorize @enrollments
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/enrollment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class Enrollment < ApplicationRecord
extend FriendlyId
friendly_id :to_s, use: :slugged

def self.ransackable_associations(auth_object = nil)
[ "course", "user" ]
end

def self.ransackable_attributes(auth_object = nil)
[ "price", "review", "rating", "updated_at", "created_at" ]
end

def to_s
user.to_s + " " + course.to_s
end
Expand Down
56 changes: 40 additions & 16 deletions app/views/enrollments/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,55 @@
Enrollments
.badge.bg-info
= @enrollments.count

%p
%form.row.g-3
= search_form_for @q do |f|
.col-auto
= f.search_field :user_email_cont, placeholder: "User email", class: "form-control form-control-sm"
.col-auto
= f.search_field :course_title_cont, placeholder: "Course title", class: "form-control form-control-sm"
.col-auto
= f.search_field :price_eq, placeholder: "Price", class: "form-control form-control-sm"
.col-auto
= f.search_field :rating_eq, placeholder: "Rating", class: "form-control form-control-sm"
.col-auto
= f.search_field :review_cont, placeholder: "Review", class: "form-control form-control-sm"
.col-auto
= f.submit class: "btn btn-sm btn-secondary"
.col-auto
= link_to enrollments_path, class: "btn btn-sm btn-secondary" do
.fa.fa-refresh
%p
.table-responsive
%table.table.table-bordered.table-striped.table-sm
%thead
%tr
%th ID
%th
= sort_link(@q, :user_email) do
.fa.fa-user
User
%th
= sort_link(@q, :created_at) do
.fa.fa-clock
.fa.fa-lock-open
Bought
%th
.fa.fa-user
User
%th
.fa.fa-clock
.fa.fa-lock-open
Bought
%th
.fa.fa-graduation-cap
Course
= sort_link(@q, :course_title) do
.fa.fa-graduation-cap
Course
%th
.fa.fa-dollar-sign
Price
= sort_link(@q, :price) do
.fa.fa-dollar-sign
Price
%th
.text-warning.fa.fa-clock
Reviewed on
= sort_link(@q, :updated_at) do
.text-warning.fa.fa-clock
Reviewed on
%th
.text-warning.fa.fa-star
Rating
= sort_link(@q, :rating) do
.text-warning.fa.fa-star
Rating
%th
.text-warning.fa.fa-comment
Review
Expand Down
Loading