fix issue #69 (Passing params using hidden tags is broken for model_form)

did committed Jul 11, 2016
commit 19920a6f1379180fad99f87eaf9ba235c43c5f2e
Showing 4 changed files with 32 additions and 5 deletions
locomotive/steam/middlewares/entry_submission.rb b/lib/locomotive/steam/middlewares/entry_submission.rb +23 -4
@@ @@ -11,6 +11,7 @@ module Locomotive::Steam
ENTRY_SUBMISSION_REGEXP = /^\/entry_submissions\/(\w+)/o
SUBMITTED_TYPE_PARAM = 'submitted_type_slug'
SUBMITTED_PARAM = 'submitted_entry_slug'
+ CONTENT_TYPE_PARAM = 'content_type_slug'
def _call
# we didn't go through the locale middleware yet,
@@ @@ -72,8 +73,26 @@ module Locomotive::Steam
end
def entry_to_query_string(entry)
- type, slug = entry.content_type_slug, entry._slug
- "#{SUBMITTED_TYPE_PARAM}=#{type}&#{SUBMITTED_PARAM}=#{slug}"
+ service_params = [
+ services.csrf_protection.field,
+ CONTENT_TYPE_PARAM,
+ SUBMITTED_TYPE_PARAM,
+ SUBMITTED_PARAM,
+ 'success_callback',
+ 'error_callback',
+ 'content',
+ 'entry'
+ ]
+
+ [].tap do |list|
+ params.each do |key, value|
+ next if service_params.include?(key)
+ list << "#{key}=#{value}"
+ end
+
+ list << "#{SUBMITTED_TYPE_PARAM}=#{entry.content_type_slug}"
+ list << "#{SUBMITTED_PARAM}=#{entry._slug}"
+ end.join('&')
end
def with_locale(&block)
@@ @@ -109,8 +128,8 @@ module Locomotive::Steam
# or from the presence of the content_type_slug param (model_form tag).
#
def get_content_type_slug
- if request.post? && (request.path_info =~ ENTRY_SUBMISSION_REGEXP || params[:content_type_slug])
- $1 || params[:content_type_slug]
+ if request.post? && (request.path_info =~ ENTRY_SUBMISSION_REGEXP || params[CONTENT_TYPE_PARAM])
+ $1 || params[CONTENT_TYPE_PARAM]
end
end
spec/fixtures/default/app/views/pages/contact.liquid.haml +2 -0
@@ @@ -22,6 +22,8 @@ position: 4
/ %input{ type: 'hidden', name: 'success_callback', value: '/events' }
/ %input{ type: 'hidden', name: 'error_callback', value: '/contact' }
+ %input{ type: 'hidden', name: 'some_variable', value: '42' }
+
%p
%label{ :for => 'name' } Name
%input{ :type => 'text', :id => 'name', :name => 'content[name]', :placeholder => 'First and last name', :tabindex => '1', required: 'required', value: '{{ message.name }}' }
spec/fixtures/default/app/views/pages/events.liquid.haml +2 -1
@@ @@ -10,9 +10,10 @@ position: 5
%p Form with errors
{% endif %}
-
%p Thank you {{ message.name }} !
+ %p Some variable: {{ params.some_variable }}
+
#events.unit.size2of3
%h2 Upcoming events
spec/integration/server/contact_form_spec.rb +5 -0
@@ @@ -138,6 +138,7 @@ describe 'ContactForm' do
let(:url) { '/events' }
let(:params) { {
'content_type_slug' => 'messages',
+ 'some_variable' => '42',
'entry' => { 'name' => 'John', 'email' => 'j@doe.net', 'message' => 'Bla bla' } } }
let(:response) { post_contact_form(url, params) }
let(:status) { response.status }
@@ @@ -168,6 +169,10 @@ describe 'ContactForm' do
expect(response.body.to_s).to include 'Thank you John'
end
+ it 'stores hidden fields from the form' do
+ expect(response.body.to_s).to include 'Some variable: 42'
+ end
+
end
context 'in a different locale' do