Skip to content

Commit

Permalink
Help links
Browse files Browse the repository at this point in the history
  • Loading branch information
smellsblue committed Oct 24, 2023
1 parent b723dfb commit 28389d7
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/move_to_ui_gem.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* This is a temporary placeholder for CSS
before it is moved to the gratefulgarment-ui gem */

.inline-form {
display: inline;
}

.disabled-title-wrapper {
cursor: not-allowed;
display: inline-block;
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/help_links_controller.rb
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
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module ApplicationHelper
def help_links
@help_links ||= HelpLink.for_users.to_a
end

def bootstrap_class_for(flash_type)
case flash_type.to_sym
when :success
Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/users/report_manipulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Users
module ReportManipulator
extend ActiveSupport::Concern

def can_edit_help_links?
super_admin?
end

def can_view_reports?
super_admin? || report_admin?
end
Expand Down
45 changes: 45 additions & 0 deletions app/models/help_link.rb
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
17 changes: 17 additions & 0 deletions app/views/common/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@
</li>
<% end %>
<% if help_links.present? || current_user.can_edit_help_links? %>
<li class="dropdown <%= "active" if active == "help_links" %>">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Help <span class="caret"></span></a>

<ul class="dropdown-menu">
<% help_links.each do |help_link| %>
<li><%= link_to help_link.label, help_link.url %></li>
<% end %>
<% if current_user.can_edit_help_links? %>
<li role="separator" class="divider"></li>
<li><%= link_to "Edit Help Links", help_links_path %></li>
<% end %>
</ul>
</li>
<% end %>

<li class="visible-xs"><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
</ul>

Expand Down
53 changes: 53 additions & 0 deletions app/views/help_links/index.html.erb
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 %>
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
end
end

resources :help_links, only: %i[index create destroy] do
member do
post :toggle_visibility
post :move_up
post :move_down
end
end

resources :items, path: "/inventory" do
collection do
get :deleted
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20231024001352_create_help_links.rb
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
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_09_18_032907) do
ActiveRecord::Schema.define(version: 2023_10_24_001352) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -153,6 +153,16 @@
t.datetime "updated_at", null: false
end

create_table "help_links", force: :cascade do |t|
t.string "label", null: false
t.string "url", null: false
t.integer "ordering", null: false
t.boolean "visible", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["ordering"], name: "index_help_links_on_ordering", unique: true
end

create_table "inventory_reconciliations", id: :serial, force: :cascade do |t|
t.string "title"
t.integer "user_id", null: false
Expand Down

0 comments on commit 28389d7

Please sign in to comment.