entries can be grouped by a belongs_to field in the back-office + add the show action for the content_entries API

This commit is contained in:
Didier Lafforgue 2012-03-07 01:39:24 +01:00
parent f77bdd9826
commit ac823aac66
7 changed files with 92 additions and 78 deletions

View File

@ -8,8 +8,8 @@ gemspec # Include gemspec dependencies
# The rest of the dependencies are for use when in the locomotive development environment
group :development do
# gem 'custom_fields', :path => '../gems/custom_fields' # for Developers
gem 'custom_fields', :git => 'git://github.com/locomotivecms/custom_fields.git', :branch => '2.0.0.rc' # Branch on Github
gem 'custom_fields', :path => '../gems/custom_fields' # for Developers
# gem 'custom_fields', :git => 'git://github.com/locomotivecms/custom_fields.git', :branch => '2.0.0.rc' # Branch on Github
gem 'rspec-rails', '~> 2.8.0' # In order to have rspec tasks and generators
gem 'rspec-cells'

View File

@ -15,16 +15,6 @@ GIT
fssm (>= 0.2.7)
sass (~> 3.1)
GIT
remote: git://github.com/locomotivecms/custom_fields.git
revision: a965fb3c76a1d689804770d04d510279cf75cf4c
branch: 2.0.0.rc
specs:
custom_fields (2.0.0.rc5)
activesupport (~> 3.2.1)
carrierwave-mongoid (~> 0.1.3)
mongoid (~> 2.4.3)
PATH
remote: .
specs:
@ -62,6 +52,14 @@ PATH
rmagick (~> 2.12.2)
sanitize (~> 2.0.3)
PATH
remote: ../gems/custom_fields
specs:
custom_fields (2.0.0.rc5)
activesupport (~> 3.2.1)
carrierwave-mongoid (~> 0.1.3)
mongoid (~> 2.4.5)
GEM
remote: http://rubygems.org/
specs:

View File

@ -568,9 +568,10 @@ form.formtastic {
}
&.label {
margin-left: 8px;
font-weight: bold;
color: #000;
margin-left: 8px;
font-weight: bold;
color: #000;
height: 31px;
}
} // ul .col

View File

@ -9,6 +9,11 @@ module Locomotive
respond_with @content_entries
end
def show
@content_entry = @content_type.entries.any_of({ :_id => params[:id] }, { :_slug => params[:id] }).first
respond_with @content_entry, :status => @content_entry ? :ok : :not_found
end
def create
@content_entry = @content_type.entries.create(params[:content_entry])
respond_with @content_entry, :location => main_app.locomotive_api_content_entries_url(@content_type.slug)

View File

@ -17,7 +17,7 @@ module Locomotive
def options_for_group_by_field(content_type)
content_type.ordered_entries_custom_fields.find_all do |field|
%w(select).include?(field.type)
%w(select belongs_to).include?(field.type)
end.map do |field|
[field.label, field._id]
end

View File

@ -59,7 +59,7 @@ module Locomotive
end
def groupable?
!!self.group_by_field && group_by_field.type == 'select'
!!self.group_by_field && %w(select belongs_to).include?(group_by_field.type)
end
def group_by_field
@ -68,7 +68,11 @@ module Locomotive
def list_or_group_entries
if self.groupable?
self.entries.group_by_select_option(self.group_by_field.name, self.order_by_definition)
if group_by_field.type == 'select'
self.entries.group_by_select_option(self.group_by_field.name, self.order_by_definition)
else
group_by_belongs_to_field(self.group_by_field)
end
else
self.ordered_entries
end
@ -110,6 +114,28 @@ module Locomotive
protected
def group_by_belongs_to_field(field)
grouped_entries = self.ordered_entries.group_by(&:"#{field.name}_id")
columns = grouped_entries.keys
target_content_type = self.class_name_to_content_type(field.class_name)
all_columns = target_content_type.ordered_entries
all_columns.map do |column|
if columns.include?(column._id)
{
:name => column._label(target_content_type),
:entries => grouped_entries.delete(column._id)
}
else
nil
end
end.compact.tap do |groups|
unless grouped_entries.empty? # "orphans" ?
groups << { :name => nil, :entries => grouped_entries.values.flatten }
end
end
end
def order_by_attribute
return self.order_by if %w(created_at updated_at _position).include?(self.order_by)
self.entries_custom_fields.find(self.order_by).name rescue 'created_at'
@ -179,63 +205,3 @@ module Locomotive
end
end
# def list_or_group_contents
# if self.groupable?
# groups = self.contents.klass.send(:"group_by_#{self.group_by_field._alias}", :ordered_contents)
#
# # look for items with no category or unknown ones
# items_without_category = self.contents.find_all { |c| !self.group_by_field.category_ids.include?(c.send(self.group_by_field_name)) }
# if not items_without_category.empty?
# groups << { :name => nil, :items => items_without_category }
# else
# groups
# end
# else
# self.ordered_contents
# end
# end
#
# def latest_updated_contents
# self.contents.latest_updated.reject { |c| !c.persisted? }
# end
#
# def ordered_contents(conditions = {})
# column = self.order_by.to_sym
#
# list = (if conditions.nil? || conditions.empty?
# self.contents
# else
# conditions_with_names = {}
#
# conditions.each do |key, value|
# # convert alias (key) to name
# field = self.entries_custom_fields.detect { |f| f._alias == key }
#
# case field.kind.to_sym
# when :category
# if (category_item = field.category_items.where(:name => value).first).present?
# conditions_with_names[field._name.to_sym] = category_item._id
# end
# else
# conditions_with_names[field._name.to_sym] = value
# end
# end
#
# self.contents.where(conditions_with_names)
# end).sort { |a, b| (a.send(column) && b.send(column)) ? (a.send(column) || 0) <=> (b.send(column) || 0) : 0 }
#
# return list if self.order_manually?
#
# self.asc_order? ? list : list.reverse
# end
#
# def sort_contents!(ids)
# ids.each_with_index do |id, position|
# self.contents.find(BSON::ObjectId(id))._position_in_list = position
# end
# self.save
# end
#
# def group_by_field
# @group_by_field ||= self.entries_custom_fields.detect { |f| f._name == self.group_by_field_name }
# end

View File

@ -102,6 +102,50 @@ describe Locomotive::ContentType do
end
context '#group_by belongs_to field' do
before(:each) do
(@category_content_type = build_content_type(:name => 'Categories')).save!
@category_1 = @category_content_type.entries.create :name => 'A-developer'
@category_2 = @category_content_type.entries.create :name => 'B-designer'
@content_type = build_content_type.tap do |content_type|
field = content_type.entries_custom_fields.build :label => 'Category', :type => 'belongs_to', :class_name => @category_1.class.to_s
content_type.group_by_field_id = field._id
content_type.save!
end
@content_1 = @content_type.entries.create :name => 'Sacha', :category => @category_2
@content_2 = @content_type.entries.create :name => 'Did', :category => @category_1
@content_3 = @content_type.entries.create :name => 'Mario', :category => @category_1
end
it 'groups entries' do
groups = @content_type.send(:group_by_belongs_to_field, @content_type.group_by_field)
groups.map { |h| h[:name] }.should == %w(A-developer B-designer)
groups.first[:entries].map(&:name).should == %w(Did Mario)
groups.last[:entries].map(&:name).should == %w(Sacha)
end
it 'groups entries with a different columns order' do
@category_content_type.update_attributes :order_by => @category_content_type.entries_custom_fields.first._id, :order_direction => 'desc'
groups = @content_type.send(:group_by_belongs_to_field, @content_type.group_by_field)
groups.map { |h| h[:name] }.should == %w(B-designer A-developer)
end
it 'deals with entries without a value for the group_by field (orphans)' do
@content_type.entries.create :name => 'Paul'
groups = @content_type.send(:group_by_belongs_to_field, @content_type.group_by_field)
groups.map { |h| h[:name] }.should == ['A-developer', 'B-designer', nil]
groups.last[:entries].map(&:name).should == %w(Paul)
end
end
describe 'custom fields' do
before(:each) do