Skip to content

Commit

Permalink
Switch themes using a POST request
Browse files Browse the repository at this point in the history
- Route switchto action via POST
- Replace links in themes admin with a single button
  • Loading branch information
mvz committed Oct 10, 2021
1 parent 8c39915 commit b4b7408
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Expand Up @@ -105,6 +105,10 @@ Rails/SkipsModelValidations:
Exclude:
- 'db/migrate/*'

Capybara/FeatureMethods:
Exclude:
- 'spec/features/**_spec.rb'

# Allow the use of 'and' 'or' in control structures.
Style/AndOr:
EnforcedStyle: conditionals
Expand Down
6 changes: 3 additions & 3 deletions publify_core/app/views/admin/themes/index.html.erb
Expand Up @@ -16,10 +16,10 @@
</div>
<% else %>
<div>
<h3><%= link_to(theme.name, switch_url, title: t('.use_this_theme')) %></h3>
<%= link_to(image_tag(preview_url, class: 'img-thumbnail'), switch_url, title: t('.use_this_theme')) %>
<h3><%= theme.name %></h3>
<%= image_tag(preview_url, class: 'img-thumbnail') %>
<%= raw theme.description_html %>
<p><%= link_to(t('.use_this_theme'), switch_url, class: 'btn btn-info') %></p>
<p><%= button_to(t('.use_this_theme'), switch_url, class: 'btn btn-info') %></p>
</div>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion publify_core/config/routes.rb
Expand Up @@ -144,7 +144,7 @@
resources :themes, only: [:index], format: false do
collection do
get "preview"
get "switchto"
post "switchto"
end
end

Expand Down
Expand Up @@ -23,7 +23,7 @@
end

it "redirects to :index after the :switchto action" do
get :switchto, params: { theme: "typographic" }
post :switchto, params: { theme: "typographic" }
assert_response :redirect, action: "index"
end

Expand Down
1 change: 1 addition & 0 deletions publify_core/spec/rails_helper.rb
Expand Up @@ -74,6 +74,7 @@
# Test helpers needed for Devise
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature
config.include Devise::Test::IntegrationHelpers, type: :request

# Test helpers to check feed contents
config.include PublifyCore::TestingSupport::FeedAssertions, type: :view
Expand Down
25 changes: 25 additions & 0 deletions publify_core/spec/requests/admin/themes_spec.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Admin::Themes", type: :request do
before do
create(:blog)
henri = create(:user, :as_admin)
sign_in henri
end

describe "GET /admin/themes/switchto" do
it "is not available" do
expect { get switchto_admin_themes_path(theme: "bootstrap-2") }.
to raise_error ActiveRecord::RecordNotFound
end
end

describe "POST /admin/themes/switchto" do
it "redirects to the themes list" do
post switchto_admin_themes_path(theme: "bootstrap-2")
expect(response).to redirect_to admin_themes_path
end
end
end
24 changes: 24 additions & 0 deletions spec/features/switch_theme_spec.rb
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.feature "Changing themes", type: :feature do
let(:admin) { create :user, :as_admin }
let(:blog) { Blog.first }

before do
load Rails.root.join("db/seeds.rb")
Blog.first.update blog_name: "Awesome!", base_url: "http://www.example.com/"
end

scenario "switching themes by clicking links in the themes admin" do
sign_in admin
visit "/admin/themes"

expect(page).to have_text "plain - Active theme"

click_link_or_button "Use this theme"

expect(page).to have_text "bootstrap-2 - Active theme"
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Expand Up @@ -33,6 +33,7 @@ class ActionView::TestCase::TestController

# Test helpers needed for Devise
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature

# Test helpers to check feed contents
config.include PublifyCore::TestingSupport::FeedAssertions, type: :controller
Expand Down

0 comments on commit b4b7408

Please sign in to comment.