Skip to content

Commit

Permalink
Ability to super scaffold fields onto Membership (#990)
Browse files Browse the repository at this point in the history
* Allow super scaffolded fields to work on Memerships

* prevent duplicate fields if someone has ejected this form

* Use account/memberships/attributes to super scaffold attributes on to Membership#show

* Allow account/memberships/fields to work

* Allow the app to specify membership params for invitations

* refactor

* linter
  • Loading branch information
jagthedrummer authored Dec 18, 2024
1 parent eb8ef54 commit 4b055d2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
10 changes: 6 additions & 4 deletions bullet_train-super_scaffolding/lib/scaffolding/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,10 @@ def valid_#{attribute.collection_name}
end

show_page_doesnt_exist = child == "User"
scaffold_add_line_to_file("./app/views/account/scaffolding/completely_concrete/tangible_things/show.html.erb", field_content.strip, ERB_NEW_FIELDS_HOOK, prepend: true, suppress_could_not_find: show_page_doesnt_exist)

show_page_target = (child == "Membership") ?
"./app/views/account/scaffolding/completely_concrete/tangible_things/_attributes.html.erb" :
"./app/views/account/scaffolding/completely_concrete/tangible_things/show.html.erb"
scaffold_add_line_to_file(show_page_target, field_content.strip, ERB_NEW_FIELDS_HOOK, prepend: true, suppress_could_not_find: show_page_doesnt_exist)
end

#
Expand All @@ -848,7 +850,7 @@ def valid_#{attribute.collection_name}
# table header.
field_content = "<th#{cell_attributes.present? ? " " + cell_attributes : ""}><%= t('.fields.#{attribute.is_vanilla? ? attribute.name : attribute.name_without_id_suffix}.heading') %></th>"

unless ["Team", "User"].include?(child)
unless ["Team", "User", "Membership"].include?(child)
scaffold_add_line_to_file("./app/views/account/scaffolding/completely_concrete/tangible_things/_index.html.erb", field_content, "<%# 🚅 super scaffolding will insert new field headers above this line. %>", prepend: true)
end

Expand Down Expand Up @@ -884,7 +886,7 @@ def valid_#{attribute.collection_name}
field_content.gsub!(/\s%>/, ", one_line: true %>")
end

unless ["Team", "User"].include?(child)
unless ["Team", "User", "Membership"].include?(child)
scaffold_add_line_to_file("./app/views/account/scaffolding/completely_concrete/tangible_things/_tangible_thing.html.erb", field_content.strip, ERB_NEW_FIELDS_HOOK, prepend: true)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def permitted_fields
raise "It looks like you've removed `permitted_fields` from your controller. This will break Super Scaffolding."
end

def permitted_membership_fields
raise "It looks like you've removed `permitted_membership_fields` from your controller. This will break Super Scaffolding."
end

def permitted_arrays
raise "It looks like you've removed `permitted_arrays` from your controller. This will break Super Scaffolding."
end
Expand All @@ -145,6 +149,7 @@ def invitation_params
# 🚅 super scaffolding will insert new fields above this line.
# 🚅 super scaffolding will insert new arrays above this line.
membership_attributes: [
*permitted_membership_fields,
:user_first_name,
:user_last_name,
role_ids: []
Expand Down
4 changes: 4 additions & 0 deletions bullet_train/app/views/account/invitations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
</div>
<% end %>
<% end %>

<% with_field_settings form: fields do %>
<%= render "account/memberships/fields", membership: fields.object, form: fields %>
<% end %>
</div>
<% end %>
<% end %>
Expand Down
60 changes: 31 additions & 29 deletions bullet_train/app/views/account/memberships/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
<%= form_with(model: [:account, (@team unless membership.persisted?), membership], class: 'form', local: true) do |form| %>
<%= render 'account/shared/forms/errors', form: form %>

<% if membership.unclaimed? || membership.tombstone? %>
<div class="grid grid-cols-1 gap-y gap-x sm:grid-cols-6">
<div class="sm:col-span-3">
<%= render 'shared/fields/text_field', form: form, method: :user_first_name %>
</div>

<div class="sm:col-span-3">
<%= render 'shared/fields/text_field', form: form, method: :user_last_name %>
<% with_field_settings form: form do %>
<% if membership.unclaimed? || membership.tombstone? %>
<div class="grid grid-cols-1 gap-y gap-x sm:grid-cols-6">
<div class="sm:col-span-3">
<%= render 'shared/fields/text_field', form: form, method: :user_first_name %>
</div>

<div class="sm:col-span-3">
<%= render 'shared/fields/text_field', form: form, method: :user_last_name %>
</div>
</div>
</div>
<% end %>
<% end %>

<% if membership.tombstone? %>
<% if cloudinary_enabled? %>
<%= render 'shared/fields/cloudinary_image', form: form, method: :user_profile_photo_id %>
<% if membership.tombstone? %>
<% if cloudinary_enabled? %>
<%= render 'shared/fields/cloudinary_image', form: form, method: :user_profile_photo_id %>
<% end %>
<% end %>
<% end %>

<%= hidden_field_tag 'membership[role_ids][]', nil %>

<% Membership.assignable_roles.each do |role| %>
<% if role.manageable_by?(current_membership.roles) %>
<div class="flex items-top">
<%= form.check_box :role_ids, {multiple: true, class: "h-4 w-4 text-primary-500 focus:ring-primary-500 border-slate-300 rounded mt-0.5"}, role.id, nil %>
<label for="membership_role_ids_<%= role.id %>" class="ml-2 block select-none">
<%= t('.grant_privileges_of', role_key: t(".fields.role_ids.options.#{role.key}.label")) %>
<div class="mt-0.5 text-slate-400 font-light leading-normal">
<%= t(".fields.role_ids.options.#{role.key}.description") %>
</div>
</label>
</div>
<%= hidden_field_tag 'membership[role_ids][]', nil %>

<% Membership.assignable_roles.each do |role| %>
<% if role.manageable_by?(current_membership.roles) %>
<div class="flex items-top">
<%= form.check_box :role_ids, {multiple: true, class: "h-4 w-4 text-primary-500 focus:ring-primary-500 border-slate-300 rounded mt-0.5"}, role.id, nil %>
<label for="membership_role_ids_<%= role.id %>" class="ml-2 block select-none">
<%= t('.grant_privileges_of', role_key: t(".fields.role_ids.options.#{role.key}.label")) %>
<div class="mt-0.5 text-slate-400 font-light leading-normal">
<%= t(".fields.role_ids.options.#{role.key}.description") %>
</div>
</label>
</div>
<% end %>
<% end %>
<% end %>

<%= render "account/memberships/fields", membership: membership, form: form %>
<%# 🚅 super scaffolding will insert new fields above this line. %>
<%= render "account/memberships/fields", membership: membership, form: form %>
<% end %>

<div class="buttons">
<%= form.submit t('.buttons.update'), class: "button" %>
Expand Down
1 change: 1 addition & 0 deletions bullet_train/app/views/account/memberships/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<% end %>
<% end %>
<% end %>
<%= render "account/memberships/attributes", membership: @membership %>
<% end %>
<%# 🚅 super scaffolding will insert new fields above this line. %>
<% end %>
Expand Down
1 change: 0 additions & 1 deletion bullet_train/app/views/account/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<% end %>

<%= render "account/users/fields", user: user, form: form %>
<%# 🚅 super scaffolding will insert new fields above this line. %>
</div>
<% end %>

Expand Down

0 comments on commit 4b055d2

Please sign in to comment.