Skip to content

Commit

Permalink
Add ability to use nested only parameter
Browse files Browse the repository at this point in the history
- The only string works much the same as before with its comma separation
-- Nested includes are indicated with square brackets "[ ]"
-- The nested include is the value immediately preceding the square brackets
-- The only string is the comma separated string inside those brackets
- Default includes are split between format types when necessary
-- This prevents unnecessary includes from being added on page load
- Available includes are those items which are allowed to be accessible to the user
-- Some aren't because they are sensitive, such as the creator of a flag
-- Some aren't because the number of associated items is too large
- The amount of times the same model can be included to prevent recursions
-- One exception is the root model may include the same model once
--- e.g. the user model can include the inviter which is also the user model
-- Another exception is if the include is a has_many association
--- e.g. artist urls can include the artist, and then artist urls again
  • Loading branch information
BrokenEagle committed Feb 12, 2020
1 parent dd42583 commit 63b3503
Show file tree
Hide file tree
Showing 85 changed files with 634 additions and 85 deletions.
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -34,6 +34,25 @@ def respond_with(subject, *options, &block)
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 = []
end
includes_array
end

def default_includes(*)
[]
end

def model_name
controller_name.classify
end

def redirect_to_show(items)
redirect_to send("#{controller_path.singularize}_path", items.first, format: request.format.symbol)
end
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/artist_commentaries_controller.rb
Expand Up @@ -3,7 +3,7 @@ class ArtistCommentariesController < ApplicationController
before_action :member_only, only: [:create_or_update, :revert]

def index
@commentaries = ArtistCommentary.paginated_search(params)
@commentaries = ArtistCommentary.paginated_search(params).includes(model_includes(params))
respond_with(@commentaries)
end

Expand Down Expand Up @@ -36,6 +36,14 @@ def revert

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
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/artist_commentary_versions_controller.rb
Expand Up @@ -2,7 +2,7 @@ class ArtistCommentaryVersionsController < ApplicationController
respond_to :html, :xml, :json

def index
@commentary_versions = ArtistCommentaryVersion.paginated_search(params)
@commentary_versions = ArtistCommentaryVersion.paginated_search(params).includes(model_includes(params))
respond_with(@commentary_versions)
end

Expand All @@ -12,4 +12,14 @@ def show
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
27 changes: 24 additions & 3 deletions app/controllers/artist_urls_controller.rb
Expand Up @@ -3,10 +3,10 @@ class ArtistUrlsController < ApplicationController
before_action :member_only, except: [:index]

def index
@artist_urls = ArtistUrl.includes(:artist).paginated_search(params)
@artist_urls = ArtistUrl.paginated_search(params).includes(model_includes(params))
respond_with(@artist_urls) do |format|
format.json { render json: @artist_urls.to_json(include: "artist") }
format.xml { render xml: @artist_urls.to_xml(include: "artist", root: "artist-urls") }
format.json { render json: @artist_urls.to_json(format_params) }
format.xml { render xml: @artist_urls.to_xml(format_params) }
end
end

Expand All @@ -18,6 +18,27 @@ def update

private

def default_includes(params)
if ["json", "xml"].include?(params[:format])
[{artist: [:urls]}]
else
[:artist]
end
end

def format_params
param_hash = {}
if params[:only]
param_hash[:only] = params[:only]
else
param_hash[:include] = [:artist]
end
if request.format.symbol == :xml
param_hash[:root] = "artist-urls"
end
param_hash
end

def artist_url_params
permitted_params = %i[is_active]

Expand Down
12 changes: 11 additions & 1 deletion app/controllers/artist_versions_controller.rb
Expand Up @@ -2,7 +2,7 @@ class ArtistVersionsController < ApplicationController
respond_to :html, :xml, :json

def index
@artist_versions = ArtistVersion.includes(:updater).paginated_search(params)
@artist_versions = ArtistVersion.paginated_search(params).includes(model_includes(params))
respond_with(@artist_versions)
end

Expand All @@ -12,4 +12,14 @@ def show
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
13 changes: 9 additions & 4 deletions app/controllers/artists_controller.rb
Expand Up @@ -31,10 +31,7 @@ def index
# XXX
params[:search][:name] = params.delete(:name) if params[:name]

@artists = Artist.includes(:urls).paginated_search(params)
@artists = @artists.includes(:tag) if request.format.html?
@artists = @artists.includes(:urls) if !request.format.html?

@artists = Artist.paginated_search(params).includes(model_includes(params))
respond_with(@artists)
end

Expand Down Expand Up @@ -81,6 +78,14 @@ def show_or_new

private

def default_includes(params)
if ["json", "xml"].include?(params[:format])
[:urls]
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])
Expand Down
14 changes: 10 additions & 4 deletions app/controllers/bans_controller.rb
Expand Up @@ -12,10 +12,8 @@ def edit
end

def index
@bans = Ban.paginated_search(params, count_pages: true)
respond_with(@bans) do |fmt|
fmt.html { @bans = @bans.includes(:user, :banner) }
end
@bans = Ban.paginated_search(params, count_pages: true).includes(model_includes(params))
respond_with(@bans)
end

def show
Expand Down Expand Up @@ -50,6 +48,14 @@ def destroy

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
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/bulk_update_requests_controller.rb
Expand Up @@ -42,12 +42,20 @@ def destroy
end

def index
@bulk_update_requests = BulkUpdateRequest.includes(:user, :approver, :forum_topic, forum_post: [:votes]).paginated_search(params, count_pages: true)
@bulk_update_requests = BulkUpdateRequest.paginated_search(params, count_pages: true).includes(model_includes(params))
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
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/comment_votes_controller.rb
Expand Up @@ -5,7 +5,7 @@ class CommentVotesController < ApplicationController
rescue_with CommentVote::Error, ActiveRecord::RecordInvalid, status: 422

def index
@comment_votes = CommentVote.includes(:user, comment: [:creator, :post]).paginated_search(params, count_pages: true)
@comment_votes = CommentVote.paginated_search(params, count_pages: true).includes(model_includes(params))
respond_with(@comment_votes)
end

Expand All @@ -20,4 +20,14 @@ def destroy
@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
22 changes: 15 additions & 7 deletions app/controllers/comments_controller.rb
@@ -1,5 +1,5 @@
class CommentsController < ApplicationController
respond_to :html, :xml, :json
respond_to :html, :xml, :json, :atom
respond_to :js, only: [:new, :destroy, :undelete]
before_action :member_only, :except => [:index, :search, :show]
skip_before_action :api_check
Expand Down Expand Up @@ -74,6 +74,18 @@ def undelete

private

def default_includes(params)
if ["json", "xml"].include?(params[:format])
[:creator, :updater]
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
Expand All @@ -90,12 +102,8 @@ def index_by_post
end

def index_by_comment
@comments = Comment.includes(:creator, :updater).paginated_search(params)
respond_with(@comments) do |format|
format.atom do
@comments = @comments.includes(:post, :creator).load
end
end
@comments = Comment.paginated_search(params).includes(model_includes(params))
respond_with(@comments)
end

def check_privilege(comment)
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/dmails_controller.rb
Expand Up @@ -15,7 +15,7 @@ def new
end

def index
@dmails = Dmail.visible.paginated_search(params, defaults: { folder: "received" }, count_pages: true)
@dmails = Dmail.visible.paginated_search(params, defaults: { folder: "received" }, count_pages: true).includes(model_includes(params))
respond_with(@dmails)
end

Expand Down Expand Up @@ -51,6 +51,14 @@ def mark_all_as_read

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
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/dtext_links_controller.rb
Expand Up @@ -2,7 +2,17 @@ class DtextLinksController < ApplicationController
respond_to :html, :xml, :json

def index
@dtext_links = DtextLink.includes(:model).paginated_search(params)
@dtext_links = DtextLink.paginated_search(params).includes(model_includes(params))
respond_with(@dtext_links)
end

private

def default_includes(params)
if ["json", "xml"].include?(params[:format])
[]
else
[:model]
end
end
end
10 changes: 9 additions & 1 deletion app/controllers/favorite_groups_controller.rb
Expand Up @@ -4,7 +4,7 @@ class FavoriteGroupsController < ApplicationController

def index
params[:search][:creator_id] ||= params[:user_id]
@favorite_groups = FavoriteGroup.paginated_search(params)
@favorite_groups = FavoriteGroup.paginated_search(params).includes(model_includes(params))
respond_with(@favorite_groups)
end

Expand Down Expand Up @@ -61,6 +61,14 @@ def add_post

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
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/forum_post_votes_controller.rb
Expand Up @@ -3,7 +3,7 @@ class ForumPostVotesController < ApplicationController
before_action :member_only, only: [:create, :destroy]

def index
@forum_post_votes = ForumPostVote.includes(creator: [], forum_post: [:topic]).paginated_search(params, count_pages: true)
@forum_post_votes = ForumPostVote.paginated_search(params, count_pages: true).includes(model_includes(params))
respond_with(@forum_post_votes)
end

Expand All @@ -21,6 +21,14 @@ def destroy

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
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/forum_posts_controller.rb
Expand Up @@ -24,7 +24,7 @@ def edit
end

def index
@forum_posts = ForumPost.paginated_search(params).includes(:topic)
@forum_posts = ForumPost.paginated_search(params).includes(model_includes(params))
respond_with(@forum_posts)
end

Expand Down Expand Up @@ -68,6 +68,14 @@ def undelete

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
Expand Down

0 comments on commit 63b3503

Please sign in to comment.