How to add an action in active_admin in rails - ruby-on-rails

I am working on active_admin in rails in which I want to add an action which will run a service.
action should appear in the index view. so, how can i add a action which runs this service.
My code of app/admin/website.rb is
ActiveAdmin.register Website do
actions :index
index do
selectable_column
id_column
column :state
column :name
column :created_at
column :updated_at
end
end
in above file i want to check that id state='draft' then it should run the action which runs a service.
My service file app/services/website_verify_state_service.rb:
class WebsiteVerifyStateService
def self.process!(website)
new(website).process
end
def initialize(website)
#website = website
end
def process
site_response = self.class.post("#{BASE_MONO_URL}reseller/account/site",
site_data = JSON.parse(site_response.body)['data']
if site_data
#website.update(published: true) if site_data[0]['site']['lastPublishDate'].present?
end
end
end
SO my only concern is that how i call this service through action when from active admin.
Thanks in advance

You can add conditional actions to the index table:
ActiveAdmin.register Website do
actions :index
index do
selectable_column
id_column
column :state
column :name
column :created_at
column :updated_at
actions do |website|
item('Verify state', verify_admin_websites_path(website.id), class: 'member_link') if website.state == 'draft'
end
end
member_action :verify do
# do your magic here, like or whatever you do
WebsiteVerifyStateService.process!
redirect_back(fallback_location: root_path)
end
end

Related

Rails Active Admin : How to have a "Approve" button in the active admin index view

I am just exploring active admin but got stuck in adding an action button ( approve / reject ) in the index view.
Here is the code snippet.
In, app/admin/user.rb
index do
selectable_column
id_column
column :email
column :current_sign_in_at
column :sign_in_count
column :created_at
column '' do
# Here is where I need a button to "Approve / Reject" the user(s).
end
actions
end
You got stuck adding an action button ...
Here's how to do it
# app/admin/some_class.rb
ActiveAdmin.register SomeClass do
action_item :approve, only: :index do
link_to "Approve", some_path
end
index do
selectable_column
id_column
column :email
column :current_sign_in_at
column :sign_in_count
column :created_at
actions
end
end
You can use link_to inside the block
column "Approve / Reject" do
link_to("Approve / Reject", some_path)
end

how to set before filter to index action in active admin?

I want to display something before a particular page loading.(Like a flash message after checking some condition). I am using active admin. How can i do this?
ActiveAdmin.register User do
config.batch_actions = false
config.paginate = false
menu false
actions :index, :destroy, :show
index do
column 'ID', :id
column :name
column :short_name
column :start_date
column :end_date
column :aggregatable
column :branch do |user|
link_to user.branch.name, active_user_branch_path(id: user.branch_id)
end
column :lock_status
default_actions( name: 'Actions' )
end
end
Answering the question regarding how to set before filter in ActiveAdmin:
before filter should be set in controller.
Pattern:
controller do
before_filter :my_filter, only: %i(index)
private
def my_filter
#logic here
end
end

ActiveAdmin Custom page with index table

I have created a custom page with ActiveAdmin as follows:
ActiveAdmin.register_page "message_list" do
controller do
def index
#collection = client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
render :layout => 'active_admin'
end
end
end
I have created an index.html.erb file with a table that I want to display on this page. This however is not optimal. How do I use the active admin standard table layout that also comes with pagination and display it with my table info? I know that ActiveAdmin PageDSL Class does not include #index and therefore I can't simply do:
index do
selectable_column
id_column
column :to
column :from
default_actions
end
In addition to achieving the ActiveAdmin table layout on a custom page, how do I change the Title of the page itself? As of now it is called "Index".
An easier method would be to define an ActiveAdmin resource for your message class, Message, and limit the actions to only allow :index.
ActiveAdmin.register Message do
actions :index
index do
selectable_column
id_column
column :to
column :from
default_actions
end
controller do
def scoped_collection
super.where(account_id: account.id).order(:date_sent)
# Or provide a custom collection similar to the current implementation:
# client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
end
end
end
It is also possible to rename the resource if necessary by providing an :as option to the #register method:
ActiveAdmin.register Message, as: "Account Message" do
# ...
end
While the accepted answer works well if you can use an ActiveAdmin resource instead of a custom page, it is possible to get an index-style table on a custom page via Arbre:
<%=
Arbre::Context.new({}, self) do
table_for(client().account.messages, sortable: true, class: 'index_table') do
column :id
column :created_at
end
end
%>

ActiveAdmin limit records on Index page

I have some 10,000+ records in my model. In active_admin index page for that model I have set config.paginate = false. So all the 10,000+ records are shown by default.
How can I limit the number to say last 500 records. I have tried using the below method described here, but its not doing anything to the index page.
ActiveAdmin.register Post do
controller do
def scoped_collection
Post.all.limit(500)
end
end
end
set custom # of rows on page with controller before_filter
controller do
before_filter :set_per_page_var, :only => [:index]
def set_per_page_var
session[:per_page]=params[:per_page]||30
#per_page = session[:per_page]
end
end
and render sidebar with corresponding text input (you can render it as a drop-list)
#...
sidebar('Rows on page', :only => :index) do
form do |f|
f.text_field nil, 'per_page', :value => session[:per_page]
end
end
The issue is this code in Active Admin:
module ActiveAdmin
class ResourceController < BaseController
module DataAccess
def per_page
return max_csv_records if request.format == 'text/csv'
return max_per_page if active_admin_config.paginate == false
#per_page || active_admin_config.per_page
end
def max_csv_records
10_000
end
def max_per_page
10_000
end
end
end
end
When the paginate config option is set to false, it defaults to the number value returned by max_per_page. If you're fine with overriding it globally, you can put this in an initializer:
# config/initializers/active_admin_data_access.rb
module ActiveAdmin
class ResourceController < BaseController
module DataAccess
def max_per_page
500 # was 10,000
end
end
end
end
I was looking for an answer to this same question. I was unable to limit the number of records, so instead I have opted for putting a default value in one of my filters that guarantees an empty page when it loads.
(NOTE: I stole this idea from this stackoverflow question here:: Set ActiveAdmin filter default value )
Example::
In this example, I set a filter called "my_filter_id" equal to "0" in the "before_filter" method if all of the parameters are blank.
ActiveAdmin.register MyModel do
before_filter my_filter_id: :index do
params[:q] = {my_filter_id_eq: 0} if params[:commit].blank?
end
end
Use
Post.limit(500) instead of Post.all.limit(500) so it will minimize the latency.
controller do
def scoped_collection
Post.limit(500)
end
end
index :pagination_total => false do
selectable_column
column :id
column :user_name
column :country
column :city
end
Hope this will help someone.
Try below code. Replace something with your model name.
result = Something.find(:all, :order => "id desc", :limit => 5)
while !result.empty?
puts result.pop
end

Customise layout (components ) for activeadmin

Activeadmin layout is not just a file, its a collection of components.
How can I override some of the components like, logo, navigation with activeadmin.
The activeadmin layout components are very easy to customize. What you need to do: Just define a module that opens ActiveAdmin::View.
you can have a custom_activeadmin_components.rb in initializers or admin directory where you defined all of your activeadmin resources. I prefer to put it in the directory where your activeadmin resources are. Then override any module you want:
here is a sample:
module ActiveAdmin
module Views
class Header < Component
def build(namespace, menu)
super(:id => "header")
#namespace = namespace
#menu = menu
#utility_menu = #namespace.fetch_menu(:utility_navigation)
build_site_title
build_global_navigation
build_utility_navigation
#you can add any other component here in header section
end
def build_site_title
render "admin/parts/logo"
end
def build_global_navigation
render "admin/parts/main_nav"
end
def build_utility_navigation
render 'admin/parts/language_options'
insert_tag view_factory.global_navigation, #utility_menu, :id => "utility_nav", :class => 'header-item tabs'
render 'admin/parts/branch_in_header'
end
end
module Pages
class Base
def build_page_content
build_flash_messages
div :id => :wizard_progress_bar do
render 'admin/parts/wizard_progress_bar'
end
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
end
end
end
end
You can customise active admin page in admin/your_model.rb file.
Sample code for active admin is as follows.
ActiveAdmin.register User do
menu :label => "Our User", :priority => 3 #rename menu & set priority#
#for index page#
index :title => 'Our User' do #set page title#
# index :download_links => false do
selectable_column
column :title
column :category do |e| #want to change name of category
e.categoryone
end
column :address
default_actions#this will add default action i.e. show|edit|delete#
end
#end index#
#for controller#
controller do
actions :all, :except => [:edit, :new] # you can decide which all methods to be shown in show page.
end
#end controller#
#show start page#
show do |user|
h3 "User Details"
attributes_table do
row :title
row :description
row :address, :label=>"User Address" #overide address label
row :country
row :approval
end
h3 "Activity Photoes"
attributes_table do
user.uploads.each do |img| #call associated model. Here i want to display uploaded image of upload.rb file
row(" ") do
link_to image_tag(img.to_jq_upload['small_url']), admin_upload_path(img)#here i have added upload module to admin thats y i can directly give path to the upload record of admin module#
end
end
end
active_admin_comments # to show comment block of active admin
end
#show end#
#filer(search) start righthand side panel#
filter :title
filter :category
filter :address
#end filter#
#for menu on show page#
action_item only:[:show] do # this add "Approve User" button on the right hand side of show menu only as we have specified only show method
if User.find(params[:id]).approval == true
link_to "Approve User", approv_user_path(:id => params[:id])#call custom method of normal rails controller#
end
end
#end menu#
#start add side bar on page#
sidebar "Project Details" do
ul do
li link_to("Profile", new_project_path)
end
end
#end side bar#
end

Resources