Skip to content

Instantly share code, notes, and snippets.

@adis-io
Created December 24, 2015 11:16
Show Gist options
  • Save adis-io/764ff356bd78f436b0cf to your computer and use it in GitHub Desktop.
Save adis-io/764ff356bd78f436b0cf to your computer and use it in GitHub Desktop.
Chained select in ActiveAdmin
if ($('body').hasClass('admin_subscriptions')) {
$.post('/admin/users/1/subscriptions/get_subjects', function(data) {
$('body').data('subscription.subjects', data);
});
$("#subscription_subject_type").on("change", function () {
var $select_type = $("#subscription_subject_type"),
$select_id = $("#subscription_subject_id");
$("option[value]", $select_id).remove();
$.each($('body').data('subscription.subjects'), function() {
if (this.type == $select_type.val()) {
$select_id.append('<option value=' + this.id + '>' + this.name + '</option>');
}
});
});
}
ActiveAdmin.register Subscription do
belongs_to :user
navigation_menu :subscription
scope("Taxonomy") { |scope| scope.where(subject_type: 'Taxonomy') }
scope("Author") { |scope| scope.where(subject_type: 'Author') }
config.filters = false
collection_action :get_subjects, method: :post do
render json: subjects
end
form do |f|
f.semantic_errors
f.inputs do
f.input :subject_type, label: 'Type', as: :select2, placeholder: "Choose type", collection: self.controller.subject_types, input_html: { style: 'width: 300px;' }
f.input :subject_id, label: 'Subject', as: :select2, placeholder: "Choose subject", collection: [], input_html: { style: 'width: 300px;' }
end
f.actions
end
index do
selectable_column
column "ID", :id
column "Type", :subject_type
column "Name" do |resource|
resource.subject.name
end
column :created_at
end
controller do
def subjects
arr = []
arr = arr + Author.all.map do |subject|
{
type: 'Author',
id: subject.id,
name: subject.name
}
end
arr = arr + MuoCategory.all.map do |subject|
{
type: 'Category',
id: subject.id,
name: subject.name
}
end
arr = arr + MuoTag.all.map do |subject|
{
type: 'Tag',
id: subject.id,
name: subject.name
}
end
arr
end
def subject_types
['Author', 'Category', 'Tag']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment