group contents by category for the has_ony / has_many widgets

This commit is contained in:
did 2011-06-23 16:17:08 -07:00
parent 2cc07b3957
commit c9f3989c2b
4 changed files with 72 additions and 13 deletions

View File

@ -45,15 +45,43 @@ module Admin::CustomFieldsHelper
end.compact
end
def options_for_has_one(field)
target_contents_from_field(field).collect { |c| [c._label, c._id] }
def options_for_has_one(field, value)
self.options_for_has_one_or_has_many(field) do |groups|
grouped_options_for_select(groups.collect do |g|
if g[:items].empty?
nil
else
[g[:name], g[:items].collect { |c| [c._label, c._id] }]
end
end.compact, value)
end
end
alias :options_for_has_many :options_for_has_one
def options_for_has_many(field)
self.options_for_has_one_or_has_many(field)
end
def target_contents_from_field(field)
def options_for_has_one_or_has_many(field, &block)
content_type = field.target.constantize._parent
content_type.ordered_contents
if content_type.groupable?
grouped_contents = content_type.list_or_group_contents
if block_given?
block.call(grouped_contents)
else
grouped_contents.collect do |g|
if g[:items].empty?
nil
else
{ :name => g[:name], :items => g[:items].collect { |c| [c._label, c._id] } }
end
end.compact
end
else
contents = content_type.ordered_contents
contents.collect { |c| [c._label, c._id] }
end
end
end

View File

@ -1,4 +1,4 @@
- field.target.constantize.reload_parent! # to make sure all the contents from the parent are loaded
= form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'has_one' }, :as => :select, :collection => options_for_has_one(field), :selected => form.object.send(field._alias.to_sym).try(:_id)
= form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'has_one' }, :as => :select, :collection => options_for_has_one(field, form.object.send(field._alias.to_sym).try(:_id))

View File

@ -27,11 +27,12 @@ x issue #90: seo metadata
x issue #57: seo page title
x issue #56
x tweak ui: form, quick link to edit a model in the popup menu
- Has_one => group by in the select
- convert existing templates (the 2 of the themes section)
x Has_one => group by in the select
- better hints:
- notify the user that after changing the page title, they still have to click "update" for the change to be saved
- created_by ASC => "Creation date ascending"
- cancan: authors / designers
- convert existing templates (the 2 of the themes section)
- bug heroku: unable to upload a new file
BACKLOG:

View File

@ -7,12 +7,28 @@ $(document).ready(function() {
$.fn.hasManySelector = function(options) {
var populateSelect = function(context) {
context.select.find('option').remove();
context.select.find('optgroup, option').remove();
for (var i = 0; i < context.data.collection.length; i++) {
var obj = context.data.collection[i];
if ($.inArray(obj[1], context.data.taken_ids) == -1)
context.select.append(new Option(obj[0], obj[1], true, true));
if (typeof(obj.name) != 'undefined') { // grouped items
var optgroup = $('<optgroup/>').attr('label', obj.name);
var size = 0;
for (var j = 0; j < obj.items.length; j++) {
var innerObj = obj.items[j];
if ($.inArray(innerObj[1], context.data.taken_ids) == -1) {
optgroup.append(new Option(innerObj[0], innerObj[1], true, true));
size++;
}
}
if (size > 0) context.select.append(optgroup);
} else {
if ($.inArray(obj[1], context.data.taken_ids) == -1)
context.select.append(new Option(obj[0], obj[1], true, true));
}
}
if (context.select.find('option').size() == 0)
@ -146,8 +162,22 @@ $(document).ready(function() {
for (var j = 0; j < context.data.collection.length; j++) {
var current = context.data.collection[j];
if (data.id == current[1])
data.label = current[0];
if (typeof(current.name) == 'undefined') {
if (data.id == current[1]) {
data.label = current[0];
break;
}
} else { // loop thru the groups
for (var k = 0; k < current.items.length; k++) {
var localCurrent = current.items[k];
if (data.id == localCurrent[1]) {
data.label = localCurrent[0];
break;
}
}
}
}
addElement(context, data);