Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

2 customisation options for has_many form #1945

Closed
wants to merge 6 commits into
from

Conversation

Projects
None yet
3 participants
Contributor

developer88 commented Feb 27, 2013

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

Owner

seanlinsley commented Feb 27, 2013

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

Contributor

developer88 commented Feb 28, 2013

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.

@seanlinsley seanlinsley and 1 other commented on an outdated diff Mar 6, 2013

lib/active_admin/form_builder.rb
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
@seanlinsley

seanlinsley Mar 6, 2013

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.

@developer88

developer88 Mar 6, 2013

Contributor

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

@seanlinsley

seanlinsley Mar 6, 2013

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.

@developer88

developer88 Mar 11, 2013

Contributor
  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")

Owner

seanlinsley commented Mar 15, 2013

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)

Contributor

developer88 commented Mar 22, 2013

@daxter i've updated my forked repo.

Contributor

macfanatic commented Apr 9, 2013

#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?

Contributor

developer88 commented Apr 9, 2013

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

Contributor

macfanatic commented Apr 9, 2013

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.

Owner

seanlinsley commented Apr 20, 2013

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