how can we create partials in active admin for index panel - ruby-on-rails

I want to create a partials to render some data in index panel for different namespaces for DRY
I am currently writing
index do
render 'index', user: :user
end
//_index.html.arb
column :id
column 'Customer Name', :name
column :mobile
column :recipient_number
column :cash_in_hand do |customer|
number_to_currency(customer.cash_in_hand, unit: "\u20B9", precision: 2)
end
column "Due Balance" do |customer|
number_to_currency(customer.due_balance, unit: "\u20B9", precision: 2)
end
actions

You can create a partial to render that data exactly like this
# app/admin/some_class.rb
index do
render 'admin/index', context: self
end
You probably will want to create a folder called 'admin' in views for these types of partials ...
# app/views/admin/_index.html.erb
<% context.instance_eval do
column :id
column 'Customer Name', :name
column :mobile
column :recipient_number
column :cash_in_hand do |customer|
number_to_currency(customer.cash_in_hand, unit: "\u20B9", precision: 2)
end
column "Due Balance" do |customer|
number_to_currency(customer.due_balance, unit: "\u20B9", precision: 2)
end
actions
end %>
I can confirm this works with .erb extention file and .haml but no guaranteeing others

This works for me in Rails 6 with:
#app/admin/some_class.rb
index do
render partial: 'active_admin/index', locals:{context: self}
end
and
#app/views/active_admin/_index.html.arb
context.instance_eval do
selectable_column
id_column
column :some_attribute
actions
end
This also satisfies locating the file in a shared place not specific to the admin/ space. It's useful because I have AA running with two separate user models and I often want to share the same code for index, show, etc with minor differences.
If needed, additional key-values can be passed in the locals hash for minor customization of the results.

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

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

ActiveAdmin show, edit, and delete actions in custom models

I have a rails 3 application which uses the ActiveAdmin gem.
If I do not customizes my models, 3 actions are enabled in each line of my model : show, edit, delete
But if I customizes my model, the actions disappear.
Model not customized showing the actions (users.rb) :
ActiveAdmin.register User, as: 'Users_full' do
menu :parent => 'Users'
end
Custom model not showing actions (companies.rb) :
ActiveAdmin.register Company do
index do
selectable_column
column :name
column :url
end
csv do
column :name
column :url
end
end
Is there a way to get actions in customized models ? I have already tried to add : actions :all, config.batch_actions = true and action_item to my companies.rb file but nothing change.
Add the actions, like:
index do
selectable_column
column :name
column :url
actions
end
You're defining the index page's content, and that content includes the actions-if you omit them, they won't show up.

ActiveAdmin (Rails) empty class attribute for custom index table rows

I am using ActiveAdmin to render an index table for a model.
The config looks like this:
ActiveAdmin.register User do
index do
selectable_column
column :username
column "Email" do |u|
raw "<span title='#{u.email}'>#{truncate(u.email, length: 14)}</span>"
end
end
end
The rendered HTML for the "email" TD looks like this:
<td class=""><span title="useremail#example.com">useremail...</span></td>
I have tried passing class: 'foo' before the block and a few things like that but none have worked.
All of the columns that are not being created with the block syntax are getting a class attribute equal to the field name.
How do I set the class for the TD when using a block to render the column?
you have to override build_table_cell method,
you can take needed code here https://gist.github.com/3995962
put it to initializers folder.
now you can use it like
column(:status, :sortable => 'enabled', :class=>'status') do |row|
#some logic
end

How to I make a drop down beside a search box that searches the specific field selected in rails?

Okay so im new to this site but this is what I have:
Report.rb
def self.search(search)
if search
where('JOBLETTER_CD_NUMBER LIKE ? AND DATE LIKE? AND CUST LIKE ?', "%#{search}%")
else
scoped
end
end
end
index.html.erb
select_tag "search", options_for_select([ "Job Letter and CD #", "Date", "Cust", "Job", "Date shipped", "Date billed", "Billed by" ], params[:search])
form_tag reports_path, :method => 'get' do
text_field_tag :search, params[:search], :class=> "form-search", :align => "right"
<%= submit_tag "Search", :JOBLETTER_CD_NUMBER => nil, :class => "btn btn-success", :align => "right"
reports controller
def index
#report = Report.paginate(:per_page => 1, :page => params[:page])
#report = Report.search(params[:search]).paginate(:per_page => 1, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #views }
end
end
The only field it will search is the Job Letter and CD # field I need it to allow me to search whatever is selected in the drop down box. Btw I am using bootstrap fro js and css functions.
Your query has 3 placeholders ? but passed only one argument "#{search}" - if you run it like that, what you really should be getting is an exceptions stating
ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (1 for 3) ...
Also, your select_tag is outside the form, so it won't be passed to the controller at all. If you move it into the form, you'd have to rename (e.g. to column) it since the name search is already used by the text field. Then you could pass both the column and the search parameters to your search function to construct the query.
HOWEVER, this is not safe, since nothing prevents a user to pass in any other column by manipulating the post request, and since you can't use placeholders for column names, there's a danger of SQL injection as well.
There are many solutions out there to construct searches, no need to reinvent the wheel. Take a look at the ransack gem. Here's a recent Railscast on how to use it.

Resources