ActiveAdmin how to get sum from column - ruby-on-rails

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

Related

Display the total for a single columns values in Activeadmin

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

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

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

Iterating through columns on active_admin model

New to Ruby and Rails, so perhaps I'm not searching/asking this the right way. I'm using the ActiveAdmin gem and want to turn this:
column :purchase_price, :sortable => :purchase_price do |piece|
div :class => "price" do
number_to_currency piece.purchase_price
end
end
column :appraised_value, :sortable => :appraised_value do |piece|
div :class => "price" do
number_to_currency piece.appraised_value
end
end
column :sale_price, :sortable => :sale_price do |piece|
div :class => "price" do
number_to_currency piece.sale_price
end
end
into this:
price_array = [:purchase_price, :appraised_value, :sale_price]
price_array.each do |p|
column p, :sortable => p do |piece|
div :class => "price" do
number_to_currency piece.p
end
end
end
...in the interest of DRY.
The longer solution works, but the shorter one gives a "NoMethodError in Admin::Pieces#index" and I'm kind of at a loss as to what's wrong. Any suggestions?
As house9 said, my problem was that I tried calling piece.p when really it should be piece.send(p). Still not exactly sure why you can't just replace all instances of p with the iterator loop like I did originally but maybe someone else can explain.
In any case, thank you!

Rails ActiveAdmin index formatting numbers

I'd like to have my numbers right aligned and with a thousands separator. Can someone point me in the right direction?
ActiveAdmin.register Thing do
index do
column :id
column :amount # need to make this fomatted nicely
default_actions
end
end
You can pass a block to the column.
column :amount do |thing|
div :class => "amount" do
number_to_currency thing.amount
end
end
css
.amount {
text-align :right;
}
This railscast goes through some pretty good info too http://railscasts.com/episodes/284-active-admin?view=asciicast
Alternately:
column :amount, :class => 'text-right' do |thing|
number_to_currency thing.amount
end
then in your CSS
.text-right { text-align: right;}
you can use Active Admin Addons gem to improve the UI
https://github.com/platanus/activeadmin_addons/blob/master/docs/number-formatting.md

Resources