favorite and filter documents also

This commit is contained in:
Andrew Brown 2020-01-28 11:56:38 -06:00
parent 80396654c6
commit e1260ed570
6 changed files with 31 additions and 5 deletions

View File

@ -77,9 +77,13 @@ $(document).ready(function () {
toggle.attr('data-tooltip', 'Unfavorite this page');
}
$.ajax({
post_url = (content_class == 'documents'
? '/documents/' + content_id + '/toggle_favorite'
: "/plan/" + content_class + "/" + content_id + "/toggle_favorite");
$.ajax({
type: "POST",
url: "/plan/" + content_class + "/" + content_id + "/toggle_favorite",
url: post_url,
data: { id: content_id },
success: function () {
// console.log("success!");

View File

@ -172,6 +172,17 @@ class DocumentsController < ApplicationController
end
end
def toggle_favorite
document = Document.with_deleted.find_or_initialize_by(id: params[:id])
unless document.updatable_by?(current_user)
flash[:notice] = "You don't have permission to edit that!"
return redirect_back fallback_location: document
end
document.update!(favorite: !document.favorite)
end
def destroy
if current_user.can_delete?(@document)
@document.destroy

View File

@ -27,10 +27,12 @@
</div>
<div class="card-action">
<%= link_to edit_polymorphic_path(content), class: 'green-text right', target: content.is_a?(Document) ? '_new' : '_self' do %>
<i class="material-icons left"><%= content_type.icon %></i> Edit
<i class="material-icons left"><%= content_type.icon %></i>
Edit
<% end %>
<%= link_to polymorphic_path(content), class: 'blue-text text-lighten-1' do %>
<i class="material-icons left"><%= content_type.icon %></i> View
<i class="material-icons left"><%= content_type.icon %></i>
View
<% end %>
</div>
<div class="card-reveal">

View File

@ -27,9 +27,12 @@ Rails.application.routes.draw do
get '/queue_analysis', to: 'documents#queue_analysis', on: :member
post '/link_entity', to: 'documents#link_entity', on: :collection
post :toggle_favorite, on: :member
# todo these routes don't belong here and make for awfully weird urls (/documents/:analysis_id/destroy, etc)
get '/destroy_analysis', to: 'documents#destroy_analysis', on: :member
get '/destroy_entity', to: 'documents#destroy_document_entity', on: :member
end
scope '/my' do

View File

@ -0,0 +1,5 @@
class AddFavoriteFlagToDocuments < ActiveRecord::Migration[6.0]
def change
add_column :documents, :favorite, :boolean
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_01_28_161646) do
ActiveRecord::Schema.define(version: 2020_01_28_174509) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
@ -914,6 +914,7 @@ ActiveRecord::Schema.define(version: 2020_01_28_161646) do
t.text "synopsis"
t.datetime "deleted_at"
t.integer "universe_id"
t.boolean "favorite"
t.index ["universe_id", "deleted_at"], name: "index_documents_on_universe_id_and_deleted_at"
t.index ["universe_id"], name: "index_documents_on_universe_id"
t.index ["user_id"], name: "index_documents_on_user_id"