Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

2 customisation options for has_many form #1945

Closed
wants to merge 6 commits into from

3 participants

Andrey Eremin Sean Linsley Matthew Millsaps-Brewer
Andrey Eremin

Added :title => false and :new_record => false to prevent adding h3 block and 'New object' button for has_many form

Sean Linsley
Owner

I'm confused, what are you trying to achieve with this? Could you provide before & after screenshots?

Andrey Eremin

Showing h3 title
before_1
Showing Add button
before2
Not showing h3 title
after1
not showing Add button
after2

so i have a form with:
f.has_many :translations, :title => false, :new_record => false do |translation|
so with my options i can turn off showing h3 Title because i don't need it and not showing add button which i don't need either.

I think, one of this options can be useful for someone else too.

lib/active_admin/form_builder.rb
((6 lines not shown))
inputs options, &form_block
- js = js_for_has_many(association, form_block, template)
+ js = options[:new_record] == false ? "" : js_for_has_many(association, form_block, template)
form_buffers.last << js.html_safe
end
Sean Linsley Owner

Perhaps this is a better way to write the changes:

# at the top of the method definition:
options = { :for => association, :title => true, :new_record => true }.merge(options)

# then the actual code
if options[:title]
  title = object.class.reflect_on_association(association).class_name.human(:count => 1.1)
  form_buffers.last << template.content_tag(:h3, title)
end

inputs options, &form_block

form_buffers.last << js_for_has_many(association, form_block, template).html_safe if options[:new_record]

Note that I changed klass.model_name to the more succinct class_name

... @gregbell, is it possible to use Arbre here? I hate to force the title part to live on four lines instead of one.

With your changes i got tons of errors in specs:

with class_name and i 've got errors for spec if we do not add anything to form_buffers.last

Sean Linsley Owner

Really? class_name works fine for me.

The form_buffers.last problem is likely that with my changes, the function will return nil instead of the form buffer if new_record is false.

1) with class_name i've got ' undefined method `human' for "Post":String' in specs.

2) with form_buffers changes you advise - because of form_buffers.pop (why?) it is not rendering first element inside has_many block:
spec failed: Failure/Error: body.should have_tag("label", "Title 1")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sean Linsley
Owner

By the way, you need to rebase on master.

git pull --rebase upstream master

Specifically, the onclick string in js_for_has_many has been updated (un-broken, really)

Sean Linsley seanlinsley referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Andrey Eremin

@Daxter i've updated my forked repo.

Matthew Millsaps-Brewer

#2068 was just merged in that added the title functionality. Could you rebase this to just include your new option to hide the "add" button?

Andrey Eremin

Sure, but... please, tell me how :)

Matthew Millsaps-Brewer

First, start by rebasing your feature branch off of the master branch from upstream (gregbell/active_admin).

git pull --rebase upstream master

If you don't have upstream as a remote, you can add it with:

git remote add upstream git://github.com/gregbell/active_admin.git

Then resolve conflicts as they arise when your commits are re-applied as new ones on master, like you had just done the work today. You'll need to remove the code that is no longer needed, since master now has the functionality for specifying a title.

When you're done, force push your feature branch and we'll see the changes in this PR.

git push origin +master

In general, you should create a branch in your fork & submit that branch as a PR to master over here. That way you can pull new changes from our master back into your forked repo's master at any time in the future.

Sean Linsley
Owner

Closing this due to inactivity. @developer88 if you still want to add in the option to disable the "add" button, please create a new pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 27, 2013
  1. Andrey Eremin
  2. Andrey Eremin
Commits on Mar 6, 2013
  1. Andrey Eremin

    Small refactoring.

    developer88 authored
  2. Andrey Eremin
Commits on Mar 11, 2013
  1. [Added] slight changes.

    Andrey Eremin authored
Commits on Mar 22, 2013
  1. Andrey Eremin
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 5 deletions.
  1. +8 −3 lib/active_admin/form_builder.rb
  2. +11 −2 spec/unit/form_builder_spec.rb
11 lib/active_admin/form_builder.rb
View
@@ -42,7 +42,7 @@ def commit_action_with_cancel_link
end
def has_many(association, options = {}, &block)
- options = { :for => association }.merge(options)
+ options = { :for => association, :title => true, :new_record => true }.merge(options)
options[:class] ||= ""
options[:class] << "inputs has_many_fields"
@@ -67,10 +67,15 @@ def has_many(association, options = {}, &block)
content = with_new_form_buffer do
template.content_tag :div, :class => "has_many #{association}" do
- form_buffers.last << template.content_tag(:h3, object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1))
+ if options[:title]
+ title = object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1)
+ form_buffers.last << template.content_tag(:h3, title)
+ end
+
inputs options, &form_block
- form_buffers.last << js_for_has_many(association, form_block, template)
+ js = options[:new_record] == false ? "" : js_for_has_many(association, form_block, template)
+ form_buffers.last << js.html_safe
end
end
form_buffers.last << content
13 spec/unit/form_builder_spec.rb
View
@@ -167,7 +167,7 @@ def build_form(options = {}, form_object = Post.new, &block)
end
- context "with actons" do
+ context "with actions" do
it "should generate the form once" do
body = build_form do |f|
f.inputs do
@@ -351,7 +351,7 @@ def build_form(options = {}, form_object = Post.new, &block)
let :body do
build_form({:url => '/categories'}, Category.new) do |f|
f.object.posts.build
- f.has_many :posts do |p,i|
+ f.has_many :posts, :title => false, :new_record => false do |p,i|
p.input :title, :label => "Title #{i}"
end
end
@@ -360,6 +360,15 @@ def build_form(options = {}, form_object = Post.new, &block)
it "should accept a block with a second argument" do
body.should have_tag("label", "Title 1")
end
+
+ it "should not add a header" do
+ body.should_not have_tag('h3', 'Post')
+ end
+
+ it "should not add link to new nested records" do
+ body.should_not have_tag('a', 'Add New Post')
+ end
+
end
pending "should render the block if it returns nil" do
Something went wrong with that request. Please try again.