Created
July 19, 2011 19:30
-
-
Save schoren/1093488 to your computer and use it in GitHub Desktop.
Custom view for rails_admin to create related HABTM records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:ruby | |
config = field.associated_model_config | |
selected = field.bindings[:object].send(field.association[:name]) | |
selected_ids = selected.map(&:id) | |
xhr = field.associated_collection_count > 100 | |
if xhr | |
collection = selected.map do |o| | |
[o.send(config.object_label_method), o.id] | |
end | |
else | |
collection = field.associated_collection(@authorization_adapter).sort_by {|a| selected_ids.index(a[1]) || selected_ids.size } | |
end | |
= field_wrapper_for(form, field) do | |
%input{:name => field.dom_name, :type => "hidden", :value => ""} | |
= form.select field.method_name, collection, { :selected => selected_ids, :object => @object }, field.html_attributes | |
- head_style 'rails_admin/ra.filtering-multiselect.css' | |
- head_javascript "rails_admin/ra.filtering-multiselect.js" | |
- head_javascript "ms.remote-form.js" | |
- head_javascript do | |
$j(document).ready(function($){ | |
$("##{field.dom_id}").filteringMultiselect({ | |
- if xhr | |
createQuery: function(query) { | |
return { query: query } | |
}, | |
source: "#{rails_admin_list_path(config.abstract_model.to_param, :compact => true)}", | |
sortable: #{field.orderable ? 'true' : 'false'}, | |
regional: { | |
chooseAll: "#{t("admin.new.chose_all")}", | |
chosen: "#{t("admin.new.chosen", :name => config.abstract_model.model.model_name.human(:count => 2, :default => config.label.pluralize))}", | |
clearAll: "#{t("admin.new.clear_all")}", | |
selectChoice: "#{t("admin.new.select_choice")}", | |
up: "#{t("admin.new.up")}", | |
down: "#{t("admin.new.down")}" | |
} | |
}); | |
$(".#{field.dom_id} .hasAndBelongsToManyAssociation").after('#{link_to t('admin.actions.create').capitalize + ' ' + config.abstract_model.pretty_name.downcase, rails_admin_new_path(:model_name => config.abstract_model.to_param), :class => "createAssociatedRecord"}'); | |
$(".#{field.dom_id} .createAssociatedRecord").remoteForm({ dialogClass: "createAssociatedRecordDialog" }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.extend($.ra.remoteForm.prototype, { | |
_bindFormEvents: function() { | |
var dialog = this._getDialog(), | |
form = dialog.find("form"), | |
widget = this, | |
saveButtonText = dialog.find(":submit[name=_save]").text(), | |
cancelButtonText = dialog.find(":submit[name=_continue]").text(); | |
dialog.dialog("option", "title", $("h2.title", dialog).remove().text()); | |
form.attr("data-remote", true); | |
dialog.find(":submit").remove(); | |
dialog.find(".ra-block-content").removeClass("ra-block-content"); | |
var buttons = {}; | |
if (saveButtonText) { | |
buttons[saveButtonText] = function() { | |
// We need to manually update CKeditor mapped textarea before ajax submit | |
if(typeof CKEDITOR != 'undefined') { | |
for ( instance in CKEDITOR.instances ) | |
CKEDITOR.instances[instance].updateElement(); | |
} | |
dialog.find("form").submit(); | |
}; | |
} | |
if (cancelButtonText) { | |
buttons[cancelButtonText] = function() { | |
dialog.dialog("close"); | |
}; | |
} | |
dialog.dialog("option", "buttons", buttons); | |
/* Remove original button container if it's now empty */ | |
if (0 == $("form > .navform :submit", dialog).length) { | |
$("form > .navform", dialog).remove(); | |
} | |
form.bind("ajax:success", function(e, data, status, xhr) { | |
var json = $.parseJSON(data); | |
var select = widget.element.siblings('.ra-multiselect').find('.ra-multiselect-right select.ra-multiselect-selection'); | |
var input = widget.element.siblings('.hasAndBelongsToManyAssociation'); | |
if(select.length > 0) { | |
select.append('<option value="' + json.id + '">' + json.label + '</option>' ); | |
} | |
if(input.length > 0){ | |
input.append('<option value="' + json.id + '" selected="selected">' + json.label + '</option>' ); | |
} | |
dialog.dialog("close"); | |
}); | |
form.bind("ajax:error", function(e, xhr, status, error) { | |
dialog.html(xhr.responseText); | |
widget._bindFormEvents(); | |
}); | |
} | |
}); | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RailsAdmin.config do |config| | |
config.model Content do | |
edit do | |
field :tags, :has_and_belongs_to_many_association do | |
partial 'custom_multiselect' | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment