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

Use EssencePage in contact forms #1640

Merged
merged 3 commits into from
Oct 16, 2019
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
31 changes: 23 additions & 8 deletions app/controllers/alchemy/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Alchemy
# - name: mail_from
# type: EssenceText
# - name: success_page
# type: EssenceSelect
# type: EssencePage
#
# The fields +mail_to+, +mail_from+, +subject+ and +success_page+ are recommended.
# The +Alchemy::MessagesController+ uses them to send your mails. So your customer has full controll of these values inside his contactform element.
Expand All @@ -32,14 +32,16 @@ module Alchemy
# elements: [pageheading, heading, contactform]
# autogenerate: [contactform]
#
# Disabling the page caching is stronlgy recommended!
# Disabling the page caching is strongly recommended!
#
# The editor view for your element should have this layout:
#
# <%= render_essence_editor_by_name(element, 'mail_from') %>
# <%= render_essence_editor_by_name(element, 'mail_to') %>
# <%= render_essence_editor_by_name(element, 'subject') %>
# <%= page_selector(element, 'success_page', page_attribute: :urlname) %>
# <%= element_editor_for(element) do |el| %>
# <%= el.edit :mail_from %>
# <%= el.edit :mail_to %>
# <%= el.edit :subject %>
# <%= el.edit :success_page %>
# <% end %>
#
# Please have a look at the +alchemy/config/config.yml+ file for further Message settings.
#
Expand Down Expand Up @@ -97,8 +99,8 @@ def subject

def redirect_to_success_page
flash[:notice] = Alchemy.t(:success, scope: 'contactform.messages')
if @element.ingredient(:success_page)
urlname = @element.ingredient(:success_page)
if success_page
urlname = success_page_urlname
elsif mailer_config['forward_to_page'] && mailer_config['mail_success_page']
urlname = Page.find_by(urlname: mailer_config['mail_success_page']).urlname
else
Expand All @@ -110,6 +112,19 @@ def redirect_to_success_page
)
end

def success_page
@_success_page ||= @element.ingredient(:success_page)
end

def success_page_urlname
case success_page
when Alchemy::Page
success_page.urlname
when String
success_page
end
end

def get_page
@page = Language.current.pages.find_by(page_layout: mailer_config['page_layout_name'])
if @page.blank?
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/alchemy/admin/essences_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def pages_attributes_for_select(pages, page_attribute, indent = false)
#
def page_name_attribute_for_select(page, indent = false)
if indent
("&nbsp;&nbsp;" * (page.level - 1) + page.name).html_safe
("&nbsp;&nbsp;" * (page.depth - 1) + page.name).html_safe
else
page.name
end
Expand Down
30 changes: 23 additions & 7 deletions spec/controllers/alchemy/messages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,31 @@ module Alchemy
end

describe "#redirect_to_success_page" do
context "if 'success_page' ingredient of element is set with urlname" do
before do
allow(element).to receive(:ingredient).with(:success_page).and_return('success-page')
context "if 'success_page' ingredient of element" do
context 'is set with urlname string' do
before do
allow(element).to receive(:ingredient).with(:success_page).and_return('success-page')
end

it "should redirect to the given urlname" do
expect(
subject
).to redirect_to(show_page_path(urlname: 'success-page'))
end
end

it "should redirect to the given urlname" do
expect(
subject
).to redirect_to(show_page_path(urlname: 'success-page'))
context 'is set with page instance' do
let(:page) { build(:alchemy_page, name: 'Success', urlname: 'success-page') }

before do
allow(element).to receive(:ingredient).with(:success_page).and_return(page)
end

it "should redirect to the given urlname" do
expect(
subject
).to redirect_to(show_page_path(urlname: 'success-page'))
end
end
end

Expand Down
31 changes: 19 additions & 12 deletions spec/dummy/app/views/alchemy/elements/_contactform_view.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<%- cache(contactform_view) do -%>
<%= element_view_for(contactform_view) do |el| -%>
<div class="mail_from">
<%= el.render :mail_from %>
</div>
<div class="mail_to">
<%= el.render :mail_to %>
</div>
<div class="subject">
<%= el.render :subject %>
</div>
<div class="success_page">
<%= el.render :success_page %>
</div>
<form action="/messages" method="POST">
<div>
<label>
Name<br>
<input type="text" placeholder="Your name">
</label>
</div>
<div>
<label>
Email<br>
<input type="email" placeholder="Your email">
</label>
</div>
<div>
<label>Comment</label><br>
<textarea></textarea>
</div>
<button>Send</button>
</form>
<%- end -%>
<%- end -%>
2 changes: 1 addition & 1 deletion spec/dummy/config/alchemy/elements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
validate:
- presence
- name: success_page
type: EssenceSelect
type: EssencePage
validate:
- presence

Expand Down
5 changes: 2 additions & 3 deletions spec/features/admin/attachment_assignment_overlay_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

describe "filter by tags", js: true do
let(:a_page) { create(:alchemy_page, autogenerate_elements: true) }
let(:element) { create(:alchemy_element, page: a_page, name: 'download') }
let!(:file1) { create(:alchemy_attachment, file_name: "job_alert.png", tag_list: "jobs") }
let!(:file2) { create(:alchemy_attachment, file_name: "keynote.png", tag_list: "presentations") }

Expand All @@ -20,9 +19,9 @@
click_on "Assign a file"
end

within ".alchemy-dialog.modal", wait: 15 do
within ".alchemy-dialog.modal" do
# We expect to see both attachments
expect(page).to have_selector("#assign_file_list .list a", count: 2)
expect(page).to have_selector("#assign_file_list .list a", count: 2, wait: 10)

# Click on a tag to filter the attachments
within ".tag-list" do
Expand Down