{"id":19773143,"url":"https://github.com/activeadmin-plugins/active_admin_scoped_collection_actions","last_synced_at":"2025-04-06T16:11:48.023Z","repository":{"id":34541020,"uuid":"38485194","full_name":"activeadmin-plugins/active_admin_scoped_collection_actions","owner":"activeadmin-plugins","description":":muscle: Plugin for ActiveAdmin. Provides batch Update and Delete for scoped_collection (Filters + Scope) across all pages.","archived":false,"fork":false,"pushed_at":"2024-03-02T16:56:40.000Z","size":328,"stargazers_count":57,"open_issues_count":3,"forks_count":25,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T13:09:44.815Z","etag":null,"topics":["activeadmin","batch-actions"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/activeadmin-plugins.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-07-03T09:40:45.000Z","updated_at":"2025-02-10T11:21:24.000Z","dependencies_parsed_at":"2024-03-02T15:49:06.640Z","dependency_job_id":null,"html_url":"https://github.com/activeadmin-plugins/active_admin_scoped_collection_actions","commit_stats":{"total_commits":53,"total_committers":12,"mean_commits":4.416666666666667,"dds":0.6603773584905661,"last_synced_commit":"090d4e5264192d7a8fdb34e313633af2bfcde679"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activeadmin-plugins%2Factive_admin_scoped_collection_actions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activeadmin-plugins%2Factive_admin_scoped_collection_actions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activeadmin-plugins%2Factive_admin_scoped_collection_actions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activeadmin-plugins%2Factive_admin_scoped_collection_actions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/activeadmin-plugins","download_url":"https://codeload.github.com/activeadmin-plugins/active_admin_scoped_collection_actions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247509223,"owners_count":20950232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activeadmin","batch-actions"],"created_at":"2024-11-12T05:08:47.120Z","updated_at":"2025-04-06T16:11:48.005Z","avatar_url":"https://github.com/activeadmin-plugins.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://img.shields.io/travis/activeadmin-plugins/active_admin_scoped_collection_actions.svg)](https://travis-ci.org/activeadmin-plugins/active_admin_scoped_collection_actions)\n\n# ActiveAdmin Scoped Collection Actions\nPlugin for ActiveAdmin. Provides batch Update and Delete for scoped_collection (Filters + Scope) across all pages.\n\n![Step 1](/screenshots/sidebar.png)\n\n![Step 1](/screenshots/pupup.png)\n\n\n# Description\n\nThis gem give you ability to perform various batch actions on any filtered (or scoped) resource. Action applies to all records across all pages. It is similar to ActiveAdmin batch action, but affects all filtered records. This is usefull if you want to delete or update a lot of records in one click.\n\n# Install\n\nAdd this line to your application's Gemfile:\n\n```ruby\n# last version\ngem 'active_admin_scoped_collection_actions'\n# master branch\ngem 'active_admin_scoped_collection_actions', github: 'activeadmin-plugins/active_admin_scoped_collection_actions'\n```\n\nAnd then execute:\n\n```\n$ bundle\n```\n\nAdd the following line at the end of \"app/assets/javascript/active_admin.js\":\n\n```javascript\n//= require active_admin_scoped_collection_actions\n```\n\nAlso include CSS in \"app/assets/stylesheets/active_admin.css.scss\"\n\n```css\n@import \"active_admin_scoped_collection_actions\";\n```\n\n# Usage\n\nUsually you need two standard actions: Delete and Update.\n\nFor example, if you have resource Posts and you want to have a delete action, add:\n\n```ruby\nscoped_collection_action :scoped_collection_destroy\n```\n\nExample:\n\n```ruby\nActiveAdmin.register Post do\n  config.batch_actions = true\n\n  scoped_collection_action :scoped_collection_destroy\n\n  index do\n    # ...\n  end\nend\n```\n\n**Important**: Visit Posts page with your browser and you will see no changes. Now, perform any filter with the Filters sidebar. Only after you filter will you see a delete button. It will be in sidebar under Filters.\n\n### Update action\n\nUpdate is second standard action and is more complex. It has \"form\" hash wrapped in Proc:\n\n```ruby\n  scoped_collection_action :scoped_collection_update, form: -\u003e do\n                                         { name: 'text',\n                                            diagonal: 'text',\n                                            manufactured_at: 'datepicker',\n                                            vendor_id: Vendor.all.map { |region| [region.name, region.id] },\n                                            has_3g: [['Yes', 't'], ['No', 'f']]\n                                          }\n                                        end\n```\n\nIn this example Phone model has fields:\n * name - varchar string\n * diagonal - integer(of float)\n * manufactured_at - datetime\n * vendor_id - association \"belongs_to :vendor, class_name: 'Vendor', foreign_key: :vendor_id\"\n * has_3g - boolean\n\nParameter \"form\" is a proc object which returns Hash. It defines what fields you want to be able to update. Hash keys are  column names in database. Hash values are a types of HTML inputs. We support only \"text\", \"datepicker\" and \"selectbox\". If you want something more complex - you can build your own forms.\n\n# Custom Actions\n\nExample: We have Phone resource and it has column \"manufactured_at\". We need an action which will erase this date.\n\nIn ActiveAdmin resource:\n\n```ruby\nActiveAdmin.register Phone do\n  config.batch_actions = true\n\n  scoped_collection_action :erase_date do\n    scoped_collection_records.update_all(manufactured_at: nil)\n  end\n\n  index do\n    # ...\n  end\nend\n```\n\nThis simple code will create a new button \"Erase date\" in sidebar. After clicking this button, the user will see confirm message \"Are you sure?\". After confirming, all filtered records will be updated.\n\n\n# Details and Settings\n\n\n### Why I don't see Sidebar with Collection Actions.\n\nSidebar visibility by default depends on several things.\n\nFirst you must set:\n\n```ruby\nconfig.batch_actions = true\n```\n\nActually, inside of this Gem we use \"batch_actions\". So without them Collection Actions wouldn't work.\n\n```ruby\nscoped_collection_action :something_here\n```\n\nYou resource should have some collection actions. If it doesn't have any, the sidebar will not appear.\n\nBy default we dont allow perform actions on **all** the records. We want protect you from accidental deleting.\n\nSidebar with buttons will appear only after you perform filtering or scopes on resource records.\n\nAnd lastly you can manage sidebar visibility by resource config:\n\n```ruby\n# Always\nconfig.scoped_collection_actions_if = -\u003e { true }\n# Only for scopes\nconfig.scoped_collection_actions_if = -\u003e { params[:scope] }\n# etc.\n```\n\n### Can I use my handler on update/delete action?\n\nYou can pass block to default actions update and delete.\nAnd do custom redirect after it. Use `render` (location: 'something') instead of `redirect_to()`.\n\nThis example renders form which allows to change `name` field. And after it do redirect to dashboard page.\n\n```ruby\n  scoped_collection_action :scoped_collection_update,\n                           form: -\u003e {\n                             {name: 'text'}\n                           } do\n    scoped_collection_records.update_all(name: params[:changes][:name])\n    flash[:notice] = 'Name successfully changed.'\n    render nothing: true, status: :no_content, location: admin_dashboard_path\n  end\n```\n\n\n### How can I rename button?\n\nEvery scoped_collection_action has option `:title`.\n\nExample:\n\n```ruby\n  scoped_collection_action :erase_date, title: 'Nullify' do\n    scoped_collection_records.update_all(manufactured_at: nil)\n  end\n```\n\n\n### How can I modify modal dialog title?\n\nSimilar to button title. Use option `:confirm`\n\n```ruby\n  scoped_collection_action :scoped_collection_destroy, confirm: 'Delete all phones?'\n```\n\n\n### Can I replace you pop-up form with my own?\n\nYes. But also you must take care of mandatory parameters passed to the server.\n\n\n```ruby\n  scoped_collection_action :my_pop_action, class: 'my_popup'\n```\n\nNow in HTML page, you have button:\n\n```html\n  \u003cbutton class=\"my_popup\" data=\"{\u0026quot;auth_token\u0026quot;:\u0026quot;2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=\u0026quot;,\u0026quot;batch_action\u0026quot;:\u0026quot;my_pop_action\u0026quot;,\u0026quot;confirm\u0026quot;:\u0026quot;Are you sure?\u0026quot;}\"\u003eMy pop action\u003c/button\u003e\n```\n\nWithout handler, clicking on the button does nothing.\n\nYou can render the form in any way you want:\n  - It can be some popup(Fancybox, Simplemodal, etc.), or some inline collapsible form.\n  - It can even be a separate full-page.\n\nOne thing is important - how you will send data to server. Generally it should be:\n\nPOST request\n\nURL: /admin/collection_path/batch_action\n\nwith GET params identical to current page\n\nThe easiest way to get them is:\n\n```javascript\nurl = window.location.pathname + '/batch_action' + window.location.search\n```\n\nAnd Request body params should be like:\n\n```\nchanges[manufactured_at] = \"2015-07-21 18:11\"\nchanges[diagonal] = \"7\"\nchanges[some_filed_name]='new value'\nauthenticity_token = \"2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=\"\nbatch_action = \"my_pop_action\"\n```\n\n```authenticity_token``` and ```batch_action``` you can get from data-attribute of the Button.\n\n\nExample in JavaScript:\n\n```javascript\n  url = window.location.pathname + '/batch_action' + window.location.search\n  form_data = {\n    changes: {\"manufactured_at\": \"2015-07-21 18:11\", \"diagonal\": \"7\"},\n    collection_selection: [],\n    authenticity_token: \"2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=\",\n    batch_action: \"my_pop_action\"\n  }\n  $.post(url, form_data).always () -\u003e\n    window.location.reload()\n```\n\n### How notify user about success and error operations?\n\nWe recommend using Rails Flash messages.\n\nExample with updating phone diagonal attribute. In this case model Phone has validation:\n\n```ruby\n  class Phone \u003c ActiveRecord::Base\n    validates :diagonal, numericality: { only_integer: true }\n  end\n```\n\n```ruby\n  scoped_collection_action :change_diagonal, form: { diagonal: 'text' }  do\n    errors = []\n    scoped_collection_records.find_each do |record|\n      errors \u003c\u003c \"#{record.errors.full_messages.join('. ')}\" unless record.update(diagonal: params[:changes][:diagonal])\n    end\n    if errors.empty?\n      flash[:notice] = 'Diagonal changed successfully'\n    else\n      flash[:error] = errors.join('. ')\n    end\n    render nothing: true, status: :no_content\n  end\n```\n\nWhen you try to update diagonal with \"5.6\" you will see flash error:\n\n```\nDiagonal must be an integer.\n```\n\nBut if you use your custom popup, you can show messages with JS.\n\n\n### Can I perform action only on selected items?\n\nStandard index-page of a resource with batch_action enabled has selectable column.\n\nIf you checked some items and parform any Collection Action, the handler will take care of it. If you write custom actions, you should do like this:\n\n```ruby\n  scoped_collection_action :do_something do\n    scoped_collection_records.find_each do |record|\n      record.update(name: 'x')\n    end\n  end\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factiveadmin-plugins%2Factive_admin_scoped_collection_actions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Factiveadmin-plugins%2Factive_admin_scoped_collection_actions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factiveadmin-plugins%2Factive_admin_scoped_collection_actions/lists"}