Filter by multiple hstore keys in active admin
What I ended up doing was using a custom scope like so:
# Ransackable Scopes
def self.ransackable_scopes(_auth_object = nil)
[:by_title]
end
# My scopes
scope :by_title, ->(search_term) { by_field(search_term, :title) }
scope :by_field, lambda { |search_term, field|
where("#{field}_translations -> :key ILIKE :value", key: :en, value: "%#{search_term}%")
.or(where("#{field}_translations -> :key ILIKE :value", key: :fr, value: "%#{search_term}%"))
.or(where("#{field}_translations -> :key ILIKE :value", key: :de, value: "%#{search_term}%"))
}
It doesn't exactly answer the question, but was a solution that worked for me needs.
Activeadmin and Formtastic with HStore column
For anyone who happens to stumble across this, I was able to use the activeadmin_hstore_editor gem for this purpose, which gives a way to input arbitrary json into an input field.
ActiveAdmin Custom Scope with hstore
So I was able to find out a solution for my question. Found this from:
https://github.com/activerecord-hackery/ransack/issues/267#
Heres what I did for the fix:
admin/user.rb
filter :upload_eq, label: 'User Upload Ability', as: :select, collection: { 'On' => 'false', 'Off' => 'true' }
user.rb
ransacker :upload do |parent|
Arel::Nodes::InfixOperation.new('->', parent.table[:properties], Arel::Nodes.build_quoted('upload'))
end
Turns out I didn't need to use the ransackable_scopes
method in order to achieve this. And because I am using Rails 4.2 I had to wrap build_quoted
around the upload
property because I was getting a unsupported: String
error (https://github.com/rails/arel/issues/323).
ActiveAdmin filters- find all with a blank field
filter
by itself doesn't allow you to search for blanks, I think you have to workaround by adding a ransacker to your User model:
ransacker :by_supervisor, formatter: proc{ |v|
v=nil if v==0
data = User.where(supervisor: v).ids
data = data.present? ? data : nil
} do |parent|
parent.table[:id]
end
and in the ActiveAdmin module instead of the normal filter
filter :supervisor, collection: [Supervisor.new(id: 0, name: 'None'), Supervisor.all].flatten
This provides a Supervisor with id=0 to allow searching for "no supervisors", id=0 is turned into nil by the ransacker
ActiveAdmin custom filter . Order Issue
No point in putting different groups (scopes) into an array and then sort it - just operate on AdminUser
:
filter :admin_user,
as: :select,
collection: -> AdminUser.order('name asc').pluck(:name)
If, though, you only need to have specific groups/scopes of AdminUser
(not all of them), create a new scope:
scope :for_filtering, -> {
exec_backoffice
.merge(exec_hotline)
.merge(exec_customer_relations)
}
And then use it:
filter :admin_user,
as: :select,
collection: -> AdminUser.for_filtering.order('name asc').pluck(:name)
Related Topics
Jekyll Plugin Not Work on Github
Error: Error Installing JSON: Error: Failed to Build Gem Native Extension
Generate Full Url to JavaScript in Rails (Similar to JavaScript_Path, But Url)
Class VS Class.New, Module VS Module.New
Activerecord Is Not Aware of Timezones
Ssl_Connect Syscall Returned=5 Errno=0 State=Sslv3 Read Server Hello a (Openssl::Ssl::Sslerror)
Active Resource Complaining About Expects an Hash
Ruby on Rails - Add Condition on ':Include =>' to Load Limited Number of Objects
Is /Etc/Irbrc Installed by Os X? Does Irb Read It
Ruby Code for Modifying Outer Quotes on Strings
Official Expansion of ||= Conditional Assignment Operator
Reverse a String Each Two Characters with Ruby
Uploading New Products with Multiple Variant Options
How to Build a Docker Image for a Ruby Project Without Build Tools