order direction for content types

This commit is contained in:
did 2011-03-10 00:54:38 +01:00
parent 2ba0718647
commit f38e6a8514
6 changed files with 36 additions and 3 deletions

View File

@ -1,7 +1,7 @@
module Admin::CustomFieldsHelper module Admin::CustomFieldsHelper
def options_for_field_kind def options_for_field_kind
options = %w{string text category boolean date file}.map do |kind| %w(string text category boolean date file).map do |kind|
[t("custom_fields.kind.#{kind}"), kind] [t("custom_fields.kind.#{kind}"), kind]
end end
end end
@ -13,6 +13,12 @@ module Admin::CustomFieldsHelper
options + options_for_highlighted_field(content_type, collection_name) options + options_for_highlighted_field(content_type, collection_name)
end end
def options_for_order_direction
%w(asc desc).map do |direction|
[t("admin.content_types.form.order_direction.#{direction}"), direction]
end
end
def options_for_highlighted_field(content_type, collection_name) def options_for_highlighted_field(content_type, collection_name)
custom_fields_collection_name = "ordered_#{collection_name.singularize}_custom_fields".to_sym custom_fields_collection_name = "ordered_#{collection_name.singularize}_custom_fields".to_sym
collection = content_type.send(custom_fields_collection_name) collection = content_type.send(custom_fields_collection_name)
@ -28,7 +34,7 @@ module Admin::CustomFieldsHelper
end end
def options_for_text_formatting def options_for_text_formatting
options = %w{none html}.map do |option| options = %w(none html).map do |option|
[t("admin.custom_fields.text_formatting.#{option}"), option] [t("admin.custom_fields.text_formatting.#{option}"), option]
end end
end end

View File

@ -7,6 +7,7 @@ class ContentType
field :description field :description
field :slug field :slug
field :order_by field :order_by
field :order_direction, :default => 'asc'
field :highlighted_field_name field :highlighted_field_name
field :group_by_field_name field :group_by_field_name
field :api_enabled, :type => Boolean, :default => false field :api_enabled, :type => Boolean, :default => false
@ -46,6 +47,14 @@ class ContentType
self.group_by_field && group_by_field.category? self.group_by_field && group_by_field.category?
end end
def order_manually?
self.order_by == '_position_in_list'
end
def asc_order?
self.order_direction.blank? || self.order_direction == 'asc'
end
def list_or_group_contents def list_or_group_contents
if self.groupable? if self.groupable?
groups = self.contents.klass.send(:"group_by_#{self.group_by_field._alias}", :ordered_contents) groups = self.contents.klass.send(:"group_by_#{self.group_by_field._alias}", :ordered_contents)
@ -65,7 +74,7 @@ class ContentType
def ordered_contents(conditions = {}) def ordered_contents(conditions = {})
column = self.order_by.to_sym column = self.order_by.to_sym
(if conditions.nil? || conditions.empty? list = (if conditions.nil? || conditions.empty?
self.contents self.contents
else else
conditions_with_names = {} conditions_with_names = {}
@ -79,6 +88,10 @@ class ContentType
self.contents.where(conditions_with_names) self.contents.where(conditions_with_names)
end).sort { |a, b| (a.send(column) || 0) <=> (b.send(column) || 0) } end).sort { |a, b| (a.send(column) || 0) <=> (b.send(column) || 0) }
return list if self.order_manually?
self.asc_order? ? list : list.reverse
end end
def sort_contents!(order) def sort_contents!(order)

View File

@ -16,6 +16,7 @@
= f.foldable_inputs :name => :options, :class => 'switchable' do = f.foldable_inputs :name => :options, :class => 'switchable' do
= f.input :order_by, :as => :select, :collection => options_for_order_by(f.object, 'contents'), :include_blank => false = f.input :order_by, :as => :select, :collection => options_for_order_by(f.object, 'contents'), :include_blank => false
= f.input :order_direction, :as => :select, :collection => options_for_order_direction, :include_blank => false, :wrapper_html => { :style => "#{'display: none' if f.object.order_manually?}" }
= f.custom_input :api_enabled, :css => 'toggle' do = f.custom_input :api_enabled, :css => 'toggle' do
= f.check_box :api_enabled = f.check_box :api_enabled
= hidden_field_tag 'content_type[api_accounts][]', '' = hidden_field_tag 'content_type[api_accounts][]', ''

View File

@ -227,6 +227,9 @@ en:
created_at: 'By "created at" date' created_at: 'By "created at" date'
updated_at: 'By "updated at" date' updated_at: 'By "updated at" date'
position_in_list: Manually position_in_list: Manually
order_direction:
asc: Ascending
desc: Descending
contents: contents:
index: index:

View File

@ -226,6 +226,9 @@ fr:
created_at: 'Par date création' created_at: 'Par date création'
updated_at: 'Par date de mise à jour' updated_at: 'Par date de mise à jour'
position_in_list: Manuellement position_in_list: Manuellement
order_direction:
asc: Ascendant
desc: Descendant
contents: contents:
index: index:

View File

@ -14,6 +14,13 @@ $(document).ready(function() {
$('#content_type_slug').keypress(function() { $(this).addClass('filled'); }); $('#content_type_slug').keypress(function() { $(this).addClass('filled'); });
$('#content_type_order_by').change(function() {
if ($(this).val() != '_position_in_list')
$('#content_type_order_direction_input').show();
else
$('#content_type_order_direction_input').hide();
});
// api enabled ? // api enabled ?
// console.log('subscribing...'); // console.log('subscribing...');