diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d4280030d..ccc728f60 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,28 +25,18 @@ class ApplicationController < ActionController::Base private def respond_with(subject, *options, &block) - @current_item = subject - if params[:action] == "index" && is_redirect?(subject) redirect_to_show(subject) - else - super + return end - end - def model_includes(params, model = nil) - if params[:only] && ["json", "xml"].include?(params[:format]) - includes_array = ParameterBuilder.includes_parameters(params[:only], model_name) - elsif params[:action] == "index" - includes_array = default_includes(params) - else - includes_array = [] + if request.format.json? || request.format.xml? + associations = ParameterBuilder.includes_parameters(params[:only], model_name) + subject = subject.includes(associations) end - includes_array - end - def default_includes(*) - [] + @current_item = subject + super end def model_name diff --git a/app/controllers/artist_commentaries_controller.rb b/app/controllers/artist_commentaries_controller.rb index 5308325ff..df71626df 100644 --- a/app/controllers/artist_commentaries_controller.rb +++ b/app/controllers/artist_commentaries_controller.rb @@ -3,7 +3,9 @@ class ArtistCommentariesController < ApplicationController before_action :member_only, only: [:create_or_update, :revert] def index - @commentaries = ArtistCommentary.paginated_search(params).includes(model_includes(params)) + @commentaries = ArtistCommentary.paginated_search(params) + @commentaries = @commentaries.includes(post: :uploader) if request.format.html? + respond_with(@commentaries) end @@ -36,14 +38,6 @@ class ArtistCommentariesController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [{post: [:uploader]}] - end - end - def commentary_params params.fetch(:artist_commentary, {}).except(:post_id).permit(%i[ original_description original_title translated_description translated_title diff --git a/app/controllers/artist_commentary_versions_controller.rb b/app/controllers/artist_commentary_versions_controller.rb index 170e339aa..cee5140c2 100644 --- a/app/controllers/artist_commentary_versions_controller.rb +++ b/app/controllers/artist_commentary_versions_controller.rb @@ -2,7 +2,9 @@ class ArtistCommentaryVersionsController < ApplicationController respond_to :html, :xml, :json def index - @commentary_versions = ArtistCommentaryVersion.paginated_search(params).includes(model_includes(params)) + @commentary_versions = ArtistCommentaryVersion.paginated_search(params) + @commentary_versions = @commentary_versions.includes(:updater, post: :uploader) if request.format.html? + respond_with(@commentary_versions) end @@ -12,14 +14,4 @@ class ArtistCommentaryVersionsController < ApplicationController format.html { redirect_to artist_commentary_versions_path(search: { post_id: @commentary_version.post_id }) } end end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [{post: [:uploader]}, :updater] - end - end end diff --git a/app/controllers/artist_urls_controller.rb b/app/controllers/artist_urls_controller.rb index 235c7b984..56bbec746 100644 --- a/app/controllers/artist_urls_controller.rb +++ b/app/controllers/artist_urls_controller.rb @@ -3,7 +3,9 @@ class ArtistUrlsController < ApplicationController before_action :member_only, except: [:index] def index - @artist_urls = ArtistUrl.paginated_search(params).includes(model_includes(params)) + @artist_urls = ArtistUrl.paginated_search(params) + @artist_urls = @artist_urls.includes(:artist) if request.format.html? + respond_with(@artist_urls) end @@ -15,14 +17,6 @@ class ArtistUrlsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:artist] - end - end - def artist_url_params permitted_params = %i[is_active] diff --git a/app/controllers/artist_versions_controller.rb b/app/controllers/artist_versions_controller.rb index 4fbef5fc0..54262e560 100644 --- a/app/controllers/artist_versions_controller.rb +++ b/app/controllers/artist_versions_controller.rb @@ -2,7 +2,9 @@ class ArtistVersionsController < ApplicationController respond_to :html, :xml, :json def index - @artist_versions = ArtistVersion.paginated_search(params).includes(model_includes(params)) + @artist_versions = ArtistVersion.paginated_search(params) + @artist_versions = @artist_versions.includes(:updater, artist: :urls) if request.format.html? + respond_with(@artist_versions) end @@ -12,14 +14,4 @@ class ArtistVersionsController < ApplicationController format.html { redirect_to artist_versions_path(search: { artist_id: @artist_version.artist_id }) } end end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:updater, {artist: [:urls]}] - end - end end diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index d409718da..40774455c 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -30,8 +30,9 @@ class ArtistsController < ApplicationController def index # XXX params[:search][:name] = params.delete(:name) if params[:name] + @artists = Artist.paginated_search(params) + @artists = @artists.includes(:urls, :tag) if request.format.html? - @artists = Artist.paginated_search(params).includes(model_includes(params)) respond_with(@artists) end @@ -78,14 +79,6 @@ class ArtistsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:urls, :tag] - end - end - def item_matches_params(artist) if params[:search][:any_name_or_url_matches] artist.name == Artist.normalize_name(params[:search][:any_name_or_url_matches]) diff --git a/app/controllers/bans_controller.rb b/app/controllers/bans_controller.rb index 9b59c9e6e..2e7f45411 100644 --- a/app/controllers/bans_controller.rb +++ b/app/controllers/bans_controller.rb @@ -12,7 +12,9 @@ class BansController < ApplicationController end def index - @bans = Ban.paginated_search(params, count_pages: true).includes(model_includes(params)) + @bans = Ban.paginated_search(params, count_pages: true) + @bans = @bans.includes(:user, :banner) if request.format.html? + respond_with(@bans) end @@ -48,14 +50,6 @@ class BansController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user, :banner] - end - end - def ban_params(context) permitted_params = %i[reason duration expires_at] permitted_params += %i[user_id user_name] if context == :create diff --git a/app/controllers/bulk_update_requests_controller.rb b/app/controllers/bulk_update_requests_controller.rb index ce93af08e..b7febed42 100644 --- a/app/controllers/bulk_update_requests_controller.rb +++ b/app/controllers/bulk_update_requests_controller.rb @@ -41,20 +41,13 @@ class BulkUpdateRequestsController < ApplicationController end def index - @bulk_update_requests = BulkUpdateRequest.paginated_search(params, count_pages: true).includes(model_includes(params)) + @bulk_update_requests = BulkUpdateRequest.paginated_search(params, count_pages: true) + @bulk_update_requests = @bulk_update_requests.includes(:user, :approver, :forum_topic, forum_post: [:votes]) if request.format.html? respond_with(@bulk_update_requests) end private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user, :approver, :forum_topic, {forum_post: [:votes]}] - end - end - def load_bulk_update_request @bulk_update_request = BulkUpdateRequest.find(params[:id]) end diff --git a/app/controllers/comment_votes_controller.rb b/app/controllers/comment_votes_controller.rb index 98ce24d29..47f82fc90 100644 --- a/app/controllers/comment_votes_controller.rb +++ b/app/controllers/comment_votes_controller.rb @@ -5,7 +5,8 @@ class CommentVotesController < ApplicationController rescue_with CommentVote::Error, ActiveRecord::RecordInvalid, status: 422 def index - @comment_votes = CommentVote.paginated_search(params, count_pages: true).includes(model_includes(params)) + @comment_votes = CommentVote.paginated_search(params, count_pages: true) + @comment_votes = @comment_votes.includes(:user, comment: [:creator, post: [:uploader]]) if request.format.html? respond_with(@comment_votes) end @@ -20,14 +21,4 @@ class CommentVotesController < ApplicationController @comment.unvote! respond_with(@comment) end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user, {comment: [:creator, {post: [:uploader]}]}] - end - end end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 6c44c448b..fba277533 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -74,18 +74,6 @@ class CommentsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - elsif params[:format] == "atom" - [:creator, :post] - else - includes_array = [:creator, :updater, {post: [:uploader]}] - includes_array << :votes if CurrentUser.is_member? - includes_array - end - end - def index_for_post @post = Post.find(params[:post_id]) @comments = @post.comments @@ -95,14 +83,24 @@ class CommentsController < ApplicationController def index_by_post @posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC NULLS LAST").paginate(params[:page], :limit => 5, :search_count => params[:search]) - @posts = @posts.includes(comments: [:creator]) - @posts = @posts.includes(comments: [:votes]) if CurrentUser.is_member? + if request.format.html? + @posts = @posts.includes(comments: [:creator]) + @posts = @posts.includes(comments: [:votes]) if CurrentUser.is_member? + end respond_with(@posts) end def index_by_comment - @comments = Comment.paginated_search(params).includes(model_includes(params)) + @comments = Comment.paginated_search(params) + + if request.format.atom? + @comments = @comments.includes(:creator, :post) + elsif request.format.html? + @comments = @comments.includes(:creator, :updater, post: :uploader) + @comments = @comments.includes(:votes) if CurrentUser.is_member? + end + respond_with(@comments) end diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index 0b3af5812..12abbea64 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -15,7 +15,9 @@ class DmailsController < ApplicationController end def index - @dmails = Dmail.visible.paginated_search(params, defaults: { folder: "received" }, count_pages: true).includes(model_includes(params)) + @dmails = Dmail.visible.paginated_search(params, defaults: { folder: "received" }, count_pages: true) + @dmails = @dmails.includes(:owner, :to, :from) if request.format.html? + respond_with(@dmails) end @@ -51,14 +53,6 @@ class DmailsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:owner, :to, :from] - end - end - def check_show_privilege(dmail) raise User::PrivilegeError unless dmail.visible_to?(CurrentUser.user, params[:key]) end diff --git a/app/controllers/dtext_links_controller.rb b/app/controllers/dtext_links_controller.rb index 503777c57..2d7d42489 100644 --- a/app/controllers/dtext_links_controller.rb +++ b/app/controllers/dtext_links_controller.rb @@ -2,17 +2,9 @@ class DtextLinksController < ApplicationController respond_to :html, :xml, :json def index - @dtext_links = DtextLink.paginated_search(params).includes(model_includes(params)) + @dtext_links = DtextLink.paginated_search(params) + @dtext_links = @dtext_links.includes(:model) if request.format.html? + respond_with(@dtext_links) end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:model] - end - end end diff --git a/app/controllers/favorite_groups_controller.rb b/app/controllers/favorite_groups_controller.rb index b870e73f7..7f08bae11 100644 --- a/app/controllers/favorite_groups_controller.rb +++ b/app/controllers/favorite_groups_controller.rb @@ -4,7 +4,9 @@ class FavoriteGroupsController < ApplicationController def index params[:search][:creator_id] ||= params[:user_id] - @favorite_groups = FavoriteGroup.paginated_search(params).includes(model_includes(params)) + @favorite_groups = FavoriteGroup.paginated_search(params) + @favorite_groups = @favorite_groups.includes(:creator) if request.format.html? + respond_with(@favorite_groups) end @@ -61,14 +63,6 @@ class FavoriteGroupsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator] - end - end - def check_write_privilege(favgroup) raise User::PrivilegeError unless favgroup.editable_by?(CurrentUser.user) end diff --git a/app/controllers/forum_post_votes_controller.rb b/app/controllers/forum_post_votes_controller.rb index b95561c5e..150a73608 100644 --- a/app/controllers/forum_post_votes_controller.rb +++ b/app/controllers/forum_post_votes_controller.rb @@ -3,7 +3,9 @@ class ForumPostVotesController < ApplicationController before_action :member_only, only: [:create, :destroy] def index - @forum_post_votes = ForumPostVote.paginated_search(params, count_pages: true).includes(model_includes(params)) + @forum_post_votes = ForumPostVote.paginated_search(params, count_pages: true) + @forum_post_votes = @forum_post_votes.includes(:creator, forum_post: [:creator, :topic]) if request.format.html? + respond_with(@forum_post_votes) end @@ -21,14 +23,6 @@ class ForumPostVotesController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator, {forum_post: [:topic]}] - end - end - def forum_post_vote_params params.fetch(:forum_post_vote, {}).permit(:score) end diff --git a/app/controllers/forum_posts_controller.rb b/app/controllers/forum_posts_controller.rb index d307dbb60..ab6133d7f 100644 --- a/app/controllers/forum_posts_controller.rb +++ b/app/controllers/forum_posts_controller.rb @@ -24,7 +24,9 @@ class ForumPostsController < ApplicationController end def index - @forum_posts = ForumPost.paginated_search(params).includes(model_includes(params)) + @forum_posts = ForumPost.paginated_search(params) + @forum_posts = @forum_posts.includes(:topic, :creator) if request.format.html? + respond_with(@forum_posts) end @@ -68,14 +70,6 @@ class ForumPostsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:topic, :creator] - end - end - def load_post @forum_post = ForumPost.find(params[:id]) @forum_topic = @forum_post.topic diff --git a/app/controllers/forum_topics_controller.rb b/app/controllers/forum_topics_controller.rb index 8bb747164..4e7346101 100644 --- a/app/controllers/forum_topics_controller.rb +++ b/app/controllers/forum_topics_controller.rb @@ -23,7 +23,13 @@ class ForumTopicsController < ApplicationController params[:search][:order] ||= "sticky" if request.format.html? params[:limit] ||= 40 - @forum_topics = ForumTopic.paginated_search(params).includes(model_includes(params)) + @forum_topics = ForumTopic.paginated_search(params) + + if request.format.atom? + @forum_topics = @forum_topics.includes(:creator, :original_post) + elsif request.format.html? + @forum_topics = @forum_topics.includes(:creator, :updater, :forum_topic_visit_by_current_user) + end respond_with(@forum_topics) end @@ -32,9 +38,15 @@ class ForumTopicsController < ApplicationController if request.format.html? @forum_topic.mark_as_read!(CurrentUser.user) end + @forum_posts = ForumPost.search(:topic_id => @forum_topic.id).reorder("forum_posts.id").paginate(params[:page]) - @forum_posts = @forum_posts.includes(:creator, :bulk_update_request) - @forum_posts = @forum_posts.reverse_order.load if request.format.atom? + + if request.format.atom? + @forum_posts = @forum_posts.reverse_order.load + elsif request.format.html? + @forum_posts = @forum_posts.includes(:creator, :bulk_update_request) + end + respond_with(@forum_topic) end @@ -77,16 +89,6 @@ class ForumTopicsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - elsif params[:format] == "atom" - [:creator, :original_post] - else - [:creator, :updater, :forum_topic_visit_by_current_user] - end - end - def normalize_search if params[:title_matches] params[:search] ||= {} diff --git a/app/controllers/ip_addresses_controller.rb b/app/controllers/ip_addresses_controller.rb index be9f67c87..d111f7946 100644 --- a/app/controllers/ip_addresses_controller.rb +++ b/app/controllers/ip_addresses_controller.rb @@ -3,30 +3,16 @@ class IpAddressesController < ApplicationController before_action :moderator_only def index - @ip_addresses = IpAddress.visible(CurrentUser.user).paginated_search(params).includes(model_includes(params)) + @ip_addresses = IpAddress.visible(CurrentUser.user).paginated_search(params) if search_params[:group_by] == "ip_addr" @ip_addresses = @ip_addresses.group_by_ip_addr(search_params[:ipv4_masklen], search_params[:ipv6_masklen]) elsif search_params[:group_by] == "user" - @ip_addresses = @ip_addresses.group_by_user + @ip_addresses = @ip_addresses.group_by_user.includes(:user) + else + @ip_addresses = @ip_addresses.includes(:user, :model) end respond_with(@ip_addresses) end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - if params[:search][:group_by] == "user" - [:user] - elsif params[:search][:group_by] == "ip_addr" - [] - else - [:user, :model] - end - end - end end diff --git a/app/controllers/ip_bans_controller.rb b/app/controllers/ip_bans_controller.rb index 0c3d072b3..d63637a41 100644 --- a/app/controllers/ip_bans_controller.rb +++ b/app/controllers/ip_bans_controller.rb @@ -12,7 +12,9 @@ class IpBansController < ApplicationController end def index - @ip_bans = IpBan.paginated_search(params, count_pages: true).includes(model_includes(params)) + @ip_bans = IpBan.paginated_search(params, count_pages: true) + @ip_bans = @ip_bans.includes(:creator) if request.format.html? + respond_with(@ip_bans) end @@ -24,14 +26,6 @@ class IpBansController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator] - end - end - def ip_ban_params params.fetch(:ip_ban, {}).permit(%i[ip_addr reason]) end diff --git a/app/controllers/mod_actions_controller.rb b/app/controllers/mod_actions_controller.rb index 40ebcc018..8441b2f56 100644 --- a/app/controllers/mod_actions_controller.rb +++ b/app/controllers/mod_actions_controller.rb @@ -2,7 +2,9 @@ class ModActionsController < ApplicationController respond_to :html, :xml, :json def index - @mod_actions = ModAction.paginated_search(params).includes(model_includes(params)) + @mod_actions = ModAction.paginated_search(params) + @mod_actions = @mod_actions.includes(:creator) if request.format.html? + respond_with(@mod_actions) end @@ -12,14 +14,4 @@ class ModActionsController < ApplicationController fmt.html { redirect_to mod_actions_path(search: { id: @mod_action.id }) } end end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator] - end - end end diff --git a/app/controllers/moderation_reports_controller.rb b/app/controllers/moderation_reports_controller.rb index c5f0f77e1..af872b8fe 100644 --- a/app/controllers/moderation_reports_controller.rb +++ b/app/controllers/moderation_reports_controller.rb @@ -10,7 +10,9 @@ class ModerationReportsController < ApplicationController end def index - @moderation_reports = ModerationReport.paginated_search(params, count_pages: true).includes(model_includes(params)) + @moderation_reports = ModerationReport.paginated_search(params, count_pages: true) + @moderation_reports = @moderation_reports.includes(:creator, :model) if request.format.html? + respond_with(@moderation_reports) end @@ -29,14 +31,6 @@ class ModerationReportsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator, :model] - end - end - def model_type params.fetch(:moderation_report, {}).fetch(:model_type) end diff --git a/app/controllers/moderator/post/disapprovals_controller.rb b/app/controllers/moderator/post/disapprovals_controller.rb index 36a3ac8e4..60ec94025 100644 --- a/app/controllers/moderator/post/disapprovals_controller.rb +++ b/app/controllers/moderator/post/disapprovals_controller.rb @@ -12,7 +12,9 @@ module Moderator end def index - @post_disapprovals = PostDisapproval.paginated_search(params).includes(model_includes(params)) + @post_disapprovals = PostDisapproval.paginated_search(params) + @post_disapprovals = @post_disapprovals.includes(:user) if request.format.html? + respond_with(@post_disapprovals) end @@ -22,14 +24,6 @@ module Moderator "PostDisapproval" end - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user] - end - end - def post_disapproval_params params.require(:post_disapproval).permit(%i[post_id reason message]) end diff --git a/app/controllers/note_versions_controller.rb b/app/controllers/note_versions_controller.rb index d1d58cbbd..a5b224d8d 100644 --- a/app/controllers/note_versions_controller.rb +++ b/app/controllers/note_versions_controller.rb @@ -2,7 +2,9 @@ class NoteVersionsController < ApplicationController respond_to :html, :xml, :json def index - @note_versions = NoteVersion.paginated_search(params).includes(model_includes(params)) + @note_versions = NoteVersion.paginated_search(params) + @note_versions = @note_versions.includes(:updater) if request.format.html? + respond_with(@note_versions) end @@ -12,14 +14,4 @@ class NoteVersionsController < ApplicationController format.html { redirect_to note_versions_path(search: { note_id: @note_version.note_id }) } end end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:updater] - end - end end diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 08e1fcc89..3e7cf43c1 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -6,7 +6,9 @@ class NotesController < ApplicationController end def index - @notes = Note.paginated_search(params).includes(model_includes(params)) + @notes = Note.paginated_search(params) + @notes = @notes.includes(:creator, :post) if request.format.html? + respond_with(@notes) end @@ -59,14 +61,6 @@ class NotesController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator, :post] - end - end - def note_params(context) permitted_params = %i[x y width height body] permitted_params += %i[post_id html_id] if context == :create diff --git a/app/controllers/pool_versions_controller.rb b/app/controllers/pool_versions_controller.rb index bdffa55f6..8b949729e 100644 --- a/app/controllers/pool_versions_controller.rb +++ b/app/controllers/pool_versions_controller.rb @@ -4,7 +4,9 @@ class PoolVersionsController < ApplicationController around_action :set_timeout def index - @pool_versions = PoolArchive.paginated_search(params).includes(model_includes(params)) + @pool_versions = PoolArchive.paginated_search(params) + @pool_versions = @pool_versions.includes(:updater, :pool) if request.format.html? + respond_with(@pool_versions) end @@ -27,14 +29,6 @@ class PoolVersionsController < ApplicationController "PoolArchive" end - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:updater, :pool] - end - end - def set_timeout PoolArchive.connection.execute("SET statement_timeout = #{CurrentUser.user.statement_timeout}") yield diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index e0b4b6e57..410483ddd 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -17,7 +17,8 @@ class PoolsController < ApplicationController end def index - @pools = Pool.paginated_search(params, count_pages: true).includes(model_includes(params)) + @pools = Pool.paginated_search(params, count_pages: true) + @pools = @pools.includes(:creator) if request.format.html? respond_with(@pools) end @@ -90,10 +91,6 @@ class PoolsController < ApplicationController private - def default_includes(params) - [:creator] - end - def item_matches_params(pool) if params[:search][:name_matches] Pool.normalize_name_for_search(pool.name) == Pool.normalize_name_for_search(params[:search][:name_matches]) diff --git a/app/controllers/post_appeals_controller.rb b/app/controllers/post_appeals_controller.rb index 75bae9aa3..e78606ab4 100644 --- a/app/controllers/post_appeals_controller.rb +++ b/app/controllers/post_appeals_controller.rb @@ -8,7 +8,14 @@ class PostAppealsController < ApplicationController end def index - @post_appeals = PostAppeal.paginated_search(params).includes(model_includes(params)) + @post_appeals = PostAppeal.paginated_search(params) + + if request.format.html? + @post_appeals = @post_appeals.includes(:creator, post: [:appeals, :uploader, :approver]) + else + @post_appeals = @post_appeals.includes(:post) + end + respond_with(@post_appeals) end @@ -26,14 +33,6 @@ class PostAppealsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [:post] - else - [:creator, {post: [:appeals, :uploader, :approver]}] - end - end - def post_appeal_params params.fetch(:post_appeal, {}).permit(%i[post_id reason]) end diff --git a/app/controllers/post_approvals_controller.rb b/app/controllers/post_approvals_controller.rb index d01749e97..33f37de2c 100644 --- a/app/controllers/post_approvals_controller.rb +++ b/app/controllers/post_approvals_controller.rb @@ -2,17 +2,9 @@ class PostApprovalsController < ApplicationController respond_to :html, :xml, :json def index - @post_approvals = PostApproval.paginated_search(params).includes(model_includes(params)) + @post_approvals = PostApproval.paginated_search(params) + @post_approvals = @post_approvals.includes(:user, post: :uploader) if request.format.html? + respond_with(@post_approvals) end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user, {post: [:uploader]}] - end - end end diff --git a/app/controllers/post_flags_controller.rb b/app/controllers/post_flags_controller.rb index 44a9e8c96..9b999f27f 100644 --- a/app/controllers/post_flags_controller.rb +++ b/app/controllers/post_flags_controller.rb @@ -8,7 +8,14 @@ class PostFlagsController < ApplicationController end def index - @post_flags = PostFlag.paginated_search(params).includes(model_includes(params)) + @post_flags = PostFlag.paginated_search(params) + + if request.format.html? + @post_flags = @post_flags.includes(:creator, post: [:flags, :uploader, :approver]) + else + @post_flags = @post_flags.includes(:post) + end + respond_with(@post_flags) end @@ -26,16 +33,6 @@ class PostFlagsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [:post] - else - includes_array = [{post: [:flags, :uploader, :approver]}] - includes_array << :creator if CurrentUser.is_moderator? - includes_array - end - end - def post_flag_params params.fetch(:post_flag, {}).permit(%i[post_id reason]) end diff --git a/app/controllers/post_replacements_controller.rb b/app/controllers/post_replacements_controller.rb index cbc2ec7a4..b3497595a 100644 --- a/app/controllers/post_replacements_controller.rb +++ b/app/controllers/post_replacements_controller.rb @@ -24,21 +24,14 @@ class PostReplacementsController < ApplicationController def index params[:search][:post_id] = params.delete(:post_id) if params.key?(:post_id) - @post_replacements = PostReplacement.paginated_search(params).includes(model_includes(params)) + @post_replacements = PostReplacement.paginated_search(params) + @post_replacements = @post_replacements.includes(:creator, post: :uploader) if request.format.html? respond_with(@post_replacements) end private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:creator, {post: [:uploader]}] - end - end - def create_params params.require(:post_replacement).permit(:replacement_url, :replacement_file, :final_source, :tags) end diff --git a/app/controllers/post_versions_controller.rb b/app/controllers/post_versions_controller.rb index fac3cabd3..ea7913bc9 100644 --- a/app/controllers/post_versions_controller.rb +++ b/app/controllers/post_versions_controller.rb @@ -6,7 +6,14 @@ class PostVersionsController < ApplicationController respond_to :js, only: [:undo] def index - @post_versions = PostArchive.paginated_search(params).includes(model_includes(params)) + @post_versions = PostArchive.paginated_search(params) + + if request.format.html? + @post_versions = @post_versions.includes(:updater, post: [:uploader, :versions]) + else + @post_versions = @post_versions.includes(post: :versions) + end + respond_with(@post_versions) end @@ -26,14 +33,6 @@ class PostVersionsController < ApplicationController "PostArchive" end - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [:updater, {post: [:versions]}] - else - [:updater, {post: [:uploader, :versions]}] - end - end - def set_timeout PostArchive.connection.execute("SET statement_timeout = #{CurrentUser.user.statement_timeout}") yield diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index 222dddb29..77eee0c9f 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -5,7 +5,9 @@ class PostVotesController < ApplicationController rescue_with PostVote::Error, status: 422 def index - @post_votes = PostVote.paginated_search(params, count_pages: true).includes(model_includes(params)) + @post_votes = PostVote.paginated_search(params, count_pages: true) + @post_votes = @post_votes.includes(:user, post: :uploader) if request.format.html? + respond_with(@post_votes) end @@ -22,14 +24,4 @@ class PostVotesController < ApplicationController respond_with(@post) end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user, {post: [:uploader]}] - end - end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index bd6f23311..9639e65ca 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -11,7 +11,7 @@ class PostsController < ApplicationController end else @post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], raw: params[:raw], random: params[:random], format: params[:format]) - @posts = @post_set.posts = @post_set.posts.includes(model_includes(params)) if !@post_set.is_random? + @posts = @post_set.posts = @post_set.posts.includes(:uploader) if !@post_set.is_random? && CurrentUser.is_moderator? respond_with(@posts) do |format| format.atom end @@ -98,14 +98,6 @@ class PostsController < ApplicationController private - def default_includes(params) - if ["json", "xml", "atom"].include?(params[:format]) - [] - else - (CurrentUser.user.is_moderator? ? [:uploader] : []) - end - end - def tag_query params[:tags] || (params[:post] && params[:post][:tags]) end diff --git a/app/controllers/saved_searches_controller.rb b/app/controllers/saved_searches_controller.rb index 90bb97976..716d21d73 100644 --- a/app/controllers/saved_searches_controller.rb +++ b/app/controllers/saved_searches_controller.rb @@ -2,7 +2,7 @@ class SavedSearchesController < ApplicationController respond_to :html, :xml, :json, :js def index - @saved_searches = saved_searches.paginated_search(params, count_pages: true).includes(model_includes(params)) + @saved_searches = saved_searches.paginated_search(params, count_pages: true) respond_with(@saved_searches) end diff --git a/app/controllers/tag_aliases_controller.rb b/app/controllers/tag_aliases_controller.rb index b2ac5e2c3..68116d1db 100644 --- a/app/controllers/tag_aliases_controller.rb +++ b/app/controllers/tag_aliases_controller.rb @@ -22,7 +22,9 @@ class TagAliasesController < ApplicationController end def index - @tag_aliases = TagAlias.paginated_search(params, count_pages: true).includes(model_includes(params)) + @tag_aliases = TagAlias.paginated_search(params, count_pages: true) + @tag_aliases = @tag_aliases.includes(:antecedent_tag, :consequent_tag, :approver) if request.format.html? + respond_with(@tag_aliases) end @@ -42,14 +44,6 @@ class TagAliasesController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:antecedent_tag, :consequent_tag, :approver] - end - end - def tag_alias_params params.require(:tag_alias).permit(%i[antecedent_name consequent_name forum_topic_id skip_secondary_validations]) end diff --git a/app/controllers/tag_implications_controller.rb b/app/controllers/tag_implications_controller.rb index d70bd6226..b7e099f18 100644 --- a/app/controllers/tag_implications_controller.rb +++ b/app/controllers/tag_implications_controller.rb @@ -22,7 +22,9 @@ class TagImplicationsController < ApplicationController end def index - @tag_implications = TagImplication.paginated_search(params, count_pages: true).includes(model_includes(params)) + @tag_implications = TagImplication.paginated_search(params, count_pages: true) + @tag_implications = @tag_implications.includes(:antecedent_tag, :consequent_tag, :approver) if request.format.html? + respond_with(@tag_implications) end @@ -42,14 +44,6 @@ class TagImplicationsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:antecedent_tag, :consequent_tag, :approver] - end - end - def tag_implication_params params.require(:tag_implication).permit(%i[antecedent_name consequent_name forum_topic_id skip_secondary_validations]) end diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 619f04470..4a3cd5665 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -9,7 +9,7 @@ class TagsController < ApplicationController end def index - @tags = Tag.paginated_search(params).includes(model_includes(params)) + @tags = Tag.paginated_search(params) respond_with(@tags) end diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 451995ca6..42bc12daa 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -23,7 +23,9 @@ class UploadsController < ApplicationController end def index - @uploads = Upload.paginated_search(params, count_pages: true).includes(model_includes(params)) + @uploads = Upload.paginated_search(params, count_pages: true) + @uploads = @uploads.includes(:uploader, post: :uploader) if request.format.html? + respond_with(@uploads) end @@ -58,14 +60,6 @@ class UploadsController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:uploader, {post: [:uploader]}] - end - end - def upload_params permitted_params = %i[ file source tag_string rating status parent_id artist_commentary_title diff --git a/app/controllers/user_feedbacks_controller.rb b/app/controllers/user_feedbacks_controller.rb index b7bbcf70a..a5626c48d 100644 --- a/app/controllers/user_feedbacks_controller.rb +++ b/app/controllers/user_feedbacks_controller.rb @@ -19,7 +19,9 @@ class UserFeedbacksController < ApplicationController end def index - @user_feedbacks = UserFeedback.paginated_search(params, count_pages: true).includes(model_includes(params)) + @user_feedbacks = UserFeedback.paginated_search(params, count_pages: true) + @user_feedbacks = @user_feedbacks.includes(:user, :creator) if request.format.html? + respond_with(@user_feedbacks) end @@ -37,14 +39,6 @@ class UserFeedbacksController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:user, :creator] - end - end - def check_privilege(user_feedback) raise User::PrivilegeError unless user_feedback.editable_by?(CurrentUser.user) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1851c776e..503f2dae3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -31,7 +31,9 @@ class UsersController < ApplicationController return end - @users = User.paginated_search(params).includes(model_includes(params)) + @users = User.paginated_search(params) + @users = @users.includes(:inviter) if request.format.html? + respond_with(@users) end @@ -94,14 +96,6 @@ class UsersController < ApplicationController private - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:inviter] - end - end - def item_matches_params(user) if params[:search][:name_matches] User.normalize_name(user.name) == User.normalize_name(params[:search][:name_matches]) diff --git a/app/controllers/wiki_page_versions_controller.rb b/app/controllers/wiki_page_versions_controller.rb index 83890cb5c..7293f72a9 100644 --- a/app/controllers/wiki_page_versions_controller.rb +++ b/app/controllers/wiki_page_versions_controller.rb @@ -3,7 +3,9 @@ class WikiPageVersionsController < ApplicationController layout "sidebar" def index - @wiki_page_versions = WikiPageVersion.paginated_search(params).includes(model_includes(params)) + @wiki_page_versions = WikiPageVersion.paginated_search(params) + @wiki_page_versions = @wiki_page_versions.includes(:updater) if request.format.html? + respond_with(@wiki_page_versions) end @@ -27,14 +29,4 @@ class WikiPageVersionsController < ApplicationController respond_with([@thispage, @otherpage]) end - - private - - def default_includes(params) - if ["json", "xml"].include?(params[:format]) - [] - else - [:updater] - end - end end diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index e2cba2ad0..a3ed76e0d 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -15,7 +15,7 @@ class WikiPagesController < ApplicationController end def index - @wiki_pages = WikiPage.paginated_search(params).includes(model_includes(params)) + @wiki_pages = WikiPage.paginated_search(params) respond_with(@wiki_pages) end diff --git a/app/logical/parameter_builder.rb b/app/logical/parameter_builder.rb index 3a863029e..2787a5915 100644 --- a/app/logical/parameter_builder.rb +++ b/app/logical/parameter_builder.rb @@ -40,6 +40,8 @@ class ParameterBuilder end def self.includes_parameters(only_string, model_name) + return [] if only_string.blank? + only_array = split_only_string(only_string) get_includes_array(only_array, model_name) end