-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move fields of role form to partial and start writing logic to create…
… new roles
- Loading branch information
1 parent
292b2b9
commit a48e38a
Showing
5 changed files
with
68 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module RoleFormHelper | ||
def link_to_add_role(name, form, association) | ||
new_role = form.object.send(association).klass.new | ||
id = new_role.object_id | ||
fields = form.fields_for(association, new_role, child_index: id) do |builder| | ||
render("#{association.to_s.singularize}_fields", form: builder) | ||
end | ||
link_to( | ||
name, | ||
'#', | ||
class: 'add_fields', | ||
data: { | ||
id: id, | ||
fields: fields.gsub("\n", '') | ||
} | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class addFields { | ||
// This executes when the function is instantiated. | ||
constructor() { | ||
this.links = document.querySelectorAll(".add_fields"); | ||
this.iterateLinks(); | ||
} | ||
|
||
iterateLinks() { | ||
// If there are no links on the page, stop the function from executing. | ||
if (this.links.length === 0) return; | ||
// Loop over each link on the page. A page could have multiple nested forms. | ||
this.links.forEach((link) => { | ||
link.addEventListener("click", (e) => { | ||
this.handleClick(link, e); | ||
}); | ||
}); | ||
} | ||
|
||
handleClick(link, e) { | ||
// Stop the function from executing if a link or event were not passed into the function. | ||
if (!link || !e) return; | ||
// Prevent the browser from following the URL. | ||
e.preventDefault(); | ||
// Save a unique timestamp to ensure the key of the associated array is unique. | ||
let time = new Date().getTime(); | ||
// Save the data id attribute into a variable. This corresponds to `new_object.object_id`. | ||
let linkId = link.dataset.id; | ||
// Create a new regular expression needed to find any instance of the `new_object.object_id` used in the fields data attribute if there's a value in `linkId`. | ||
let regexp = linkId ? new RegExp(linkId, "g") : null; | ||
// Replace all instances of the `new_object.object_id` with `time`, and save markup into a variable if there's a value in `regexp`. | ||
let newFields = regexp ? link.dataset.fields.replace(regexp, time) : null; | ||
// Add the new markup to the form if there are fields to add. | ||
newFields ? link.insertAdjacentHTML("beforebegin", newFields) : null; | ||
} | ||
} | ||
window.addEventListener("turbolinks:load", () => new addFields()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%div.border.border-dark-subtle.rounded.p-1.fw-light | ||
= person_role.hidden_field :_destroy | ||
Rolle | ||
= person_role.collection_select :role_id, Role.order(:name), :id, :name, {}, class: "form-select w-100" | ||
%div | ||
Stufe | ||
%div.d-flex.fw-light | ||
= person_role.collection_select :person_role_level_id, PersonRoleLevel.order(:level), :id, :level, {}, class: "form-select w-50 me-1" | ||
= person_role.number_field :percent, step: 1, class: "form-control w-50" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters