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

Dialog to set start and end records for export #2626

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,31 @@
window.ActiveAdmin.modalDialog = (message, inputs, callback)->
html = """<form id="dialog_confirm" title="#{message}"><ul>"""
for name, type of inputs
if /^(datepicker|checkbox|text)$/.test type
wrapper = 'input'
else if type is 'textarea'
wrapper = 'textarea'
else if $.isArray type
[wrapper, elem, opts, type] = ['select', 'option', type, '']
else
console.warn "Unsupported input type: {#{name}: #{type}}"

klass = if type is 'datepicker' then type else ''
html += """<li>
<label>#{name.charAt(0).toUpperCase() + name.slice(1)}</label>
<#{wrapper} name="#{name}" class="#{klass}" type="#{type}">""" +
(if opts then ("<#{elem}>#{v}</#{elem}>" for v in opts).join '' else '') +
"</#{wrapper}>" +
"</li>"
[wrapper, elem, opts, type, klass] = [] # unset any temporary variables

html += "</ul></form>"
$(html).appendTo('body').dialog
modal: true
buttons:
OK: ->
callback $(@).serializeObject()
$(@).dialog('close')
Cancel: ->
$(@).dialog('close').remove()
$('input').blur() # prevent auto-focus event, so datepicker can be initialized
@@ -0,0 +1,8 @@
# `serializeArray` generates => [{ name: 'foo', value: 'bar' }]
# This function remaps it to => { foo: 'bar' }
jQuery ($)->
$.fn.serializeObject = ->
obj = {}
for o in @serializeArray()
obj[o.name] = o.value
obj
26 changes: 19 additions & 7 deletions app/assets/javascripts/active_admin/pages/batch_actions.js.coffee
@@ -1,12 +1,24 @@
jQuery ($) ->
jQuery ($)->

#
# Use Rails.js click handler to allow for Rails' confirm dialogs
# Use ActiveAdmin.modalDialog to prompt user if confirmation is required for current Batch Action
#
$('#batch_actions_selector li a').click (event)->
event.stopPropagation() # prevent Rails UJS click event
if message = $(@).data 'confirm'
ActiveAdmin.modalDialog message, $(@).data('inputs'), (inputs)=>
$(@).trigger 'confirm:complete', inputs
else
$(@).trigger 'confirm:complete'

$('#batch_actions_selector li a').on 'confirm:complete', (event, inputs)->
if val = JSON.stringify inputs
$('#batch_action_inputs').val val
else
$('#batch_action_inputs').attr 'disabled', 'disabled'

$(document).delegate "#batch_actions_selector li a", "click.rails", ->
$("#batch_action").val $(@).attr("data-action")
$("#collection_selection").submit()
$('#batch_action').val $(@).data('action')
$('#collection_selection').submit()

#
# Add checkbox selection to resource tables and lists if batch actions are enabled
Expand All @@ -19,8 +31,8 @@ jQuery ($) ->
else
$(".paginated_collection").checkboxToggler()

$(".paginated_collection").find(":checkbox").bind "change", ->
if $(".paginated_collection").find(":checkbox").filter(":checked").length > 0
$(".paginated_collection :checkbox").change ->
if $(".paginated_collection :checkbox:checked").length
$("#batch_actions_selector").aaDropdownMenu("enable")
else
$("#batch_actions_selector").aaDropdownMenu("disable")
24 changes: 24 additions & 0 deletions app/assets/javascripts/active_admin/pages/export_form.js.coffee
@@ -0,0 +1,24 @@
jQuery ($) ->

$('[data-export-modal]').click (e) ->
e.preventDefault()

$btn = $(this)

inputs =
"export[start]": "text"
"export[end]": "text"

ActiveAdmin.modalDialog "Export Data", inputs, (inputs)=>
target = $btn.attr("href")

if target.indexOf("?") == -1
append = "?"
else
append = "&"

append += $.param(inputs)

window.location = target + append

return false
1 change: 1 addition & 0 deletions app/assets/stylesheets/active_admin/_base.css.scss
Expand Up @@ -12,6 +12,7 @@
@import "active_admin/components/popovers";
@import "active_admin/components/tables";
@import "active_admin/components/batch_actions";
@import "active_admin/components/modal_dialog";
@import "active_admin/components/blank_slates";
@import "active_admin/components/breadcrumbs";
@import "active_admin/components/dropdown_menu";
Expand Down
34 changes: 34 additions & 0 deletions app/assets/stylesheets/active_admin/components/_modal_dialog.scss
@@ -0,0 +1,34 @@
.ui-widget-overlay {
position: fixed;
background: rgba(0,0,0,.2);
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1001;
}

.ui-dialog {
position: fixed;
z-index: 1002;
@include section-background;
@include box-shadow(rgba(0,0,0,0.5) 0 0 10px);

.ui-dialog-titlebar {
@include section-header;
span { font-size: 1.1em; }
}
.ui-dialog-titlebar-close { visibility: hidden }

ul { list-style-type: none; }
li { margin: 10px 0; }
label { margin-right: 10px; }

.ui-dialog-buttonpane, form {
padding: 7px 15px 13px;
}
.ui-dialog-buttonpane button {
&:first-child { @include dark-button; } // OK
&:last-child { @include light-button; } // Cancel
}
}
1 change: 1 addition & 0 deletions config/locales/en.yml
Expand Up @@ -53,6 +53,7 @@ en:
link: "Create one"
batch_actions:
button_label: "Batch Actions"
default_confirmation: "Are you sure you want to do this? It might be irreversible."
delete_confirmation: "Are you sure you want to delete these %{plural_model}? You won't be able to undo this."
succesfully_destroyed:
one: "Successfully destroyed 1 %{model}"
Expand Down
14 changes: 14 additions & 0 deletions docs/3-index-pages.md
Expand Up @@ -204,3 +204,17 @@ ActiveAdmin.setup do |config|

end
```

## Customizing Export Behaviour

You can choose what you want to do when the user uses a download link:

```ruby
# Per resource:
ActiveAdmin.register Post do

config.export_bahaviour = :modal
config.export_bahaviour = :paginate
config.export_bahaviour = :all

end
64 changes: 64 additions & 0 deletions features/index/export_behaviour.feature
@@ -0,0 +1,64 @@
Feature: Export Behaviour

Allow developers to change the behaviour when exporting
records from ActiveAdmin

Background:
And a post with the title "Hello World" exists
And a post with the title "Hello World 2" exists
And I am logged in

Scenario: Default should show a modal
Given a configuration of:
"""
ActiveAdmin.register Post do

end
"""
When I am on the index page for posts
Then the "CSV" link should have the "data-export-modal" attribute

Scenario: Show a modal dialog
Given a configuration of:
"""
ActiveAdmin.register Post do
config.export_behaviour = :modal
end
"""
When I am on the index page for posts
Then the "CSV" link should have the "data-export-modal" attribute

Scenario: Export all
Given a configuration of:
"""
ActiveAdmin.register Post do
config.per_page = 1

config.export_behaviour = :all
end
"""
When I am on the index page for posts
Then the "CSV" link should not have the "data-export-modal" attribute

When I click "CSV"
And I should download a CSV file for "posts" containing:
| Id | Title | Body | Published At | Starred | Created At | Updated At |
| \d+ | Hello World 2 | | | | (.*) | (.*) |
| \d+ | Hello World | | | | (.*) | (.*) |

Scenario: Export current page
Given a configuration of:
"""
ActiveAdmin.register Post do
config.per_page = 1

config.export_behaviour = :paginate
end
"""
When I am on the index page for posts
Then the "CSV" link should not have the "data-export-modal" attribute

When I click "CSV"
And I should download a CSV file for "posts" containing:
| Id | Title | Body | Published At | Starred | Created At | Updated At |
| \d+ | Hello World 2 | | | | (.*) | (.*) |
20 changes: 20 additions & 0 deletions features/index/export_pagination.feature
@@ -0,0 +1,20 @@
Feature: Index Export Pagination

Background:
Given an index configuration of:
"""
ActiveAdmin.register Post
"""
And 20 posts exist

Scenario: Export with only start parameter set
When I am on the index page for posts with params "?export%5Bstart%5D=5"
Then the table "#index_table_posts" should have 16 rows (including the header row)

Scenario: Export with only end parameter set
When I am on the index page for posts with params "?export%5Bend%5D=5"
Then the table "#index_table_posts" should have 6 rows (including the header row)

Scenario: Export with start and end parameter set
When I am on the index page for posts with params "?export%5Bstart%5D=5&export%5Bend%5D=15"
Then the table "#index_table_posts" should have 11 rows (including the header row)
11 changes: 11 additions & 0 deletions features/step_definitions/web_steps.rb
Expand Up @@ -61,6 +61,10 @@ def with_scope(locator)
page.send should, have
end

Then /^I should see a dialog$/ do
page.should have_css(".ui-dialog")
end

Then /^the "([^"]*)" field(?: within (.*))? should( not)? contain "([^"]*)"$/ do |field, parent, negate, value|
with_scope(parent) do
field = find_field(field)
Expand All @@ -81,6 +85,13 @@ def with_scope(locator)
current_path.should == path_to(page_name)
end

Then /^the "(.*?)" link should( not)? have the "(.*?)" attribute$/ do |link_name, negate, attribute|
should = negate ? :should_not : :should

link = find_link(link_name)
link[attribute].send should, nil
end

Then /^show me the page$/ do
save_and_open_page
end
7 changes: 5 additions & 2 deletions lib/active_admin/application.rb
Expand Up @@ -36,7 +36,7 @@ def initialize

# Set the site title image displayed in the main layout (has precendence over :site_title)
inheritable_setting :site_title_image, ""

# Set a favicon
inheritable_setting :favicon, false

Expand Down Expand Up @@ -73,6 +73,9 @@ def initialize
# Default Download Links options
inheritable_setting :download_links, true

# Default Export Behaviour options
inheritable_setting :export_behaviour, :modal

# The authorization adapter to use
inheritable_setting :authorization_adapter, ActiveAdmin::AuthorizationAdapter

Expand Down Expand Up @@ -218,7 +221,7 @@ def remove_active_admin_load_paths_from_rails_autoload_and_eager_load
ActiveSupport::Dependencies.autoload_paths.reject!{ |path| load_paths.include? path }
Rails.application.config.eager_load_paths = # the array is frozen :/
Rails.application.config.eager_load_paths.reject do |path|
load_paths.include?(path)
load_paths.include?(path)
end
end

Expand Down
19 changes: 12 additions & 7 deletions lib/active_admin/batch_actions/controller.rb
Expand Up @@ -2,21 +2,26 @@ module ActiveAdmin
module BatchActions
module Controller

# Controller Action that get's called when submitting the batch action form
# Controller action that is called when submitting the batch action form
def batch_action
if selected_batch_action
selected_ids = params[:collection_selection] || []
instance_exec selected_ids, &selected_batch_action.block
if action_present?
selection = params[:collection_selection] || []
inputs = JSON.parse params[:batch_action_inputs] || '{}'
inputs = inputs.with_indifferent_access.slice *current_batch_action.inputs.keys
instance_exec selection, inputs, &current_batch_action.block
else
raise "Couldn't find batch action \"#{params[:batch_action]}\""
end
end

protected

def selected_batch_action
return unless params[:batch_action].present?
active_admin_config.batch_actions.detect { |action| action.sym.to_s == params[:batch_action] }
def action_present?
params[:batch_action].present? && current_batch_action
end

def current_batch_action
active_admin_config.batch_actions.detect{ |action| action.sym.to_s == params[:batch_action] }
end

end
Expand Down