diff --git a/app/assets/stylesheets/sticky-nav.scss b/app/assets/stylesheets/sticky-nav.scss index 641020aa3..22d7f3d04 100644 --- a/app/assets/stylesheets/sticky-nav.scss +++ b/app/assets/stylesheets/sticky-nav.scss @@ -116,7 +116,7 @@ margin-left: 2px; font-family: $monospace; .row{ - padding: calc(5px + 0.2vw) 0px; + padding: calc(3px + 0.1vw) 0px; display: inline-block; width: 96%; } diff --git a/app/controllers/html_variants_controller.rb b/app/controllers/html_variants_controller.rb index 45dc1b5b7..01806bd23 100644 --- a/app/controllers/html_variants_controller.rb +++ b/app/controllers/html_variants_controller.rb @@ -3,9 +3,13 @@ class HtmlVariantsController < ApplicationController def index authorize HtmlVariant - @user_variants = current_user.html_variants.order("created_at DESC") - @all_published_variants = HtmlVariant.where(published: true).order("created_at DESC") - @preview_path = Article.where(featured: true, published: true).order("published_at DESC").first&.path.to_s + @html_variants = if params[:state] == "mine" + current_user.html_variants.order("created_at DESC").includes(:user) + elsif params[:state] == "admin" + HtmlVariant.where(published: true).order("created_at DESC").includes(:user) + else + HtmlVariant.where(published: true, approved: true).order("success_rate DESC").includes(:user) + end end def new @@ -13,11 +17,19 @@ class HtmlVariantsController < ApplicationController @html_variant = HtmlVariant.new if params[:fork_id] @fork = HtmlVariant.find(params[:fork_id]) - @html_variant.name = @fork.name + " FORK" + @html_variant.name = @fork.name + " FORK-#{rand(10000)}" @html_variant.html = @fork.html end end + def show + @story_show = true + @@article_show = true + @html_variant = HtmlVariant.find(params[:id]) + authorize @html_variant + render layout: false + end + def edit @html_variant = HtmlVariant.find(params[:id]) authorize @html_variant @@ -29,7 +41,8 @@ class HtmlVariantsController < ApplicationController @html_variant.group = "article_show_sidebar_cta" @html_variant.user_id = current_user.id if @html_variant.save - redirect_to "/html_variants" + flash[:success] = "HTML Variant Created" + redirect_to "/html_variants/#{@html_variant.id}/edit" else render :new end @@ -39,7 +52,8 @@ class HtmlVariantsController < ApplicationController @html_variant = HtmlVariant.find(params[:id]) authorize @html_variant if @html_variant.update(html_variant_params) - redirect_to "/html_variants" + flash[:success] = "HTML Variant Updated" + redirect_to "/html_variants/#{@html_variant.id}/edit" else render :edit end diff --git a/app/models/html_variant.rb b/app/models/html_variant.rb index 86f6134e9..da6e2da40 100644 --- a/app/models/html_variant.rb +++ b/app/models/html_variant.rb @@ -7,6 +7,7 @@ class HtmlVariant < ApplicationRecord belongs_to :user, optional: true has_many :html_variant_trials has_many :html_variant_successes + before_save :prefix_all_images def calculate_success_rate! self.success_rate = html_variant_successes.size.to_f / (html_variant_trials.size * 10.0) # x10 because we only capture every 10th @@ -37,4 +38,46 @@ class HtmlVariant < ApplicationRecord errors.add(:base, "cannot change once published and approved") end end + + def prefix_all_images + # wrap with Cloudinary or allow if from giphy or githubusercontent.com + doc = Nokogiri::HTML.fragment(html) + doc.css("img").each do |img| + src = img.attr("src") + next unless src + # allow image to render as-is + img["src"] = if giphy_img?(src) + src.gsub("https://media.", "https://i.") + else + img_of_size(src, 420) + end + end + self.html = doc.to_html + end + + def giphy_img?(source) + uri = URI.parse(source) + return false if uri.scheme != "https" + return false if uri.userinfo || uri.fragment || uri.query + return false if uri.host != "media.giphy.com" && uri.host != "i.giphy.com" + return false if uri.port != 443 # I think it has to be this if its https? + + uri.path.ends_with?(".gif") + end + + def img_of_size(source, width = 420) + quality = if source && (source.include? ".gif") + 66 + else + "auto" + end + cl_image_path(source, + type: "fetch", + width: width, + crop: "limit", + quality: quality, + flags: "progressive", + fetch_format: "auto", + sign_url: true).gsub(",", "%2C") + end end diff --git a/app/views/articles/_sticky_nav.html.erb b/app/views/articles/_sticky_nav.html.erb index ca538ba55..8f6b40c1e 100644 --- a/app/views/articles/_sticky_nav.html.erb +++ b/app/views/articles/_sticky_nav.html.erb @@ -46,7 +46,7 @@ <%= follow_button(@actor) %> <% if @actor.class.name == "User" %> - <%= render "articles/user_metadata" %> + <%= render "articles/user_metadata", context: "sidebar" %> <% elsif @actor.class.name == "Organization" && @actor.approved_and_filled_out_cta? %>
<%= @actor.cta_processed_html.html_safe %> diff --git a/app/views/articles/_user_metadata.html.erb b/app/views/articles/_user_metadata.html.erb index 4ed35444d..8ac99dec3 100644 --- a/app/views/articles/_user_metadata.html.erb +++ b/app/views/articles/_user_metadata.html.erb @@ -50,7 +50,7 @@ .user-profile-header {min-height: 398px;} <% end %> - <% if @user.email_public %> + <% if @user.email_public && context != "sidebar" %>
email diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 2de506a2e..60fd8298e 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -174,7 +174,7 @@

- <%= render "articles/user_metadata" %> + <%= render "articles/user_metadata", context: "profile" %>
<% elsif @tag %> +
+
+ <%= @html_variant.html.html_safe %> +
+
\ No newline at end of file