Display the total for a single columns values in Activeadmin - ruby-on-rails

I am trying to display a box at the bottom of my ActiveAdmin index page where it will total all of the values of the collection and display this total value. I am trying to sum the value of the column :number_books and I am currently using this code as suggested when someone answered a similar question a few years ago.
This does not appear to be having any impact on my index page as nothing is visibly changing on the index page for :orders.
Any help is greatly appreciated!
ActiveAdmin.register Order do
index do
column :email
column :customer_name
column :number_books
column :street_address
column :state
column :zip_code
column :total
default_actions
div class: "panel" do
h3 "Total amount: #{collection.pluck(:number_books).reduce(:+)}"
end
end
end

I found out I had to define this new index page I was creating as the default, otherwise activeadmin uses the default index page and ignores my custom one. I also had to change default_actions to simply actions and this now works great!
the working code is:
ActiveAdmin.register Order do
index default: true do
column :email
column :customer_name
column :number_books
column :street_address
column :state
column :zip_code
column :total
actions
div class: "panel" do
h3 "Total amount: #{collection.pluck(:number_books).reduce(:+)}"
end
end
end

Related

How to change css rails admin?

I'm using Rails admin. I have a model named Order. The Order model has a Description field. When I look at the list of orders in the rails admin, the description fields of the orders are not completely showing. Only a portion of text and ellipsis is shown up.
Example: Some information about ordering ....
How to show the Description displayed completely without cutting?
config.model Orders do
list do
filters [:status, :created_at]
field :created_at do
label "Request date"
strftime_format "%b %d, %l:%M%P %Y"
end
field :description
field :status
field :scheduled_date do
strftime_format "%a %b, %d"
end
field :address do
filterable false
end
end
end
You should be able to increase the length for the description field this way:
field :description, :text do
html_attributes do
{:maxlength => 600}
end
end

ActiveAdmin status_tag not rendering inside table

I'm working on my first activeadmin project and I'm trying to create a panel on the dashboard that displays a status for each of my estimates.
The status_tag is displaying outside the table though. If i change it to just some text value rather than the status_tag it works just fine.
Why is the status_tag way up top and how can I fix it?
Example screenshot
http://snag.gy/iHYz2.jpg
ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }
content title: proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Recent Estimates" do
table_for Estimate.last(5) do
column :description
column :name
column :status, status_tag('In Progress')
end
end
end
end
end # content
end
I figured it out. It seems that instead of this:
column :status, status_tag('In Progress')
I needed to do:
column(:status) {|estimate| status_tag(estimate.status)}

Override index columns activeadmin

So I would like to have a shortened table with batch actions.
ActiveAdmin.register User do
batch_action :acitve do |selection|
User.find(selection).each do |user|
user.active! true
end
end
filter :email
index do
column :id
column :first_name
column :last_name
column :email
column :sign_in_count
default_actions
end
end
However batch action box is greyd out. It's understanbale because nothing is selected. However when I use default index settings (no columns specification), the checkbox stays there. How can I have a default checkbox with custom columns?
according to this (Customizing Table Columns part) you need to add
index do
selectable_column #batch actions checkboxes column
column ...
...
end
to render checkboxes

Active admin shows html code

I'm using ejholmes Active_admin Editor and it's working fine but in my active admin index I see the html code with tags and not the correct output. I do use .html_safe in my views but here's my active admin's posts.rb
...
index.do
column :id
column "Titolo", :title
column "Corpo news", :body
column "Creato il", :created_at
column "Modificato il", :updated_at
column :link
column "Link Immagine", :image_url
bool_column :attivo
default_actions
end
...
I want to have my "Corpo news" column rendered.
Thanks for the help!
I guess you need to use the method .to_html as you can see by the source
So in the view you need to call
body.to_html

ActiveAdmin how to get sum from column

I have a situation where I'd like to get the sum of one column and display it
e.g in activeadmin
ActiveAdmin.register Expense do
index do
column :amount
column :details
column :created_at
default_actions
end
end
I need to sum the amount column and show it. Also I can't figure out where to show the Total Sum, maybe the sidebar?
If the results are filtered then the sum has to change accordingly to results shown.
You can avoid having to count up with a running total, by simply accessing the underlying collection which will take into account currently applied filters/scopes, and use reduce(:+) to perform the sum:
ActiveAdmin.register Expense do
index do
column :amount
column :details
column :created_at
default_actions
div class: "panel" do
h3 "Total amount: #{collection.pluck(:amount).reduce(:+)}"
end
end
end
ActiveAdmin.register Expense do
amount = 0
index do
column :amount
column :details
column :created_at
column("sub amount") {|resource| amount = amount + resource.amount}
default_actions
div :class => "panel" do
h3 "Total: #{amount}"
end
end
end
I had the same question and couldn't find the answer, so after playing around a bit, I came up with this:
ActiveAdmin.register Expense do
index do
column :amount
column :details
column :created_at
default_actions
div :class => "panel" do
h3 "Total: #{Expense.search(params[:q]).result.sum(:amount)}"
end
end
end
When I tried to use the code that was mention above with "amount=0" above the "index do", when I refresh the admin page the value get increase. This solved my problem.
ActiveAdmin.register Expense do
index do
amount = 0
column :amount
column :details
column :created_at
column("sub amount") {|resource| amount = amount + resource.amount}
default_actions
div :class => "panel" do
h3 "Total: #{amount}"
end
end
end
I prefer to see the amount only on the panel without the column ("sub amount") as do this code:
ActiveAdmin.register Expense do
index do
amount = 0
column :amount
column :details
column :created_at
registers.each {|resource| amount += resource.amount}
default_actions
div :class => "panel" do
h3 "Total: #{amount}"
end
end
end
ActiveAdmin.register Expense do
index do
sum = 0
column "Amount" do |expense|
sum += expense.amount
expense.amount
end
column :details
column :created_at
default_actions
div :class => "panel" do
h3 "Total: #{sum}"
end
end
end

Resources