From f38e6a85142b1db53d9a6341e9f24345beb4e86c Mon Sep 17 00:00:00 2001 From: did Date: Thu, 10 Mar 2011 00:54:38 +0100 Subject: [PATCH] order direction for content types --- app/helpers/admin/custom_fields_helper.rb | 10 ++++++++-- app/models/content_type.rb | 15 ++++++++++++++- app/views/admin/content_types/_form.html.haml | 1 + config/locales/admin_ui.en.yml | 3 +++ config/locales/admin_ui.fr.yml | 3 +++ public/javascripts/admin/content_types.js | 7 +++++++ 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/helpers/admin/custom_fields_helper.rb b/app/helpers/admin/custom_fields_helper.rb index b8593deb..7766e21d 100644 --- a/app/helpers/admin/custom_fields_helper.rb +++ b/app/helpers/admin/custom_fields_helper.rb @@ -1,7 +1,7 @@ module Admin::CustomFieldsHelper 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] end end @@ -13,6 +13,12 @@ module Admin::CustomFieldsHelper options + options_for_highlighted_field(content_type, collection_name) 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) custom_fields_collection_name = "ordered_#{collection_name.singularize}_custom_fields".to_sym collection = content_type.send(custom_fields_collection_name) @@ -28,7 +34,7 @@ module Admin::CustomFieldsHelper end 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] end end diff --git a/app/models/content_type.rb b/app/models/content_type.rb index 7b9ff7c4..51bc4d80 100644 --- a/app/models/content_type.rb +++ b/app/models/content_type.rb @@ -7,6 +7,7 @@ class ContentType field :description field :slug field :order_by + field :order_direction, :default => 'asc' field :highlighted_field_name field :group_by_field_name field :api_enabled, :type => Boolean, :default => false @@ -46,6 +47,14 @@ class ContentType self.group_by_field && group_by_field.category? 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 if self.groupable? groups = self.contents.klass.send(:"group_by_#{self.group_by_field._alias}", :ordered_contents) @@ -65,7 +74,7 @@ class ContentType def ordered_contents(conditions = {}) column = self.order_by.to_sym - (if conditions.nil? || conditions.empty? + list = (if conditions.nil? || conditions.empty? self.contents else conditions_with_names = {} @@ -79,6 +88,10 @@ class ContentType self.contents.where(conditions_with_names) 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 def sort_contents!(order) diff --git a/app/views/admin/content_types/_form.html.haml b/app/views/admin/content_types/_form.html.haml index 8312292d..abedb391 100644 --- a/app/views/admin/content_types/_form.html.haml +++ b/app/views/admin/content_types/_form.html.haml @@ -16,6 +16,7 @@ = 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_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.check_box :api_enabled = hidden_field_tag 'content_type[api_accounts][]', '' diff --git a/config/locales/admin_ui.en.yml b/config/locales/admin_ui.en.yml index 643c06d4..41374986 100644 --- a/config/locales/admin_ui.en.yml +++ b/config/locales/admin_ui.en.yml @@ -227,6 +227,9 @@ en: created_at: 'By "created at" date' updated_at: 'By "updated at" date' position_in_list: Manually + order_direction: + asc: Ascending + desc: Descending contents: index: diff --git a/config/locales/admin_ui.fr.yml b/config/locales/admin_ui.fr.yml index 355ec9bf..7fd2f6d1 100644 --- a/config/locales/admin_ui.fr.yml +++ b/config/locales/admin_ui.fr.yml @@ -226,6 +226,9 @@ fr: created_at: 'Par date création' updated_at: 'Par date de mise à jour' position_in_list: Manuellement + order_direction: + asc: Ascendant + desc: Descendant contents: index: diff --git a/public/javascripts/admin/content_types.js b/public/javascripts/admin/content_types.js index 2d8f2564..5479e628 100644 --- a/public/javascripts/admin/content_types.js +++ b/public/javascripts/admin/content_types.js @@ -14,6 +14,13 @@ $(document).ready(function() { $('#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 ? // console.log('subscribing...');