I have a helper method for ActiveAdmin that defines some actions that are the same across all models.
In 'app/helpers/active_admin/import_helper.rb'
module ActiveAdmin
module ImportHelper
def self.included(base)
base.instance_eval do
action_item only: :index do
link_to "Import", action: :import
end
collection_action :import do
render "admin/import"
end
controller do
def save_csvimport(item)
# .. import stuff
redirect_to action: :index
end
def permitted_params
params.permit!
end
end
end
end
end
end
In 'app/admin/categories.rb'
ActiveAdmin.register Store::Category do
include ImportHelper
config.filters = false
collection_action :importcsv, method: :post do
save_csvimport "Category"
end
end
On app boot, I get the following error:
app/helpers/active_admin/import_helper.rb:6:in `block in included': undefined method `action_item' for #<Module:0x007f93efabac40> (NoMethodError)
How do I define these methods across all the 'admin/*.rb' files? (This import functionality is the same across all models.)
I am using ruby 2.0 and rails 4.
EDIT:
When I define ImportHelper in 'app/admin/import_helper.rb' like so:
# Note no namespacing
module ImportHelper
def self.included(base)
base.instance_eval do
action_item only: :index do
link_to "Import", action: :import
end
collection_action :import do
render "admin/import"
end
controller do
def save_csvimport(item)
# .. Import stuff
redirect_to action: :index
end
def permitted_params
params.permit!
end
end
end
end
end
And the 'categories.rb' like so:
ActiveAdmin.register Store::Category do
config.filters = false
require_relative "./import_helper"
include ImportHelper
collection_action :importcsv, method: :post do
save_csvimport "Category"
end
end
Everything works. However, this seems messy to me, as the Import file should not be in 'app/admin' and the require_relative call should be unnecessary.