Split Active Admin index into two tables/panels based on a field - ruby-on-rails-3

I want to do something like this:
ActiveAdmin.register Split do
index do
panel "Cute" do
if cute?
column :blah
default_actions
end
end
panel "Not so cute" do
if not cute?
column :toot
default_actions
end
end
end
end
Where there's the Cute table which lists all the objects which cute? is true and then there's the Not so cute table where cute? is false.
The problem is that I can't figure out how to split it into two different tables/panels. I'm getting the following error:
undefined method `column' for <div class="index_as_table"></div>
:ActiveAdmin::Views::IndexAsTable
Which leads me to believe I shouldn't be using panel above column. I searched through the ActiveAdmin docs and couldn't find anything about splitting the index table view into two tables

The closest solution I know of, is using two different tabs (one for each panel) which is done automatically when setting scopes in activeadmin.
# app/admin/splits.rb
scope :cute, :default => true
scope :not_cute
# app/models/splits.rb
scope :cute, where(:cute=> true)
scope :not_cute, where(:cute => false)

Related

How can I write a form to input a list of text with ActiveAdmin?

I'm writing an ActiveAdmin form for a record which takes an Array of Strings. Let's call them widgets. I'm using a multi select form like so:
ActiveAdmin.register Things do
permit_params widgets[]
form do |f|
f.inputs do
...
f.input :widgets,
as: :select,
collection: [],
multiple: true
end
end
end
With a bit of Javascript to help the user can enter their widgets and it will be delivered as an Array of Strings just fine. But when editing an existing record the existing record.widgets are not filled into the field, the widgets field is blank. I suspect what's happening is ActiveAdmin/Formtastic is interpreting this select as a has_many relationship with Widget, and the Strings as Widget IDs. ActiveAdmin is ignoring the failure.
How can I write an input to pass a plain Array of Strings into a model with ActiveAdmin? I'd like my parameters as params[:thing][:widgets] = ["foo", "bar"]
By providing collection: [], you are telling the form to always start with blank array.
You need to provide the actual Thing's widgets to the form. You'll need to modify the example to play well with the select box / javascript you wrote - just bear in mind that collection takes an array - collection: resource.widgets.split(',').

How can I use a datepicker in ActiveAdmin without rewriting the create form?

My ActiveAdmin model creation forms (ie. /admin/<model>/new) are using an awkward set of dropdowns for dates and datetimes.
I'd like to use pickers instead. But all the documentation I've found about using a datetime picker appears to require rewriting the whole form. There doesn't seem to be a way to change just one input, nor an equivalent of preserve_default_filters!.
I'd like to either change the default for all date and datetime columns, something like...
config.datepicker = ...
Or per column, like...
column :signed_up, as: :datetime_picker
EDIT
By default I get new and edit forms equivalent to this.
form do |f|
f.semantic_errors
f.inputs
f.actions
end
If I want to change signed_up to use a datepicker, I have to repeat all the columns supplied by default just to change the input type of one column.
ActiveAdmin.register Thing do
form do |f|
t.semantic_errors
t.input :name
t.input :this
t.input :that
t.input :signed_up, as: :datepicker
t.input :other
t.input :thing
t.input :left
t.input :right
t.actions
end
end
That's a lot of unnecessary repetition, and the formatting doesn't come out right. I'm looking for a way to change the input presentation of one column, or one type, without having to manually write the whole form every time.
Try the DateNTimePickerActiveAdmin Rubygem. It works really well and is customisable according to your project.
You can find the documentation here: https://github.com/NikhithaGraceJosh/date_n_time_picker_activeadmin
From the docs:
Gemfile
gem 'date_n_time_picker_activeadmin'
Code Sample (In your form)
f.input :column_name, as: :datetimepicker
CSS
In active_admin.scss, add the line,
#import date_n_time_picker_activeadmin
JS
In active_admin.js, add the line,
//= require date_n_time_picker_activeadmin
Hope it's useful!
I managed to monkey patch Active Admin behavior, to use :datepicker instead of :date_select.
In your config/application.rb, add following patches
Bundler.require(*Rails.groups)
// Start patch
module ActiveAdminFormBuilderPatch
def default_input_type(*_args, **_kwargs)
ret = super
ret == :date_select ? :datepicker : ret
end
end
ActiveAdmin::FormBuilder.include ActiveAdminFormBuilderPatch
// end patch
module YourApp
class Application < Rails::Application
// configs
end
end
Note
Confirmed this to be working on activeadmin version 2.9.0.
I used activeadmin_addons which does a lot of useful things, including using DateTimePicker for input, but not filters.
To get filters I use active_admin_datetimepicker which makes jQuery DateTimePicker available for inputs (:date_time_picker) and filters (:date_time_range). Then I monkey patched ActiveAdmin to use it by default.
# config/initializers/active_admin.rb
# Make the default filter type for a DateTime to be the DateTimeRange picker.
class ActiveAdmin::Filters::FormBuilder
protected
alias_method :original_default_input_type, :default_input_type
def default_input_type(method, options = {})
case column_for(method)&.type
when :datetime
return :date_time_range
end
original_default_input_type(method, options)
end
end

Simple_form association multiple

I am using a simpleform collection association input, as checkboxes in order to allow users to choose 1+ items from a list as part of an order. The list has a has_and_belongs_to_many association with the overall order. I want them to be able to do multiple of the same items, however. So I would want a small number input next to each checkbox. I can handle the javascript, I am just wondering how to do this with simpleform, if its even possible.
Thanks!
It is possible if you write a custom input for this specific task.
You would need to put it in # app/inputs/your_input.rb for Simple_form to automatically pick it up.
Then in the file :
class YourInput < SimpleForm::Inputs::Base
def input
# Your code here, but I just pasted the example that adds something after the existing field already as a reference.
"$ #{#builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
and in the form :
f.input :money, as: :your
In case of checkbox, the method name will change to "check_boxes" from "input"

mongoid - Dynamic date/time attribute - MultiParameter Exception

I have a model named Form, which get fields as per user requirement, eg if user puts text field in the form then a attribute is created in Form model for storing string data.
Similarly I want to store date/time and datetime values. So I added
include Mongoid::MultiParameterAttributes
in the form model, because date and time values are submitted from for in multiple attributes.
But I get Mongoid::MultiParameterAttributes::Errors::MultiparameterAssignmentErrors exception in the controller create action, on the line #form = Form.new(params[:form])
def create
#form = Form.new(params[:form])
if #form.save
redirect_to(form_path(#form))
else
redirect_to :action => "new"
end
end
How do I get through this.
Please Help
Modify your Form class so it looks like the one below.
class Form
include Mongoid::Document
include Mongoid::MultiParameterAttributes
...
end
The ruby driver can only serialize Time objects. That might be your problem.

Searching issue with the rails-sunspot gem

I'm very new to Solr and the Rails Sunspot gem, but it looks very promising for complex searching on a big database.
What I'm trying to do is allow a model in my rails app to be searched on a few fulltext columns and then a collection of its "filters" (which are just a has_and_belongs_to_many association of names).
I tried setting up my model search block as follows
self.searchable do
text :name, :boost => 5
text :description, :instructions
text :filters do
filters.map(&:name)
end
end
And my controller looks like so:
#search = ModelName.search do
keywords params[:q].to_s
end
However, I cannot seem to produce any results based on keywords found in the filters association. Am I doing something wrong? This is all very new to me.
When you initially set up your classes for search, you need to reindex the data into Solr. Have you done that? If not:
rake sunspot:reindex