Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #408 from rubytoolbox/co-comparison
Project Comparisons
- Loading branch information
Showing
20 changed files
with
431 additions
and
18 deletions.
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
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
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
class ComparisonsController < ApplicationController | ||
def show | ||
@projects = Project.where(permalink: requested_project_permalinks) | ||
.for_display(forks: true) | ||
.includes_associations | ||
.order(current_order.sql) | ||
.limit(50) | ||
@display_mode = display_mode | ||
enforce_canonical_query | ||
end | ||
|
||
private | ||
|
||
def enforce_canonical_query | ||
@expected_ids = @projects.map(&:permalink).sort.join(",") | ||
query_string = Rack::Utils.parse_nested_query(request.query_string).slice("display", "order").to_query | ||
destination = if query_string.present? | ||
comparison_path(@expected_ids) + "?#{query_string}" | ||
else | ||
comparison_path(@expected_ids) | ||
end | ||
|
||
redirect_to destination if selection_is_unordered? | ||
end | ||
|
||
def selection_is_unordered? | ||
params[:add].present? || (requested_project_permalinks.try(:join, ",").presence != @expected_ids.presence) | ||
end | ||
|
||
def requested_project_permalinks | ||
(params[:id].presence || "").split(",") + [params[:add]].map { |id| id.try(:strip).presence }.compact | ||
end | ||
|
||
def display_mode | ||
DisplayMode.new params[:display], default: "table" | ||
end | ||
|
||
def current_order | ||
@current_order ||= Project::Order.new order: params[:order] | ||
end | ||
helper_method :current_order | ||
end |
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
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
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
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
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,68 @@ | ||
- content_for :title, "Compare Projects" | ||
|
||
.hero | ||
section.section: .container | ||
.columns: .column | ||
h2 | ||
a href=comparison_path("") | ||
span Compare projects | ||
- if @projects.size < 2 | ||
.description | ||
markdown: | ||
Project comparisons allow you to view any selection of projects side by side just like they're shown on regular categories or in search results. **[You can try out an example](/compare/hanami,merb,rails,sinatra)** or start yourself by adding a library to the comparison via the input below. | ||
|
||
.columns: .column | ||
.tags | ||
- @expected_ids.split(",").each do |permalink| | ||
span.tag.is-primary.is-medium | ||
span= permalink | ||
a.delete.is-small href=(comparison_path((@expected_ids.split(",") - [permalink]).join(",")) + "?#{current_display_settings_query_string}") | ||
|
||
= form_tag request.path, method: :get, autocomplete: "off", class: "autocomplete-form" do | ||
.field.has-addons.has-addons-centered | ||
// Persist display settings across submissions | ||
- current_display_settings.compact.each do |name, value| | ||
input type="hidden" name=name value=value | ||
|
||
.control.is-expanded | ||
input.input.autocomplete-comparison autofocus="autofocus" placeholder="Type a project name to add it to the comparison" type="text" name="add" value="" | ||
.control | ||
button.button type="submit" | ||
span.icon: i.fa.fa-plus | ||
span Add to comparison | ||
|
||
/ Our autocomplete library interferes with the enter key on the input field - | ||
/ it should be possible to just type a name and hit enter without interacting | ||
/ with the autocompletion in any way. | ||
javascript: | ||
(function() { | ||
var submitComparisonForm = function() { document.querySelector("form.autocomplete-form").submit() } | ||
document.querySelector("input.autocomplete-comparison").addEventListener("keydown", function(event) { | ||
if (event.key === "Enter") { submitComparisonForm(); } | ||
}); | ||
new autoComplete({ | ||
minChars: 1, | ||
selector: "input.autocomplete-comparison", | ||
source: function(term, callback) { | ||
fetch("/search/by_name?q=" + term) | ||
.then(function(response) { | ||
response.json().then(function(data) { | ||
callback(data); | ||
}); | ||
}); | ||
}, | ||
onSelect: submitComparisonForm | ||
}); | ||
})(); | ||
- if @projects.any? | ||
section.section: .container | ||
.level | ||
.level-left | ||
.level-right | ||
.level-item= project_display_picker @display_mode | ||
.level-item= project_order_dropdown current_order | ||
|
||
= render "projects/listing", projects: @projects, show_categories: false, display_mode: @display_mode |
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
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,8 @@ | ||
.columns: .column | ||
.buttons.is-right | ||
a.button.is-outlined href="/projects/#{project.permalink}" | ||
span.icon: i.fa.fa-plus | ||
span Show more project details | ||
a.button.is-outlined href=comparison_path(project.permalink) | ||
span.icon: i.fa.fa-arrows-h | ||
span Compare |
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
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
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
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
Oops, something went wrong.