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

Add ability to override "Remove" button text on has_many forms #6523

Merged
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
5 changes: 5 additions & 0 deletions docs/5-forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ ActiveAdmin.register Post do
f.inputs do
f.has_many :comment,
new_record: 'Leave Comment',
remove_record: 'Remove Comment',
allow_destroy: -> (c) { c.author?(current_admin_user) } do |b|
b.input :body
end
Expand Down Expand Up @@ -139,6 +140,10 @@ The `:new_record` option controls the visibility of the new record button (shown
by default). If you pass a string, it will be used as the text for the new
record button.

The `:remove_record` option controls the text of the remove button (shown after
the new record button is pressed). If you pass a string, it will be used as the
text for the remove button.

The `:sortable` option adds a hidden field and will enable drag & drop sorting
of the children. It expects the name of the column that will store the index of
each child.
Expand Down
54 changes: 54 additions & 0 deletions features/has_many.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@javascript
Feature: Has Many

A resource has many other resources

Background:
Given I am logged in
And a post with the title "Hello World" written by "John Doe" exists

Scenario: Updating the parent resource page with default text for Remove button
Given a configuration of:
"""
ActiveAdmin.register User do
form do |f|
f.inputs do
f.has_many :posts do |ff|
ff.input :title
ff.input :body
end
end
f.actions
end
end
ActiveAdmin.register Post
"""
When I go to the last author's show page
And I follow "Edit User"
Then I should see a link to "Add New Post"

When I click "Add New Post"
Then I should see a link to "Remove"

Scenario: Updating the parent resource page with custom text for Remove button
Given a configuration of:
"""
ActiveAdmin.register User do
form do |f|
f.inputs do
f.has_many :posts, remove_record: "Hide" do |ff|
ff.input :title
ff.input :body
end
end
f.actions
end
end
ActiveAdmin.register Post
"""
When I go to the last author's show page
And I follow "Edit User"
Then I should see a link to "Add New Post"

When I click "Add New Post"
Then I should see a link to "Hide"
3 changes: 3 additions & 0 deletions features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def path_to(page_name)
when /^the last post's edit page$/
edit_admin_post_path(Post.last)

when /^the last author's show page$/
admin_user_path(User.last)

# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
Expand Down
6 changes: 4 additions & 2 deletions lib/active_admin/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HasManyBuilder < SimpleDelegator
attr_reader :assoc
attr_reader :options
attr_reader :heading, :sortable_column, :sortable_start
attr_reader :new_record, :destroy_option
attr_reader :new_record, :destroy_option, :remove_record

def initialize(has_many_form, assoc, options)
super has_many_form
Expand Down Expand Up @@ -72,6 +72,7 @@ def extract_custom_settings!(options)
@sortable_start = options.delete(:sortable_start) || 0
@new_record = options.key?(:new_record) ? options.delete(:new_record) : true
@destroy_option = options.delete(:allow_destroy)
@remove_record = options.delete(:remove_record)
options
end

Expand Down Expand Up @@ -107,7 +108,8 @@ def render_has_many_form(form_builder, parent, &block)
def has_many_actions(form_builder, contents)
if form_builder.object.new_record?
contents << template.content_tag(:li) do
template.link_to I18n.t("active_admin.has_many_remove"), "#", class: "button has_many_remove"
remove_text = remove_record.is_a?(String) ? remove_record : I18n.t("active_admin.has_many_remove")
template.link_to remove_text, "#", class: "button has_many_remove"
end
elsif allow_destroy?(form_builder.object)
form_builder.input(
Expand Down