diff --git a/app/controllers/favorite_groups_controller.rb b/app/controllers/favorite_groups_controller.rb index c1c7f213f..91fed24dd 100644 --- a/app/controllers/favorite_groups_controller.rb +++ b/app/controllers/favorite_groups_controller.rb @@ -47,9 +47,9 @@ class FavoriteGroupsController < ApplicationController def destroy @favorite_group = FavoriteGroup.find(params[:id]) check_write_privilege(@favorite_group) - @favorite_group.destroy - flash[:notice] = "Favorite group deleted" - redirect_to favorite_groups_path + @favorite_group.destroy! + flash[:notice] = "Favorite group deleted" if request.format.html? + respond_with(@favorite_group, location: favorite_groups_path(search: { creator_name: CurrentUser.name })) end def add_post diff --git a/app/models/favorite_group.rb b/app/models/favorite_group.rb index 19ba08b40..fbd3e933d 100644 --- a/app/models/favorite_group.rb +++ b/app/models/favorite_group.rb @@ -25,10 +25,6 @@ class FavoriteGroup < ApplicationRecord where(is_public: true).or(where(creator_id: user.id)) end - def default_order - order(name: :asc) - end - def search(params) q = super q = q.visible(CurrentUser.user) @@ -38,6 +34,17 @@ class FavoriteGroup < ApplicationRecord q = q.name_matches(params[:name_matches]) end + case params[:order] + when "name" + q = q.order(name: :asc, id: :desc) + when "created_at" + q = q.order(id: :desc) + when "post_count" + q = q.order(Arel.sql("cardinality(post_ids) desc")).order(id: :desc) + else + q = q.apply_default_order(params) + end + q.apply_default_order(params) end end diff --git a/app/presenters/user_presenter.rb b/app/presenters/user_presenter.rb index df991576f..48c04953a 100644 --- a/app/presenters/user_presenter.rb +++ b/app/presenters/user_presenter.rb @@ -88,7 +88,7 @@ class UserPresenter end def favorite_group_count(template) - template.link_to(user.favorite_group_count, template.user_favorite_groups_path(user.id)) + template.link_to(user.favorite_group_count, template.favorite_groups_path(search: { creator_name: user.name })) end def comment_count(template) diff --git a/app/views/favorite_groups/destroy.js.erb b/app/views/favorite_groups/destroy.js.erb new file mode 100644 index 000000000..e7d5cc326 --- /dev/null +++ b/app/views/favorite_groups/destroy.js.erb @@ -0,0 +1,2 @@ +$("tr#favorite-group-<%= @favorite_group.id %>").remove(); +Danbooru.notice("Favorite group deleted."); diff --git a/app/views/favorite_groups/index.html.erb b/app/views/favorite_groups/index.html.erb index 7957ca4ac..cd75141c7 100644 --- a/app/views/favorite_groups/index.html.erb +++ b/app/views/favorite_groups/index.html.erb @@ -1,15 +1,35 @@
+ <%= search_form_for(favorite_groups_path) do |f| %> + <%= f.input :name_matches, label: "Name", input_html: { value: params.dig(:search, :name_matches) } %> + <%= f.input :creator_name, label: "Creator", input_html: { value: params.dig(:search, :creator_name), "data-autocomplete": "user" } %> + <%= f.input :order, collection: [%w[Created created_at], %w[Updated updated_at], %w[Name name], %w[Post\ count post_count]], include_blank: true, selected: params.dig(:search, :order) %> + <%= f.submit "Search" %> + <% end %> - <%= table_for @favorite_groups, width: "100%" do |t| %> + <%= table_for @favorite_groups.includes(:creator), width: "100%" do |t| %> <% t.column "Name", {width: "60%"} do |favgroup| %> <%= link_to favgroup.pretty_name, favorite_group_path(favgroup) %> <% if favgroup.post_count > CurrentUser.user.per_page %> <%= link_to "page #{favgroup.last_page}", favorite_group_path(favgroup, :page => favgroup.last_page), :class => "last-page" %> <% end %> <% end %> - <% t.column "Count", {width: "10%"} do |favgroup| %> - <%= favgroup.post_count %> + <% t.column :post_count %> + <% t.column "Creator" do |favgroup| %> + <%= link_to_user favgroup.creator %> + <%= link_to "ยป", favorite_groups_path(search: { creator_name: favgroup.creator.name }) %> + <% end %> + <% t.column "Created" do |favgroup| %> + <%= time_ago_in_words_tagged(favgroup.created_at) %> + <% end %> + <% t.column "Updated" do |favgroup| %> + <%= time_ago_in_words_tagged(favgroup.updated_at) %> + <% end %> + <% t.column column: "control" do |favgroup| %> + <% if favgroup.editable_by?(CurrentUser.user) %> + <%= link_to "Edit", edit_favorite_group_path(favgroup) %> | + <%= link_to "Delete", favorite_group_path(favgroup), method: :delete, remote: true, "data-confirm": "Are you sure you want to delete this favgroup? This cannot be undone." %> + <% end %> <% end %> <% end %> diff --git a/app/views/favorite_groups/new.html.erb b/app/views/favorite_groups/new.html.erb index 74dcbc771..dc7027dd9 100644 --- a/app/views/favorite_groups/new.html.erb +++ b/app/views/favorite_groups/new.html.erb @@ -5,9 +5,9 @@ <%= error_messages_for "favorite_group" %> <%= edit_form_for(@favorite_group) do |f| %> - <%= f.input :name, :as => :string, :required => true %> - <%= f.input :post_ids_string, :label => "Posts" %> - <%= f.button :submit, "Submit" %> + <%= f.input :name, as: :string, required: true %> + <%= f.input :post_ids_string, label: "Posts", as: :text %> + <%= f.submit "Submit" %> <% end %>
diff --git a/app/views/posts/partials/common/_secondary_links.html.erb b/app/views/posts/partials/common/_secondary_links.html.erb index 0a98a31d5..69d4aded9 100644 --- a/app/views/posts/partials/common/_secondary_links.html.erb +++ b/app/views/posts/partials/common/_secondary_links.html.erb @@ -7,7 +7,7 @@ <% end %> <% unless CurrentUser.is_anonymous? %> <%= subnav_link_to "Favorites", posts_path(tags: "ordfav:#{CurrentUser.user.name}") %> - <%= subnav_link_to "Fav groups", user_favorite_groups_path(CurrentUser.id) %> + <%= subnav_link_to "Fav groups", favorite_groups_path(search: { creator_name: CurrentUser.name }) %> <%= subnav_link_to "Saved searches", posts_path(tags: "search:all") %> <% end %> <%= subnav_link_to "Changes", post_versions_path %> diff --git a/app/views/posts/partials/show/_nav_links.html.erb b/app/views/posts/partials/show/_nav_links.html.erb index 78a9da7c2..8a3cfc367 100644 --- a/app/views/posts/partials/show/_nav_links.html.erb +++ b/app/views/posts/partials/show/_nav_links.html.erb @@ -9,7 +9,7 @@ <% end %> <% if CurrentUser.user.favorite_groups.for_post(post.id).present? %> - <%= render "posts/partials/show/favorite_groups", post: post, favgroups: CurrentUser.user.favorite_groups.for_post(post.id) %> + <%= render "posts/partials/show/favorite_groups", post: post, favgroups: CurrentUser.user.favorite_groups.for_post(post.id).order(name: :asc) %> <% end %> <% end %>