Skip to content

Commit

Permalink
Sorting option has been added to the page
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajjulian committed Sep 8, 2024
1 parent 5fb0dae commit a3e7721
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 12 deletions.
17 changes: 16 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ class MoviesController < ApplicationController
before_action :set_movie, only: %i[ show edit update destroy ]

# GET /movies or /movies.json

def index
@movies = Movie.all
# Fetch sorting parameters from the URL
sort_attribute = params[:attribute] || 'title'
sort_order = params[:order] || 'asc'

# Fetch and sort movies based on parameters
@movies = Movie.sorted_by(sort_attribute, sort_order)
end

# GET /movies/1 or /movies/1.json
Expand All @@ -19,6 +25,9 @@ def new
def edit
end

def sort

end
# POST /movies or /movies.json
def create
@movie = Movie.new(movie_params)
Expand Down Expand Up @@ -67,4 +76,10 @@ def set_movie
def movie_params
params.require(:movie).permit(:title, :rating, :description, :release_date)
end

def sort
respond_to do |format|
format.html { redirect_to movies_url, notice: "Sorting is yet to be done" }
end
end
end
15 changes: 14 additions & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
class Movie < ActiveRecord::Base
end
# Class method to sort movies based on the attribute and order
def self.sorted_by(attribute, order)
valid_attributes = %w[title rating release_date] # Allowed attributes for sorting
valid_order = %w[asc desc] # Allowed orders

# Default values if parameters are invalid
attribute = 'title' unless valid_attributes.include?(attribute)
order = 'asc' unless valid_order.include?(order)

# Apply sorting using ActiveRecord's `order` method
order("#{attribute} #{order}")
end
end

8 changes: 4 additions & 4 deletions app/views/movies/_movie.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<div id="<%= dom_id movie %>">

<tr>
<td> <%= movie.title %> </td>
<td> <%= movie.rating %> </td>
<td> <%= movie.release_date %> </td>
<td> <%= link_to "Click here", movie %> </td>
<td> <%= movie.title %> </td>
<td> <%= movie.rating %> </td>
<td> <%= movie.release_date %> </td>
<td> <%= link_to "Click here", movie %> </td>
</tr>

</div>
11 changes: 11 additions & 0 deletions app/views/movies/_movieshow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

<div id="<%= dom_id movieshow %>">


<p> <%= "Title: #{movieshow.title} " %> </p>
<p> <%= "Rating: #{movieshow.rating} " %> </p>
<p> <%= "Release Date: #{movieshow.release_date} " %> </p>
<p> <%= "Description: #{movieshow.description} " %> </p>


</div>
51 changes: 47 additions & 4 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<% content_for :title, "Movies" %>

<h1>Movies</h1>


<head>

<style>
Expand All @@ -23,16 +25,55 @@
font-weight: bold;
}

.button {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
color: #fff;
background-color: #007bff;
border: none;
border-radius: 5px;
text-align: center;
cursor: pointer;
}

.button:hover {
background-color: #0056b3;
}
</style
</head>

<% if params[:attribute].present? && params[:order].present? %>
<div id="sort-message" class="alert">
Items are sorted by <%= params[:attribute].humanize %> in <%= params[:order].humanize %> order.
</div>
<% end %>

<table>
<thead>
<tr>
<th> Title </th>
<th> Rating </th>
<th> Release date </th>
<th> Show this movie </th>

<th>
<%= link_to 'Title', movies_path(attribute: 'title', order: params[:order] == 'asc' ? 'desc' : 'asc') %>
<% if params[:attribute] == 'title' %>
<%= params[:order] == 'asc' ? '▲' : '▼' %>
<% end %>
</th>
<th>
<%= link_to 'Rating', movies_path(attribute: 'rating', order: params[:order] == 'asc' ? 'desc' : 'asc') %>
<% if params[:attribute] == 'rating' %>
<%= params[:order] == 'asc' ? '▲' : '▼' %>
<% end %>
</th>
<th>
<%= link_to 'Release Date', movies_path(attribute: 'release_date', order: params[:order] == 'asc' ? 'desc' : 'asc') %>
<% if params[:attribute] == 'release_date' %>
<%= params[:order] == 'asc' ? '▲' : '▼' %>
<% end %>
</th>


<th> Show this movie </th>
</tr>
</thead>

Expand All @@ -54,3 +95,5 @@





6 changes: 4 additions & 2 deletions app/views/movies/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<p style="color: green"><%= notice %></p>

<%= render @movie %>
<%= render partial: 'movieshow', locals: { movieshow: @movie } %>

<div>
<br>
<%= link_to "Edit this movie", edit_movie_path(@movie) %> |
<%= link_to "Back to movies", movies_path %>
<br>
<br>
<%= button_to "Destroy this movie", @movie, method: :delete %>
</div>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
resources :movies
root :to => redirect('/movies')
end



0 comments on commit a3e7721

Please sign in to comment.