-
Notifications
You must be signed in to change notification settings - Fork 25
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
1 parent
b723dfb
commit 28389d7
Showing
10 changed files
with
204 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
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,46 @@ | ||
class HelpLinksController < ApplicationController | ||
require_permission :can_edit_help_links? | ||
|
||
def index | ||
@links = HelpLink.for_editing | ||
end | ||
|
||
def create | ||
HelpLink.transaction do | ||
next_ordering = (HelpLink.maximum(:ordering) || 0) + 1 | ||
HelpLink.create!(label: params[:label], url: params[:url], ordering: next_ordering, visible: false) | ||
end | ||
|
||
redirect_to help_links_path, flash: { success: "Help link created!" } | ||
end | ||
|
||
def destroy | ||
HelpLink.find(params[:id]).destroy! | ||
redirect_to help_links_path, flash: { success: "Help link deleted!" } | ||
end | ||
|
||
def toggle_visibility | ||
link = HelpLink.find(params[:id]) | ||
link.visible = !link.visible | ||
link.save! | ||
redirect_to help_links_path | ||
end | ||
|
||
def move_up | ||
HelpLink.transaction do | ||
link = HelpLink.find(params[:id]) | ||
link.move_up | ||
end | ||
|
||
redirect_to help_links_path | ||
end | ||
|
||
def move_down | ||
HelpLink.transaction do | ||
link = HelpLink.find(params[:id]) | ||
link.move_down | ||
end | ||
|
||
redirect_to help_links_path | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class HelpLink < ApplicationRecord | ||
def self.for_users | ||
where(visible: true).order(ordering: :asc) | ||
end | ||
|
||
def self.for_editing | ||
order(ordering: :asc) | ||
end | ||
|
||
def decrement_ordering | ||
self.ordering = ordering - 1 | ||
save! | ||
end | ||
|
||
def increment_ordering | ||
self.ordering = ordering + 1 | ||
save! | ||
end | ||
|
||
def move_up | ||
previous_ordering = HelpLink.where("ordering < ?", ordering).maximum(:ordering) | ||
return unless previous_ordering | ||
HelpLink.where("ordering > ?", ordering).order(ordering: :desc).to_a.each(&:increment_ordering) | ||
link = HelpLink.find_by(ordering: previous_ordering) | ||
link.ordering = ordering + 1 | ||
link.save! | ||
end | ||
|
||
def move_down | ||
next_ordering = HelpLink.where("ordering > ?", ordering).minimum(:ordering) | ||
return unless next_ordering | ||
HelpLink.where("ordering < ?", ordering).order(ordering: :asc).to_a.each(&:decrement_ordering) | ||
link = HelpLink.find_by(ordering: next_ordering) | ||
link.ordering = ordering - 1 | ||
link.save! | ||
end | ||
|
||
def visibility_icon | ||
if visible | ||
"eye-open" | ||
else | ||
"eye-close" | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<% content_for :title, "Help Links" %> | ||
<% content_for :content do %> | ||
<%= form_for HelpLink.new, data: { live_guarded: true }, method: :post do |f| %> | ||
<div class="row"> | ||
<div class="col-xs-6"> | ||
<div class="form-group"> | ||
<label class="control-label" for="help-link-label">Help Link Label</label> | ||
<input type="text" class="form-control" id="help-link-label" name="label" placeholder="Label for the help link" data-guard="required" autocomplete="off" /> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-xs-6"> | ||
<div class="form-group"> | ||
<label class="control-label" for="help-link-url">Help Link URL</label> | ||
<input type="text" class="form-control" id="help-link-url" name="url" placeholder="URL for the help link" data-guard="required" autocomplete="off" /> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<%= f.submit "Save", class: "btn btn-primary" %> | ||
<% end %> | ||
|
||
<hr /> | ||
|
||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Label</th> | ||
<th>URL</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @links.each do |help_link| %> | ||
<tr> | ||
<td><%= help_link.label %></td> | ||
<td><%= link_to help_link.url, help_link.url %></td> | ||
|
||
<td> | ||
<%= button_to toggle_visibility_help_link_path(help_link), method: :post, class: "btn btn-xs btn-default", form_class: "inline-form" do %><span class="glyphicon glyphicon-<%= help_link.visibility_icon %>"></span><% end %> | ||
<%= button_to move_up_help_link_path(help_link), method: :post, class: "btn btn-xs btn-default", form_class: "inline-form" do %><span class="glyphicon glyphicon-arrow-up"></span><% end %> | ||
<%= button_to move_down_help_link_path(help_link), method: :post, class: "btn btn-xs btn-default", form_class: "inline-form" do %><span class="glyphicon glyphicon-arrow-down"></span><% end %> | ||
<%= button_to help_link_path(help_link), method: :delete, class: "btn btn-xs btn-danger", form_class: "inline-form", data: confirm(title: "Delete Help Link") do %><span class="glyphicon glyphicon-trash"></span><% end %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateHelpLinks < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :help_links do |t| | ||
t.string :label, null: false | ||
t.string :url, null: false | ||
t.integer :ordering, null: false | ||
t.boolean :visible, null: false | ||
t.timestamps null: false | ||
t.index :ordering, unique: true | ||
end | ||
end | ||
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