From 2297e84f35beea623ca44572ed9b6a39d1d5e0ad Mon Sep 17 00:00:00 2001 From: Fernando Valverde Date: Mon, 28 Sep 2020 09:22:13 -0600 Subject: [PATCH] [deploy] HTML Variant admin dashboard (#10188) * HTML Variant admin dashboard * Tweaks to actions & spec fixes * Update app/controllers/admin/html_variants_controller.rb Co-authored-by: Michael Kohl * Removes old html_variants dashboards * Removes old lingering spec Co-authored-by: Michael Kohl --- .../admin/application_controller.rb | 1 + .../admin/html_variants_controller.rb | 84 ++++++++++ app/controllers/html_variants_controller.rb | 71 --------- app/dashboards/dashboard_manifest.rb | 3 - app/dashboards/html_variant_dashboard.rb | 79 ---------- .../html_variant_success_dashboard.rb | 58 ------- .../html_variant_trial_dashboard.rb | 58 ------- app/lib/constants/role.rb | 1 + app/models/html_variant.rb | 2 + app/views/admin/html_variants/_form.html.erb | 38 +++++ app/views/admin/html_variants/edit.html.erb | 9 ++ app/views/admin/html_variants/index.html.erb | 54 +++++++ app/views/admin/html_variants/new.html.erb | 7 + app/views/admin/html_variants/show.html.erb | 7 + app/views/html_variants/_form.html.erb | 42 ----- app/views/html_variants/_page_styles.html.erb | 144 ------------------ .../_single_html_variant.html.erb | 23 --- app/views/html_variants/edit.html.erb | 9 -- app/views/html_variants/index.html.erb | 26 ---- app/views/html_variants/new.html.erb | 7 - app/views/html_variants/show.html.erb | 24 --- config/routes.rb | 2 +- spec/requests/admin/html_variants_spec.rb | 139 +++++++++++++++++ spec/requests/html_variants_spec.rb | 109 ------------- 24 files changed, 343 insertions(+), 654 deletions(-) create mode 100644 app/controllers/admin/html_variants_controller.rb delete mode 100644 app/controllers/html_variants_controller.rb delete mode 100644 app/dashboards/html_variant_dashboard.rb delete mode 100644 app/dashboards/html_variant_success_dashboard.rb delete mode 100644 app/dashboards/html_variant_trial_dashboard.rb create mode 100644 app/views/admin/html_variants/_form.html.erb create mode 100644 app/views/admin/html_variants/edit.html.erb create mode 100644 app/views/admin/html_variants/index.html.erb create mode 100644 app/views/admin/html_variants/new.html.erb create mode 100644 app/views/admin/html_variants/show.html.erb delete mode 100644 app/views/html_variants/_form.html.erb delete mode 100644 app/views/html_variants/_page_styles.html.erb delete mode 100644 app/views/html_variants/_single_html_variant.html.erb delete mode 100644 app/views/html_variants/edit.html.erb delete mode 100644 app/views/html_variants/index.html.erb delete mode 100644 app/views/html_variants/new.html.erb delete mode 100644 app/views/html_variants/show.html.erb create mode 100644 spec/requests/admin/html_variants_spec.rb delete mode 100644 spec/requests/html_variants_spec.rb diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb index bc0dcd298..b9176bffe 100644 --- a/app/controllers/admin/application_controller.rb +++ b/app/controllers/admin/application_controller.rb @@ -16,6 +16,7 @@ module Admin { name: "display_ads", controller: "display_ads" }, { name: "events", controller: "events" }, { name: "growth", controller: "growth" }, + { name: "html_variants", controller: "html_variants" }, { name: "listings", controller: "listings" }, { name: "moderator_actions", controller: "moderator_actions" }, { name: "mods", controller: "mods" }, diff --git a/app/controllers/admin/html_variants_controller.rb b/app/controllers/admin/html_variants_controller.rb new file mode 100644 index 000000000..25a041cc2 --- /dev/null +++ b/app/controllers/admin/html_variants_controller.rb @@ -0,0 +1,84 @@ +module Admin + class HtmlVariantsController < Admin::ApplicationController + layout "admin" + + def index + relation = if params[:state] == "mine" + current_user.html_variants.order(created_at: :desc) + elsif params[:state] == "admin" + HtmlVariant.where(published: true, approved: false).order(created_at: :desc) + elsif params[:state].present? + HtmlVariant.where(published: true, approved: true, group: params[:state]).order(success_rate: :desc) + else + HtmlVariant.where(published: true, approved: true).order(success_rate: :desc) + end + + @html_variants = relation.includes(:user).page(params[:page]).per(30) + end + + def new + @html_variant = HtmlVariant.new + return unless params[:fork_id] + + @fork = HtmlVariant.find(params[:fork_id]) + @html_variant.name = "#{@fork.name} FORK-#{rand(10_000)}" + @html_variant.html = @fork.html + end + + def show + @html_variant = HtmlVariant.find(params[:id]) + render layout: "application" + end + + def edit + @html_variant = HtmlVariant.find(params[:id]) + end + + def create + @html_variant = HtmlVariant.new(html_variant_params) + @html_variant.user_id = current_user.id + + if @html_variant.save + flash[:success] = "HTML Variant has been created!" + redirect_to admin_html_variants_path(state: "mine") + else + flash[:danger] = @html_variant.errors_as_sentence + render new_admin_html_variant_path + end + end + + def update + @html_variant = HtmlVariant.find(params[:id]) + + if @html_variant.update(html_variant_params) + flash[:success] = "HTML Variant has been updated!" + redirect_to edit_admin_html_variant_path(@html_variant) + else + flash[:danger] = @html_variant.errors_as_sentence + render :edit + end + end + + def destroy + @html_variant = HtmlVariant.find(params[:id]) + + if @html_variant.destroy + flash[:success] = "HTML Variant has been deleted!" + redirect_to admin_html_variants_path + else + flash[:danger] = "Something went wrong with deleting the HTML Variant." + render :edit + end + end + + private + + def html_variant_params + params.permit(:html, :name, :published, :approved, :target_tag, :group) + end + + def authorize_admin + authorize HtmlVariant, :access?, policy_class: InternalPolicy + end + end +end diff --git a/app/controllers/html_variants_controller.rb b/app/controllers/html_variants_controller.rb deleted file mode 100644 index 66c07b8b2..000000000 --- a/app/controllers/html_variants_controller.rb +++ /dev/null @@ -1,71 +0,0 @@ -class HtmlVariantsController < ApplicationController - after_action :verify_authorized - - def index - authorize HtmlVariant - - relation = if params[:state] == "mine" - current_user.html_variants.order(created_at: :desc) - elsif params[:state] == "admin" - HtmlVariant.where(published: true, approved: false).order(created_at: :desc) - elsif params[:state].present? - HtmlVariant.where(published: true, approved: true, group: params[:state]).order(success_rate: :desc) - else - HtmlVariant.where(published: true, approved: true).order(success_rate: :desc) - end - - @html_variants = relation.includes(:user).page(params[:page]).per(30) - end - - def new - authorize HtmlVariant - @html_variant = HtmlVariant.new - return unless params[:fork_id] - - @fork = HtmlVariant.find(params[:fork_id]) - @html_variant.name = @fork.name + " FORK-#{rand(10_000)}" - @html_variant.html = @fork.html - 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 - end - - def create - authorize HtmlVariant - @html_variant = HtmlVariant.new(html_variant_params) - @html_variant.user_id = current_user.id - if @html_variant.save - flash[:success] = "HTML Variant Created" - redirect_to "/html_variants/#{@html_variant.id}/edit" - else - render :new - end - end - - def update - @html_variant = HtmlVariant.find(params[:id]) - authorize @html_variant - if @html_variant.update(html_variant_params) - flash[:success] = "HTML Variant Updated" - redirect_to "/html_variants/#{@html_variant.id}/edit" - else - render :edit - end - end - - private - - def html_variant_params - params.require(:html_variant).permit(policy(HtmlVariant).permitted_attributes) - end -end diff --git a/app/dashboards/dashboard_manifest.rb b/app/dashboards/dashboard_manifest.rb index 181497f9f..91788b88b 100644 --- a/app/dashboards/dashboard_manifest.rb +++ b/app/dashboards/dashboard_manifest.rb @@ -24,9 +24,6 @@ class DashboardManifest feedback_messages badges badge_achievements - html_variants - html_variant_trials - html_variant_successes sponsorships listing_categories ].freeze diff --git a/app/dashboards/html_variant_dashboard.rb b/app/dashboards/html_variant_dashboard.rb deleted file mode 100644 index b4eb8bf99..000000000 --- a/app/dashboards/html_variant_dashboard.rb +++ /dev/null @@ -1,79 +0,0 @@ -require "administrate/base_dashboard" - -class HtmlVariantDashboard < Administrate::BaseDashboard - # ATTRIBUTE_TYPES - # a hash that describes the type of each of the model's fields. - # - # Each different type represents an Administrate::Field object, - # which determines how the attribute is displayed - # on pages throughout the dashboard. - ATTRIBUTE_TYPES = { - user: Field::BelongsTo, - html_variant_trials: Field::HasMany, - html_variant_successes: Field::HasMany, - id: Field::Number, - group: Field::String, - name: Field::String, - html: Field::Text, - target_tag: Field::String, - success_rate: Field::Number.with_options(decimals: 2), - published: Field::Boolean, - approved: Field::Boolean, - created_at: Field::DateTime, - updated_at: Field::DateTime - }.freeze - - # COLLECTION_ATTRIBUTES - # an array of attributes that will be displayed on the model's index page. - # - # By default, it's limited to four items to reduce clutter on index pages. - # Feel free to add, remove, or rearrange items. - COLLECTION_ATTRIBUTES = %i[ - user - html_variant_trials - html_variant_successes - success_rate - id - ].freeze - - # SHOW_PAGE_ATTRIBUTES - # an array of attributes that will be displayed on the model's show page. - SHOW_PAGE_ATTRIBUTES = %i[ - user - html_variant_trials - html_variant_successes - id - group - name - html - target_tag - success_rate - published - approved - created_at - updated_at - ].freeze - - # FORM_ATTRIBUTES - # an array of attributes that will be displayed - # on the model's form (`new` and `edit`) pages. - FORM_ATTRIBUTES = %i[ - user - html_variant_trials - html_variant_successes - group - name - html - target_tag - success_rate - published - approved - ].freeze - - # Overwrite this method to customize how html variants are displayed - # across all pages of the admin dashboard. - # - # def display_resource(html_variant) - # "HtmlVariant ##{html_variant.id}" - # end -end diff --git a/app/dashboards/html_variant_success_dashboard.rb b/app/dashboards/html_variant_success_dashboard.rb deleted file mode 100644 index c6f7054ef..000000000 --- a/app/dashboards/html_variant_success_dashboard.rb +++ /dev/null @@ -1,58 +0,0 @@ -require "administrate/base_dashboard" - -class HtmlVariantSuccessDashboard < Administrate::BaseDashboard - # ATTRIBUTE_TYPES - # a hash that describes the type of each of the model's fields. - # - # Each different type represents an Administrate::Field object, - # which determines how the attribute is displayed - # on pages throughout the dashboard. - ATTRIBUTE_TYPES = { - html_variant: Field::BelongsTo, - article: Field::BelongsTo - }.freeze - - # COLLECTION_ATTRIBUTES - # an array of attributes that will be displayed on the model's index page. - # - # By default, it's limited to four items to reduce clutter on index pages. - # Feel free to add, remove, or rearrange items. - COLLECTION_ATTRIBUTES = %i[ - html_variant - article - ].freeze - - # SHOW_PAGE_ATTRIBUTES - # an array of attributes that will be displayed on the model's show page. - SHOW_PAGE_ATTRIBUTES = %i[ - html_variant - article - ].freeze - - # FORM_ATTRIBUTES - # an array of attributes that will be displayed - # on the model's form (`new` and `edit`) pages. - FORM_ATTRIBUTES = %i[ - html_variant - article - ].freeze - - # COLLECTION_FILTERS - # a hash that defines filters that can be used while searching via the search - # field of the dashboard. - # - # For example to add an option to search for open resources by typing "open:" - # in the search field: - # - # COLLECTION_FILTERS = { - # open: ->(resources) { where(open: true) } - # }.freeze - COLLECTION_FILTERS = {}.freeze - - # Overwrite this method to customize how html variant successes are displayed - # across all pages of the admin dashboard. - # - # def display_resource(html_variant_success) - # "HtmlVariantSuccess ##{html_variant_success.id}" - # end -end diff --git a/app/dashboards/html_variant_trial_dashboard.rb b/app/dashboards/html_variant_trial_dashboard.rb deleted file mode 100644 index 34d4cf75b..000000000 --- a/app/dashboards/html_variant_trial_dashboard.rb +++ /dev/null @@ -1,58 +0,0 @@ -require "administrate/base_dashboard" - -class HtmlVariantTrialDashboard < Administrate::BaseDashboard - # ATTRIBUTE_TYPES - # a hash that describes the type of each of the model's fields. - # - # Each different type represents an Administrate::Field object, - # which determines how the attribute is displayed - # on pages throughout the dashboard. - ATTRIBUTE_TYPES = { - html_variant: Field::BelongsTo, - article: Field::BelongsTo - }.freeze - - # COLLECTION_ATTRIBUTES - # an array of attributes that will be displayed on the model's index page. - # - # By default, it's limited to four items to reduce clutter on index pages. - # Feel free to add, remove, or rearrange items. - COLLECTION_ATTRIBUTES = %i[ - html_variant - article - ].freeze - - # SHOW_PAGE_ATTRIBUTES - # an array of attributes that will be displayed on the model's show page. - SHOW_PAGE_ATTRIBUTES = %i[ - html_variant - article - ].freeze - - # FORM_ATTRIBUTES - # an array of attributes that will be displayed - # on the model's form (`new` and `edit`) pages. - FORM_ATTRIBUTES = %i[ - html_variant - article - ].freeze - - # COLLECTION_FILTERS - # a hash that defines filters that can be used while searching via the search - # field of the dashboard. - # - # For example to add an option to search for open resources by typing "open:" - # in the search field: - # - # COLLECTION_FILTERS = { - # open: ->(resources) { where(open: true) } - # }.freeze - COLLECTION_FILTERS = {}.freeze - - # Overwrite this method to customize how html variant trials are displayed - # across all pages of the admin dashboard. - # - # def display_resource(html_variant_trial) - # "HtmlVariantTrial ##{html_variant_trial.id}" - # end -end diff --git a/app/lib/constants/role.rb b/app/lib/constants/role.rb index 589a98347..07aba0d99 100644 --- a/app/lib/constants/role.rb +++ b/app/lib/constants/role.rb @@ -17,6 +17,7 @@ module Constants "Resource Admin: FeedbackMessage", "Resource Admin: Config", "Resource Admin: Broadcast", + "Resource Admin: HtmlVariant", "Resource Admin: DisplayAd"].freeze end end diff --git a/app/models/html_variant.rb b/app/models/html_variant.rb index f1f0d11d4..0ee6a86ee 100644 --- a/app/models/html_variant.rb +++ b/app/models/html_variant.rb @@ -1,4 +1,6 @@ class HtmlVariant < ApplicationRecord + resourcify + GROUP_NAMES = %w[article_show_below_article_cta badge_landing_page campaign].freeze belongs_to :user, optional: true diff --git a/app/views/admin/html_variants/_form.html.erb b/app/views/admin/html_variants/_form.html.erb new file mode 100644 index 000000000..4d97ba71d --- /dev/null +++ b/app/views/admin/html_variants/_form.html.erb @@ -0,0 +1,38 @@ +<% if params[:action] == 'edit' %> +
+ <%= label_tag :user_id, "User ID:" %> + <%= text_field_tag :user_id, @html_variant.user_id, class: "form-control", disabled: true %> +
+<% end %> + +
+ <%= label_tag :name, "Name:" %> + <%= text_field_tag :name, @html_variant.name, class: "form-control" %> +
+ +
+ <%= label_tag :group, "Group:" %> + <%= select_tag :group, options_for_select(HtmlVariant::GROUP_NAMES, selected: @html_variant.group) %> +
+ +
+ <%= label_tag :target_tag, "Target Tag:" %> + <%= text_field_tag :target_tag, @html_variant.target_tag, size: "100x10", class: "form-control" %> +
+ +
+ <%= label_tag :html, "HTML:" %> + <%= text_area_tag :html, @html_variant.html, size: "100x10", class: "form-control" %> +
+ +
+ <%= label_tag :published, "Published:" %> + <%= select_tag :published, options_for_select([false, true], selected: @html_variant.published) %> +
+ +<% if params[:action] == 'edit' %> +
+ <%= label_tag :approved, "Approved:" %> + <%= select_tag :approved, options_for_select([false, true], selected: @html_variant.approved) %> +
+<% end %> diff --git a/app/views/admin/html_variants/edit.html.erb b/app/views/admin/html_variants/edit.html.erb new file mode 100644 index 000000000..0cc2caf9b --- /dev/null +++ b/app/views/admin/html_variants/edit.html.erb @@ -0,0 +1,9 @@ +

Edit HTML Variant:

+
+ <%= form_for([:admin, @html_variant], method: :patch) do %> + <%= render "form" %> + <%= submit_tag "Update HTML Variant", class: "crayons-btn" %> + <%= link_to "Preview", admin_html_variant_path(@html_variant), class: "crayons-btn crayons-btn--outlined float-right mx-2" %> + <%= link_to "Fork", new_admin_html_variant_path(fork_id: @html_variant.id), class: "crayons-btn crayons-btn--outlined float-right" %> + <% end %> +
diff --git a/app/views/admin/html_variants/index.html.erb b/app/views/admin/html_variants/index.html.erb new file mode 100644 index 000000000..6a7369a89 --- /dev/null +++ b/app/views/admin/html_variants/index.html.erb @@ -0,0 +1,54 @@ + + +
+ +<%= paginate @html_variants %> + + + + + + + + + + + + + + <% @html_variants.each do |html_variant| %> + + + + + + + + + + + <% end %> + +
NameUserGroupPublishedApprovedActions
<%= link_to html_variant.name, admin_html_variant_path(html_variant) %><%= link_to "@#{html_variant.user.username}", "/#{html_variant.user.username}" %><%= html_variant.group %><%= html_variant.published %><%= html_variant.approved %> + <%= link_to "Fork", new_admin_html_variant_path(fork_id: html_variant.id), class: "crayons-btn crayons-btn--outlined" %> + + <%= link_to "Edit", edit_admin_html_variant_path(html_variant), class: "crayons-btn" %> + + <%= link_to "Destroy", admin_html_variant_path(html_variant), class: "crayons-btn crayons-btn--danger", method: :delete, data: { confirm: "Are you sure?" } %> +
+ +<%= paginate @html_variants %> diff --git a/app/views/admin/html_variants/new.html.erb b/app/views/admin/html_variants/new.html.erb new file mode 100644 index 000000000..8d4635d55 --- /dev/null +++ b/app/views/admin/html_variants/new.html.erb @@ -0,0 +1,7 @@ +

Make a new HTML Variant:

+
+ <%= form_for([:admin, @html_variant], method: :post) do %> + <%= render "form" %> + <%= submit_tag "Create HTML Variant", class: "crayons-btn" %> + <% end %> +
diff --git a/app/views/admin/html_variants/show.html.erb b/app/views/admin/html_variants/show.html.erb new file mode 100644 index 000000000..3ff1468a7 --- /dev/null +++ b/app/views/admin/html_variants/show.html.erb @@ -0,0 +1,7 @@ +
+ HMTL Variant Preview +
+ <%= link_to "Back to admin page", "javascript:history.back()" %> +
+ +<%= @html_variant.html.html_safe %> diff --git a/app/views/html_variants/_form.html.erb b/app/views/html_variants/_form.html.erb deleted file mode 100644 index d3c53c10f..000000000 --- a/app/views/html_variants/_form.html.erb +++ /dev/null @@ -1,42 +0,0 @@ -<% flash.each do |key, value| %> -
<%= value %>
-<% end %> - -<% if @html_variant.errors.any? %> -
-

<%= pluralize(@html_variant.errors.count, "error") %> prohibited this block from being saved:

- -
    - <% @html_variant.errors.full_messages.each do |message| %> -
  • <%= message %>
  • - <% end %> -
-
-<% end %> - -<%= form_for(@html_variant) do |f| %> - <% if @html_variant.new_record? %> - <%= f.label :name %> - <%= f.text_field :name, placeholder: "Unique, descriptive name" %> - <% end %> - <%= f.label :html %> - <%= f.text_area :html, placeholder: "HTML to be shown. Make sure all CSS is properly scoped, and all HTML tags are closed, etc. Mandatory field." %> -
- <%= f.label :published %> - <%= f.check_box :published %> - <%= f.label :group %> - <%= f.select(:group, options_for_select(HtmlVariant::GROUP_NAMES, @html_variant.group || "article_show_below_article_cta")) %> -
- <%= f.submit %> -<% end %> -<% if @html_variant.html.present? %> -

Preview:

-
- <%= @html_variant.html.html_safe %> -
- <% if @html_variant.group == "article_show_below_article_cta" %> -
- <%= @html_variant.html.html_safe %> -
- <% end %> -<% end %> diff --git a/app/views/html_variants/_page_styles.html.erb b/app/views/html_variants/_page_styles.html.erb deleted file mode 100644 index 0270646e3..000000000 --- a/app/views/html_variants/_page_styles.html.erb +++ /dev/null @@ -1,144 +0,0 @@ - diff --git a/app/views/html_variants/_single_html_variant.html.erb b/app/views/html_variants/_single_html_variant.html.erb deleted file mode 100644 index 074d91d9a..000000000 --- a/app/views/html_variants/_single_html_variant.html.erb +++ /dev/null @@ -1,23 +0,0 @@ -
"> - <%= html_variant.group %> | @<%= html_variant.user.username %>: -

- <%= html_variant.name %> -

- -
- <% if html_variant.published %> - published - fork - <% else %> - view/edit - <% end %> - <% if html_variant.approved %> - <%= html_variant.success_rate %> - <% elsif admin %> - <%= form_for(html_variant) do |f| %> - <%= f.hidden_field :approved, value: "true" %> - <%= f.submit "APPROVE" %> - <% end %> - <% end %> -
-
diff --git a/app/views/html_variants/edit.html.erb b/app/views/html_variants/edit.html.erb deleted file mode 100644 index 0a2524a52..000000000 --- a/app/views/html_variants/edit.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -<%= render "page_styles" %> - -
-
- 👈 View All -

<%= @html_variant.name %>

- <%= render "form" %> -
-
diff --git a/app/views/html_variants/index.html.erb b/app/views/html_variants/index.html.erb deleted file mode 100644 index 485dcacf9..000000000 --- a/app/views/html_variants/index.html.erb +++ /dev/null @@ -1,26 +0,0 @@ -<%= render "page_styles" %> - -
-

- HTML Variants New -

- - <% if params[:state] == "mine" %> -

My Entries

- <% elsif params[:state] == "admin" %> -

All Published Entries

- <% else %> -

Leaderboard

- <% end %> - <% @html_variants.each do |html_variant| %> - <%= render "single_html_variant", html_variant: html_variant, admin: params[:state] == "admin" %> - <% end %> -
- -<%= paginate @html_variants %> diff --git a/app/views/html_variants/new.html.erb b/app/views/html_variants/new.html.erb deleted file mode 100644 index a53c018ab..000000000 --- a/app/views/html_variants/new.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -<%= render "page_styles" %> -
-
- 👈 View All - <%= render "form" %> -
-
diff --git a/app/views/html_variants/show.html.erb b/app/views/html_variants/show.html.erb deleted file mode 100644 index 687bd3465..000000000 --- a/app/views/html_variants/show.html.erb +++ /dev/null @@ -1,24 +0,0 @@ - -<%= render "layouts/styles" %> - -<% if @html_variant.group == "article_show_below_article_cta" %> -
-
- <%= @html_variant.html.html_safe %> -
-
-<% else %> -
-
- <%= @html_variant.html.html_safe %> -
-
-<% end %> diff --git a/config/routes.rb b/config/routes.rb index f45f31941..7faa1310e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -126,6 +126,7 @@ Rails.application.routes.draw do resource :config resources :badges, only: %i[index edit update new create] resources :display_ads, only: %i[index edit update new create destroy] + resources :html_variants, only: %i[index edit update new create show destroy] # These redirects serve as a safegaurd to prevent 404s for any Admins # who have the old badge_achievement URLs bookmarked. get "/badges/badge_achievements", to: redirect("/admin/badge_achievements") @@ -249,7 +250,6 @@ Rails.application.routes.draw do resources :videos, only: %i[index create new] resources :video_states, only: [:create] resources :twilio_tokens, only: [:show] - resources :html_variants, only: %i[index new create show edit update] resources :html_variant_trials, only: [:create] resources :html_variant_successes, only: [:create] resources :tag_adjustments, only: %i[create destroy] diff --git a/spec/requests/admin/html_variants_spec.rb b/spec/requests/admin/html_variants_spec.rb new file mode 100644 index 000000000..273241d46 --- /dev/null +++ b/spec/requests/admin/html_variants_spec.rb @@ -0,0 +1,139 @@ +require "rails_helper" +require "requests/shared_examples/internal_policy_dependant_request" + +RSpec.describe "/admin/html_variants", type: :request do + let(:get_resource) { get "/admin/html_variants" } + let(:params) do + { name: "Banner", html: "

Hello HTML Variants!

", group: "campaign", + approved: true, published: true } + end + let(:post_resource) { post "/admin/html_variants", params: params } + + it_behaves_like "an InternalPolicy dependant request", HtmlVariant do + let(:request) { get_resource } + end + + context "when the user is not an admin" do + let(:user) { create(:user) } + + before { sign_in user } + + describe "GET /admin/html_variants" do + it "blocks the request" do + expect { get_resource }.to raise_error(Pundit::NotAuthorizedError) + end + end + + describe "POST /admin/html_variants" do + it "blocks the request" do + expect { post_resource }.to raise_error(Pundit::NotAuthorizedError) + end + end + end + + context "when the user is a super admin" do + let(:super_admin) { create(:user, :super_admin) } + + before { sign_in super_admin } + + describe "GET /admin/html_variants" do + it "allows the request" do + get_resource + expect(response).to have_http_status(:ok) + end + end + + describe "POST /admin/html_variants" do + it "creates a new html_variant" do + expect do + post_resource + end.to change { HtmlVariant.all.count }.by(1) + end + end + + describe "PUT /admin/html_variants" do + let!(:html_variant) { create(:html_variant, approved: false) } + + it "updates HtmlVariant's approved value" do + Timecop.freeze(Time.current) do + expect do + put "/admin/html_variants/#{html_variant.id}", params: params + end.to change { html_variant.reload.approved }.from(false).to(true) + end + end + end + + describe "DELETE /admin/html_variants/:id" do + let!(:html_variant) { create(:html_variant) } + + it "deletes the Display Ad" do + expect do + delete "/admin/html_variants/#{html_variant.id}" + end.to change { HtmlVariant.all.count }.by(-1) + expect(response.body).to redirect_to "/admin/html_variants" + end + end + end + + context "when the user is a single resource admin" do + let(:single_resource_admin) { create(:user, :single_resource_admin, resource: HtmlVariant) } + + before { sign_in single_resource_admin } + + describe "GET /admin/html_variants" do + it "allows the request" do + get_resource + expect(response).to have_http_status(:ok) + end + end + + describe "POST /admin/html_variants" do + it "creates a new html_variant" do + expect do + post_resource + end.to change { HtmlVariant.all.count }.by(1) + end + end + + describe "PUT /admin/html_variants" do + let!(:html_variant) { create(:html_variant, approved: false) } + + it "updates HtmlVariant's approved value" do + Timecop.freeze(Time.current) do + expect do + put "/admin/html_variants/#{html_variant.id}", params: params + end.to change { html_variant.reload.approved }.from(false).to(true) + end + end + end + + describe "DELETE /admin/html_variants/:id" do + let!(:html_variant) { create(:html_variant) } + + it "deletes the Display Ad" do + expect do + delete "/admin/html_variants/#{html_variant.id}" + end.to change { HtmlVariant.all.count }.by(-1) + expect(response.body).to redirect_to "/admin/html_variants" + end + end + end + + context "when the user is the wrong single resource admin" do + let(:single_resource_admin) { create(:user, :single_resource_admin, resource: Article) } + + before { sign_in single_resource_admin } + + describe "GET /admin/html_variants" do + it "blocks the request" do + expect { get_resource }.to raise_error(Pundit::NotAuthorizedError) + end + end + + describe "POST /admin/html_variants" do + it "blocks the request" do + expect { post_resource }.to raise_error(Pundit::NotAuthorizedError) + end + end + end +end diff --git a/spec/requests/html_variants_spec.rb b/spec/requests/html_variants_spec.rb deleted file mode 100644 index 0efa660bb..000000000 --- a/spec/requests/html_variants_spec.rb +++ /dev/null @@ -1,109 +0,0 @@ -require "rails_helper" - -RSpec.describe "HtmlVariants", type: :request do - let(:user) { create(:user) } - let(:article) { create(:article, user_id: user.id, approved: true) } - - before do - sign_in user - end - - describe "GET /html_variants" do - it "rejects non-permissioned user" do - expect { get "/html_variants" }.to raise_error(Pundit::NotAuthorizedError) - end - - it "accepts permissioned" do - user.add_role(:super_admin) - get "/html_variants" - expect(response.body).to include("HTML Variants") - end - end - - describe "GET /html_variants/new" do - it "rejects non-permissioned user" do - expect { get "/html_variants/new" }.to raise_error(Pundit::NotAuthorizedError) - end - - it "accepts permissioned" do - user.add_role(:super_admin) - get "/html_variants/new" - expect(response.body).to include("