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
Batch Action forms #1815
Batch Action forms #1815
Conversation
When I worked with @pcreux on designing the DSL earlier this year, we wanted to leave it open enough to do anything you want with it. So, if you really want to add some DSL as you propose above, I would definitely write it as a separate gem that a user can install as needed (there are already several on rubygems doing this if you haven't looked). I think this will be a complicated addition to take on top of batch actions, but I'm available if you have any questions. |
There are add-on gems for Active Admin? 1) where are they and 2) why isn't this mentioned in the docs? |
I'm thinking a better syntax would be: batch_action :whitelist, :form => {:reason => :text} do |ids, reason|
# do your stuff!
end That way the |
I like this recommendation better. On Dec 14, 2012, at 10:51 AM, Sean Ian Linsley notifications@github.com wrote:
|
Not in the docs because they are from 3rd parties. I have a few up there, just search for "active_admin" and "active admin" on ruby gems. On Dec 14, 2012, at 10:40 AM, Sean Ian Linsley notifications@github.com wrote:
|
I've done something similar by customizing the controller. It looks something like this controller do
def batch_action
@user = User.new
if params[:batch_action] == "i_want_a_form"
render "admin/users/batch_action", :layout => "active_admin"
else
super
end
end
end Here's the gist of backtrace https://gist.github.com/4417458 When I looked into the source content_for(:layout) line yields form as it should on development but on staging its nil Thanks |
@macfanatic, here's a work in progress: https://gist.github.com/4485846 Unfortunately Rails UJS doesn't support non-blocking confirm dialogs (see stack overflow and rails/jquery-ujs#283) so I wrote my own dialog handler. The idea is to return any extra inputs in a Links for future reference: [jquery-ujs] [AA] [gist 1] [blog post] [gist 2] |
While there's no reason why this dialog form couldn't support datepickers, that won't be possible (without a hack) until something along the lines of #1879 is pulled in. |
cool stuff dude |
@macfanatic any idea how compatible this is with the Bootstrap stuff you're working on? |
There will be a merge conflict once bootstrapped is in master, but only b/c the JS & CSS assets just need to be relocated in the new structure, so not a lot of work at all. Overall I think it should be easy to integration. I'm still not sold on this being actually added to AA however. |
Cool. Why not? All this is, is a DSL put on top of jQuery UI modal dialogs to allow you to define custom forms wherever you want, along with some integration into the Batch Actions controller. IIRC, we already depend on jQuery UI, so all this PR does is properly utilize the existing toolset. This really is multipurpose. For example, in the AA app which I developed this for, I used these dynamic forms for Batch Actions, random "confirm with extra input" resource actions, as well as the calendar (click on a day, you can edit/create an event). Now given it could be prettier, but I'm welcome to suggestions. |
I think I'd be much more open to the integration if instead of accepting a hash of args, if we could render a full On Apr 01, 2013, at 12:13 PM, Sean Ian Linsley notifications@github.com wrote: Cool. |
But where would the partial exist before you click on a modal dialog/form? The great thing about this solution is it dynamically generates the form, and throws it away when it's no longer needed. |
Something like this is more of what I imagined: batch_action :publish, form: true do |*args|
# something exciting
end Or maybe you don't even have to pass
form_tag url: batch_action_form_url(resource_class) do |f|
= f.text_field :attr Just a thought, I don't think declaring a form via a hash is a maintainable DSL :) |
What do you mean, "like how the sidebar method works"? What does it do that's special? The problem with using view partials, is they can't be used for dynamic data on the page. Going back to the calendar use-case, this is all I need to have a dynamic form for both creating new events, and editing existing ones. $ ->
# Form submission handler
$('form#calendar_events').on 'confirm:complete', (e, inputs, parent)->
$('#event_id').val $(parent).data 'id'
$('#event_day').val inputs.day
$('#event_schedule').val $(parent).data('schedule') || inputs.schedule
$(@).submit()
# Moving an existing event to a different day
$('table.calendar li').click ->
AA.modalDialog "When would you like to move this to?<br>From #{$(@).data 'day'} to..."
day: 'datepicker'
(inputs)=>
$('form#calendar_events').trigger 'confirm:complete', [inputs, @]
# Creating a new event
$('#create_calendar_event').click ->
AA.modalDialog 'Enter the date and schedule for this new event.'
day: 'datepicker', schedule: ['Foo', 'Bar', 'Baz']
(inputs)=>
$('form#calendar_events').trigger 'confirm:complete', inputs That's two different "form partials", which can be bound to any element in the DOM. Then the static HTML side of things: ActiveAdmin.register_page 'Calendar' do
controller do
def index
# initialize the Calendar object
end
end
# Create event or use existing; update appropriate relations in the model logic.
page_action :events, method: :post do
# create or update the calendar event, then return a useful message
redirect_to calendar_path, notice
end
action_item do
a "Create New Event", id: 'create_calendar_event'
end
content do
form_for :event, url: calendar_events_path, html: {style: 'display: none', id: 'calendar_events'} do |f|
f.text_field :id
f.text_field :day
f.text_field :schedule
end
# and render the calendar here...
end
end |
For the power and flexibility that gives me, that's really not a lot of code. |
To implement the same thing with proper view partials, I'd have to make rendering happen in an AJAX request. |
And now with not a little confusion, this PR is rebased on the current upstream master 🔫 |
@chrise86 I explain exactly how to use it at the beginning of this PR :) |
Just realised the error of my ways, using the wrong branch... :-/ (hangs head in shame...) |
Ah, no worries 🐈 As you can see I didn't do a great job with the look & feel of the popup, but at least it works. Changes are welcome. |
FYI when using this branch / fork of activeadmin the menu items are moving around randomly with each page load, and parent / child items do not always appear... Switching to v0.6.0 on gregs main repo fixes this |
Thanks for the contrib @chrise86 💚 |
@@ -99,17 +102,31 @@ class BatchAction | |||
# => You can create batch actions with a title instead of a Symbol | |||
# | |||
# BatchAction.new( :flag, :if => proc { can? :flag, AdminUser } ) { |selection| } | |||
# => You can provide an optional :if proc to optionally display the batch action | |||
# => You can provide an optional `:if` proc to optionally display the batch action |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewrite this to:
You can provide an
:if
proc to choose when the batch action should be displayed
Thanks for the vote of confidence @anthonylebrun. I'll work on updating this PR to the latest code on master. |
@daxter Awesome! I'll be watching this thread with bated breath. |
Okay, this has been updated to master. Now to write tests and documentation. |
@daxter , you are the man. |
@include section-header; | ||
span { font-size: 1.1em; } | ||
} | ||
.ui-dialog-titlebar-close { visibility: hidden } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, needs to be display: none
thanks to @chrise86 for the updated CSS so this isn't horribly ugly
|
As for alignment... Maybe it will be more appropriate to place label above input field? This way problem with alignment will disappear ^_^ |
Yeah that'd be an option. I think when I first implemented this I set a static width on the labels, but later on I took it out because I wanted a wider label than the width I had originally set. |
That's great stuff @seanlinsley ! 👍 |
Glad you like it 🐱 |
Woohoo, thanks @seanlinsley . Can't wait to try it on my next project! |
Good job! However I didn't find ability to access request data from form block.
? |
@Fivell your example should work, if not inspect |
@timoschilling , this doesn't work because I think proc is evaluated in wrong scope using master branch one more thing is that this resource is using inside proc self is |
That's because the proc isn't evaluated in the view context, it's simply called as-is. If you'd like that feature, please submit a PR. |
I'm already working on that |
This optional :form attribute would be very useful in page_action or even action_item too |
FYI: this is an Issue that was converted into a Pull Request
I want to add optional forms to Batch Actions; an example use-case being that you want the user to add in a text box the reason why they're taking this action. Since we're already including jQuery UI and it has a similar UI for pop-over forms, a bit of the work has already been done.
What I'm curious about is what you think a good DSL syntax would be for this--should I include aform do |f|
block inside abatch_action
declaration, or should they be defined much like the new/edit pages are, with something like the below syntax?UPDATES
:confirm
option. Passing a string still works, but now if you passtrue
, it will use a default confirm message.