move post preview presenter to partial

This commit is contained in:
Albert Yi
2018-10-29 17:46:01 -07:00
parent 3d49971e49
commit 011a6f1f7c
4 changed files with 107 additions and 79 deletions

View File

@@ -20,111 +20,115 @@ class PostPresenter < Presenter
return "" return ""
end end
path = options[:path_prefix] || "/posts" locals = {}
if Danbooru.config.enable_image_cropping && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails?
cropped_src = post.crop_file_url
else
cropped_src = post.preview_file_url
end
html = %{<article itemscope itemtype="http://schema.org/ImageObject" id="post_#{post.id}" class="#{preview_class(post, options)}" #{data_attributes(post)}>} locals[:article_attrs] = {
"id" => "post_##{post.id}",
"class" => preview_class(post, options).join(" ")
}.merge(data_attributes(post))
# TODO: rename path_prefix to controller
locals[:link_params] = {
"controller" => options[:path_prefix] || "posts",
"action" => "show",
"id" => post.id
}
if options[:tags].present? && !CurrentUser.is_anonymous? if options[:tags].present? && !CurrentUser.is_anonymous?
tag_param = "?tags=#{CGI::escape(options[:tags])}" locals[:link_params]["q"] = options[:tags]
elsif options[:pool_id] || options[:pool] end
tag_param = "?pool_id=#{CGI::escape((options[:pool_id] || options[:pool].id).to_s)}" if options[:pool_id] || options[:pool]
elsif options[:favgroup_id] || options[:favgroup] locals[:link_params]["pool_id"] = options[:pool_id] || options[:pool].id
tag_param = "?favgroup_id=#{CGI::escape((options[:favgroup_id] || options[:favgroup].id).to_s)}" end
else if options[:favgroup_id] || options[:favgroup]
tag_param = nil locals[:link_params]["favgroup_id"] = options[:favgroup_id] || options[:favgroup].id
end end
html << %{<a href="#{path}/#{post.id}#{tag_param}">}
tooltip = "#{post.tag_string} rating:#{post.rating} score:#{post.score}" locals[:tooltip] = "#{post.tag_string} rating:#{post.rating} score:#{post.score}"
html << %{<picture>}
html << %{<source media="(max-width: 660px)" srcset="#{cropped_src}">} locals[:cropped_url] = if Danbooru.config.enable_image_cropping && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails?
html << %{<source media="(min-width: 660px)" srcset="#{post.preview_file_url}">} post.crop_file_url
html << %{<img itemprop="thumbnailUrl" class="has-cropped-#{post.has_cropped?}" src="#{post.preview_file_url}" title="#{h(tooltip)}" alt="#{h(post.tag_string)}">} else
html << %{</picture>} post.preview_file_url
html << %{</a>} end
locals[:preview_url] = post.preview_file_url
locals[:alt_text] = post.tag_string
locals[:has_cropped] = post.has_cropped?
if options[:pool] if options[:pool]
html << %{<p class="desc">} locals[:pool] = options[:pool]
html << %{<a href="/pools/#{options[:pool].id}">} else
html << h(options[:pool].pretty_name.truncate(80)) locals[:pool] = nil
html << %{</a>}
html << %{</p>}
end end
if options[:similarity] if options[:similarity]
html << %{<p class="desc">} locals[:similarity] = options[:similarity].round
html << "Similarity: #{options[:similarity].round}%" locals[:width] = post.image_width
html << %{</p>} locals[:height] = post.image_height
else
locals[:similarity] = nil
end end
if options[:size] if options[:size]
html << %{<p class="desc">} locals[:size] = post.file_size
html << post.file_size.to_s(:human_size) else
html << " (#{post.image_width}x#{post.image_height})" locals[:size] = nil
html << %{</p>}
end end
html << %{</article>} ApplicationController.render(partial: "posts/partials/index/preview", locals: locals)
html.html_safe
end end
def self.preview_class(post, options = {}) def self.preview_class(post, options = {})
klass = "post-preview" klass = ["post-preview"]
# klass << " large-cropped" if post.has_cropped? && options[:show_cropped] # klass << " large-cropped" if post.has_cropped? && options[:show_cropped]
klass << " captioned" if options.values_at(:pooled, :size, :similarity).any?(&:present?) klass << "captioned" if options.values_at(:pooled, :size, :similarity).any?(&:present?)
klass << " post-status-pending" if post.is_pending? klass << "post-status-pending" if post.is_pending?
klass << " post-status-flagged" if post.is_flagged? klass << "post-status-flagged" if post.is_flagged?
klass << " post-status-deleted" if post.is_deleted? klass << "post-status-deleted" if post.is_deleted?
klass << " post-status-has-parent" if post.parent_id klass << "post-status-has-parent" if post.parent_id
klass << " post-status-has-children" if post.has_visible_children? klass << "post-status-has-children" if post.has_visible_children?
klass klass
end end
def self.data_attributes(post) def self.data_attributes(post)
attributes = %{ attributes = {
data-id="#{post.id}" "data-id" => post.id,
data-has-sound="#{post.has_tag?('video_with_sound|flash_with_sound')}" "data-has-sound" => post.has_tag?('video_with_sound|flash_with_sound'),
data-tags="#{h(post.tag_string)}" "data-tags" => post.tag_string,
data-pools="#{post.pool_string}" "data-pools" => post.pool_string,
data-approver-id="#{post.approver_id}" "data-approver-id" => post.approver_id,
data-rating="#{post.rating}" "data-rating" => post.rating,
data-width="#{post.image_width}" "data-width" => post.image_width,
data-height="#{post.image_height}" "data-height" => post.image_height,
data-flags="#{post.status_flags}" "data-flags" => post.status_flags,
data-parent-id="#{post.parent_id}" "data-parent-id" => post.parent_id,
data-has-children="#{post.has_children?}" "data-has-children" => post.has_children?,
data-score="#{post.score}" "data-score" => post.score,
data-views="#{post.view_count}" "data-views" => post.view_count,
data-fav-count="#{post.fav_count}" "data-fav-count" => post.fav_count,
data-pixiv-id="#{post.pixiv_id}" "data-pixiv-id" => post.pixiv_id,
data-file-ext="#{post.file_ext}" "data-file-ext" => post.file_ext,
data-source="#{h(post.source)}" "data-source" => post.source,
data-top-tagger="#{post.keeper_id}" "data-top-tagger" => post.keeper_id,
data-uploader-id="#{post.uploader_id}" "data-uploader-id" => post.uploader_id,
data-normalized-source="#{h(post.normalized_source)}" "data-normalized-source" => post.normalized_source,
data-is-favorited="#{post.favorited_by?(CurrentUser.user.id)}" "data-is-favorited" => post.favorited_by?(CurrentUser.user.id)
} }
if CurrentUser.is_moderator? if CurrentUser.is_moderator?
attributes += %{ attributes["data-uploader"] = post.uploader_name
data-uploader="#{h(post.uploader_name)}"
}
end end
if post.visible? if post.visible?
attributes += %{ attributes["data-md5"] = post.md5
data-md5="#{post.md5}" attributes["data-file-url"] = post.file_url
data-file-url="#{post.file_url}" attributes["data-large-file-url"] = post.large_file_url
data-large-file-url="#{post.large_file_url}" attributes["data-preview-file-url"] = post.preview_file_url
data-preview-file-url="#{post.preview_file_url}"
}
end end
attributes.html_safe attributes
end end
def initialize(post) def initialize(post)

View File

@@ -12,6 +12,6 @@ class Presenter
end end
def u(s) def u(s)
CGI::escape(s) CGI.escape(s)
end end
end end

View File

@@ -0,0 +1,24 @@
<%= content_tag(:article, article_attrs) do -%>
<%= content_tag(:a, href: url_for(link_params)) do -%>
<%= content_tag(:picture) do -%>
<%= tag.source media: "(max-width: 660px)", srcset: cropped_url -%>
<%= tag.source media: "(min-width: 660px)", srcset: preview_url -%>
<%= tag.img class: "has-cropped-#{has_cropped}", src: preview_url, title: tooltip, alt: alt_text -%>
<% end -%>
<% end -%>
<% if pool -%>
<p class="desc">
<%= link_to truncate(pool.pretty_name, 80), pool_path(pool.id) %>
</p>
<% end -%>
<% if similarity -%>
<p class="desc">
Similarity: <%= similarity %>
</p>
<% end -%>
<% if size -%>
<p class="desc">
<%= number_to_human_size(size) %> (<%= width %>x<%= height %>)
</p>
<% end -%>
<% end -%>

View File

@@ -1,9 +1,9 @@
<div id="search-seq-nav"> <div id="search-seq-nav">
<ul> <ul>
<li class="active"> <li class="active">
<%= link_to "&lsaquo;&thinsp;prev".html_safe, show_seq_post_path(post, :tags => params[:tags], :seq => "prev"), :rel => "prev nofollow", :class => "prev" %> <%= link_to "&lsaquo;&thinsp;prev".html_safe, show_seq_post_path(post, :tags => params[:q], :seq => "prev"), :rel => "prev nofollow", :class => "prev" %>
<span class="search-name">Search: <%= params[:tags].presence || "status:any" %></span> <span class="search-name">Search: <%= params[:q].presence || "status:any" %></span>
<%= link_to "next&thinsp;&rsaquo;".html_safe, show_seq_post_path(post, :tags => params[:tags], :seq => "next"), :rel => "next nofollow", :class => "next" %> <%= link_to "next&thinsp;&rsaquo;".html_safe, show_seq_post_path(post, :tags => params[:q], :seq => "next"), :rel => "next nofollow", :class => "next" %>
</li> </li>
</ul> </ul>
</div> </div>