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

AC-14, persist #55

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion app/admin/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
menu priority: 10
permit_params :name, :style, :title, :body, :redirect_url, :action, :success, :created_by_id,
:use_recaptcha, :recaptcha_key, :recaptcha_secret, :origin,
form_fields_attributes: [:id, :field_id, :label, :help, :required, :placeholder, :position, :_destroy,
form_fields_attributes: [:id, :field_id, :label, :help, :required, :placeholder, :position, :persist, :_destroy,
campaign_options_attributes: %i[id campaign_code label position _destroy],],
campaign_codes: []

Expand Down Expand Up @@ -87,6 +87,7 @@
fields_f.input :placeholder, hint: "Override field placeholder."
fields_f.input :help, hint: "Optional help message."
fields_f.input :required, as: :boolean, label: "Is field required?"
fields_f.input :persist, as: :boolean, label: "Store/retrieve value from other forms?"
fields_f.has_many :campaign_options, allow_destroy: true, sortable: :position,
heading: "Campaigns" do |campaigns_f|
campaigns_f.inputs do
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/campaign-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ if (typeof window.campaignForms === 'undefined') {
// Disable and hide form
form.addClass('hidden').find('input, button').prop('disabled', 'disabled')
}

// MK: Stow any fields of class "cfpersisted" in well-named sessionStorage key
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...where "well-named ... key" probably means using the id of the field with the trailing number stripped off. That is, cf_email_address_457 becomes cf_email_address.

},
error: function (xhr) {
var errors = xhr.responseJSON || {}
Expand Down Expand Up @@ -134,6 +136,10 @@ if (typeof window.campaignForms === 'undefined') {
$(recaptchaDiv).attr('data-callback', recaptchaCallback)
window[recaptchaCallback] = window.campaignForms[formId].recaptchaCallback
}

// MK: For each field of class "cfpersisted", attempt to retrieve well-named sessionStorage key
// When successful, replace default value and set readonly HTML attribute
// https://stackoverflow.com/questions/7357256/disabled-form-inputs-do-not-appear-in-the-request
}
})
}
Expand All @@ -155,4 +161,4 @@ $(document).ready(function() {
$("[id^='cf_US_State_']").show()
}
})
})
})
2 changes: 1 addition & 1 deletion app/views/fields/_input.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group">
<label for="<%= "cf_#{field.name}_#{field.id}" %>"><%= field.label_value %></label>
<input id="<%= "cf_#{field.name}_#{field.id}" %>" class="form-control" type="<%= field.input %>" name="<%= field.name %>" placeholder="<%= field.placeholder_value %>" <%== 'required' if field.required %>>
<input id="<%= "cf_#{field.name}_#{field.id}" %>" class="form-control<%= ' cfpersisted' if field.persist %>" type="<%= field.input %>" name="<%= field.name %>" placeholder="<%= field.placeholder_value %>" <%== 'required' if field.required %>>
</div>
5 changes: 5 additions & 0 deletions db/migrate/20200727214421_add_persist_to_form_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPersistToFormFields < ActiveRecord::Migration[6.0]
def change
add_column :form_fields, :persist, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_08_170559) do
ActiveRecord::Schema.define(version: 2020_07_27_214421) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand Down Expand Up @@ -57,6 +57,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "placeholder"
t.boolean "persist", default: false
t.index ["field_id"], name: "index_form_fields_on_field_id"
t.index ["form_id"], name: "index_form_fields_on_form_id"
end
Expand Down