-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module StarRatingFilter | ||
|
||
# Location of the star images from root of website | ||
Star_imagesLoc = "/images" | ||
|
||
# The format of the img tag used by % method | ||
Star_imageTag = "<img src=\"#{Star_imagesLoc}/%s\" alt=\"%s\" />" | ||
|
||
# Displays the rating as a series of stars | ||
def star_rating(rating) | ||
|
||
wholeStars = rating.floor | ||
wholeStars += 1 if (rating - wholeStars > 0.5) | ||
|
||
halfStar = (rating - wholeStars == 0.5 ? 1 : 0) | ||
clearStars = 5 - (wholeStars + halfStar) | ||
|
||
ratingAltText = "%.1f/5.0" % [rating] | ||
|
||
htmlOutput = String.new | ||
wholeStars.times do | ||
htmlOutput += Star_imageTag % ["star_filled.png", "#{ratingAltText}"] | ||
ratingAltText = "" | ||
end | ||
|
||
if (halfStar == 1) | ||
htmlOutput += Star_imageTag % ["star_half.png", "#{ratingAltText}"] | ||
ratingAltText = "" | ||
end | ||
|
||
clearStars.times do | ||
htmlOutput += Star_imageTag % ["star_clear.png", "#{ratingAltText}"] | ||
ratingAltText = "" | ||
end | ||
|
||
return htmlOutput | ||
end | ||
|
||
end | ||
|
||
Liquid::Template.register_filter(StarRatingFilter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.