Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect Language public status for page public status #2017

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/assets/stylesheets/alchemy/buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ input.button {

&[disabled] {
cursor: not-allowed;

& + label {
display: none;
}
}
}

Expand Down
11 changes: 0 additions & 11 deletions app/models/alchemy/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class Language < BaseRecord
after_update :set_pages_language,
if: :should_set_pages_language?

after_update :unpublish_pages,
if: :should_unpublish_pages?

before_destroy if: -> { pages.any? } do
errors.add(:pages, :still_present)
throw(:abort)
Expand Down Expand Up @@ -170,13 +167,5 @@ def should_set_pages_language?
def set_pages_language
pages.update_all language_code: code
end

def should_unpublish_pages?
saved_changes[:public] == [true, false]
end

def unpublish_pages
pages.update_all(public_on: nil, public_until: nil)
end
end
end
2 changes: 1 addition & 1 deletion app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Page::PageNatures

def public?
current_time = Time.current
already_public_for?(current_time) && still_public_for?(current_time)
language.public? && already_public_for?(current_time) && still_public_for?(current_time)
end

def expiration_time
Expand Down
1 change: 1 addition & 0 deletions app/models/alchemy/page/page_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ module ClassMethods
# All public pages
#
def published
joins(:language).merge(Language.published).
where("#{table_name}.public_on <= :time AND " \
"(#{table_name}.public_until IS NULL " \
"OR #{table_name}.public_until >= :time)", time: Time.current)
Expand Down
32 changes: 22 additions & 10 deletions app/views/alchemy/admin/pages/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,35 @@
<% end %>
<label><%= Alchemy.t(:page_properties) %></label>
</div>
<% if can?(:publish, @page) %>
<div class="button_with_label">
<%= form_tag alchemy.publish_admin_page_path(@page), id: 'publish_page_form' do %>
<%= button_tag class: 'icon_button', title: Alchemy.t(:explain_publishing) do %>
<%= render_icon('cloud-upload-alt') %>
<% end %>
<label><%= Alchemy.t("Publish page") %></label>
<div class="button_with_label">
<%= form_tag alchemy.publish_admin_page_path(@page), id: 'publish_page_form' do %>
<%= button_tag class: 'icon_button', disabled: cannot?(:publish, @page) do %>
<%= render_icon('cloud-upload-alt') %>
<% end %>
</div>
<% end %>
<label>
<% if @page.language.public? %>
<%= Alchemy.t(:explain_publishing) %>
<% elsif @page.editable_by?(current_alchemy_user) %>
<%= Alchemy.t(:publish_page_language_not_public) %>
<% else %>
<%= Alchemy.t(:publish_page_not_allowed) %>
<% end %>
</label>
<% end %>
</div>
<% unless @page.layoutpage? %>
<div class="button_with_label">
<%= form_tag alchemy.visit_admin_page_path(@page), id: 'visit_page_form' do %>
<%= button_tag class: 'icon_button', disabled: !@page.public? do %>
<%= render_icon('external-link-alt') %>
<% end %>
<label><%= Alchemy.t("Visit page") %></label>
<label>
<% if @page.public? %>
<%= Alchemy.t("Visit page") %>
<% else %>
<%= Alchemy.t(:cannot_visit_unpublic_page) %>
<% end %>
</label>
<% end %>
</div>
<% end %>
Expand Down
6 changes: 4 additions & 2 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ en:
"Please log in": "Please log in."
"Please seperate the tags with commata": "* Please seperate the tags with commas."
"Properties": "Properties"
"Publish page": "Publish page"
"Read the License": "Read the License"
"Redirects to": "Redirects to"
"Reload Preview": "Reload Preview"
Expand Down Expand Up @@ -334,6 +333,7 @@ en:
big_thumbnails: "Big thumbnails"
cancel: "cancel"
cannot_delete_picture_notice: "Cannot delete Picture %{name}, because it's still in use."
cannot_visit_unpublic_page: "Publish page before visiting it."
choose_file_to_link: "Please choose a file to link"
"clear clipboard": "clear clipboard"
click_to_show_all: "Click to show all again."
Expand Down Expand Up @@ -390,7 +390,7 @@ en:
element_saved: "Saved element."
enter_external_link: "Please enter the URL you want to link with"
explain_cropping: "<p>Move the frame and change its size with the mouse or arrow keys to adjust the image mask. Click on \"apply\" when you are satisfied with your selection.</p><p>If you want to return to the original centered image mask like it was defined in the layout, click \"reset\" and \"apply\" afterwards.</p>"
explain_publishing: "Publish the page and remove the cached version from the server."
explain_publishing: "Publish current page content"
explain_sitemap_dragndrop_sorting: "Tip: Drag the pages at the icon in order to sort them."
explain_unlocking: "Leave page and unlock it for other users."
external_link_notice_1: "Please enter the complete url with http:// or a similar protocol."
Expand Down Expand Up @@ -535,6 +535,8 @@ en:
'1024': '1024px (iPad - Landscape)'
'1280': '1280px (Desktop)'
preview_url: Preview
publish_page_language_not_public: Cannot publish page if language is not public
publish_page_not_allowed: You have not the permission to publish this page
recently_uploaded_only: 'Recently uploaded only'
"regular method": "Regular method"
remove: "Remove"
Expand Down
5 changes: 4 additions & 1 deletion lib/alchemy/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,12 @@ def alchemy_editor_rules
can([
:create,
:destroy,
:publish,
], Alchemy::Page) { |p| p.editable_by?(@user) }

can(:publish, Alchemy::Page) do |page|
page.language.public? && page.editable_by?(@user)
end

can :manage, Alchemy::Picture
can :manage, Alchemy::Attachment
can :manage, Alchemy::Tag
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/page_editing_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

it "cannot publish page." do
visit alchemy.edit_admin_page_path(a_page)
expect(page).to_not have_selector("#publish_page_form")
expect(page).to have_selector("#publish_page_form button[disabled]")
end

describe "the preview frame", :js do
Expand Down
18 changes: 16 additions & 2 deletions spec/libraries/permissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,22 @@
is_expected.to be_able_to(:switch_language, Alchemy::Page)
end

it "can publish pages" do
is_expected.to be_able_to(:publish, Alchemy::Page)
context "if page language is public" do
let(:language) { create(:alchemy_language, :german, public: true) }
let(:page) { create(:alchemy_page, language: language) }

it "can publish pages" do
is_expected.to be_able_to(:publish, page)
end
end

context "if page language is not public" do
let(:language) { create(:alchemy_language, :german, public: false) }
let(:page) { create(:alchemy_page, language: language) }

it "cannot publish pages" do
is_expected.to_not be_able_to(:publish, page)
end
end

it "can manage attachments" do
Expand Down
10 changes: 0 additions & 10 deletions spec/models/alchemy/language_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ module Alchemy
expect([page.language_code, @other_page.language_code]).to eq([language.code, language.code])
end
end

describe "#unpublish_pages" do
it "should set all pages to unpublic if it gets set to unpublic" do
page = create(:alchemy_page, language: language)
@other_page = create(:alchemy_page, language: language)
language.update(public: false)
language.reload; page.reload; @other_page.reload
expect([page.public?, @other_page.public?]).to eq([false, false])
end
end
end

describe ".default" do
Expand Down
10 changes: 10 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,14 @@ class AnotherUrlPathClass; end
let!(:public_one) { create(:alchemy_page, :public) }
let!(:public_two) { create(:alchemy_page, :public) }
let!(:non_public_page) { create(:alchemy_page) }
let!(:page_with_non_public_language) { create(:alchemy_page, :public, language: non_public_language) }
let(:non_public_language) { create(:alchemy_language, :german, public: false) }

it "returns public available pages" do
expect(published).to include(public_one)
expect(published).to include(public_two)
expect(published).to_not include(non_public_page)
expect(published).to_not include(page_with_non_public_language)
end
end

Expand Down Expand Up @@ -1314,6 +1317,13 @@ class AnotherUrlPathClass; end

it { is_expected.to be(false) }
end

context "when language is not public" do
let(:language) { create(:alchemy_language, public: false, default: false) }
let(:page) { create(:alchemy_page, :public, language: language) }

it { is_expected.to be(false) }
end
end

describe "#publish!" do
Expand Down