first attempt to refactor the content types

This commit is contained in:
did 2011-12-16 13:02:10 +01:00
parent e500a1985b
commit 592a110fe5
6 changed files with 148 additions and 136 deletions

View File

@ -34,8 +34,8 @@ class Locomotive.Views.ApplicationView extends Backbone.View
timer = null
link = $(@)
(popup = link.find('.popup')).removeClass('popup').addClass('submenu-popup').css(
top: link.offset().top + link.height() - 2
left: link.offset().left - parseInt(popup.css('padding-left'))
top: link.offset().top + link.height() - 2
left: link.offset().left - parseInt(popup.css('padding-left'))
).bind('show', ->
link.find('a').addClass('hover') & popup.show()
).bind('hide', ->

View File

@ -1,89 +1,89 @@
module Locomotive::ContentTypesHelper
MAX_DISPLAYED_CONTENTS = 4
def fetch_content_types
return @content_types if @content_types
@content_types = current_site.content_types.ordered.
limit(:contents => Locomotive.config.lastest_items_nb).
only(:site_id, :name, :slug, :highlighted_field_name, :contents_custom_fields_version, :order_by, :serialized_item_template, :raw_item_template).to_a
if @content_type && @content_type.persisted? && @content_types.index(@content_type) >= MAX_DISPLAYED_CONTENTS
@content_types.delete(@content_type)
@content_types.insert(0, @content_type)
end
# be sure, we've got the custom klass up-to-date, otherwise it will fail miserably
@content_types.each do |content_type|
if content_type.content_klass_out_of_date?
content_type.reload
content_type.invalidate_content_klass
end
end
@content_types
end
def each_content_type_menu_item(which = :first, &block)
types = fetch_content_types
sliced = []
if which == :first
sliced = types[0..MAX_DISPLAYED_CONTENTS - 1]
elsif types.size > MAX_DISPLAYED_CONTENTS
sliced = types[MAX_DISPLAYED_CONTENTS, types.size - MAX_DISPLAYED_CONTENTS]
end
return [] if sliced.empty?
sliced.each do |content_type|
next if content_type.new_record?
item_on = (content_type.slug == @content_type.slug) rescue nil
label = truncate(content_type.name, :length => 15)
url = contents_url(content_type.slug)
css = @content_type && content_type.slug == @content_type.slug ? 'on' : ''
html = submenu_entry(label, url, :i18n => false, :css => css) do
yield(content_type)
end
haml_concat(html)
end
end
def other_content_types(&block)
types = fetch_content_types
if types.size > MAX_DISPLAYED_CONTENTS
sliced = types[MAX_DISPLAYED_CONTENTS, types.size - MAX_DISPLAYED_CONTENTS]
html = submenu_entry('...', '#', :i18n => false) do
yield(sliced)
end
haml_concat(html)
end
end
def content_label_for(content)
if content._parent.raw_item_template.blank?
content._label # default one
else
assigns = {
'site' => current_site,
'content' => content.to_liquid
}
registers = {
:controller => self,
:site => current_site,
:current_locomotive_account => current_locomotive_account
}
preserve(content._parent.item_template.render(::Liquid::Context.new({}, assigns, registers)))
end
end
# MAX_DISPLAYED_CONTENTS = 4
#
# def fetch_content_types
# return @content_types if @content_types
#
# @content_types = current_site.content_types.ordered.
# limit(:contents => Locomotive.config.lastest_items_nb).
# only(:site_id, :name, :slug, :highlighted_field_name, :contents_custom_fields_version, :order_by, :serialized_item_template, :raw_item_template).to_a
#
# if @content_type && @content_type.persisted? && @content_types.index(@content_type) >= MAX_DISPLAYED_CONTENTS
# @content_types.delete(@content_type)
# @content_types.insert(0, @content_type)
# end
#
# # be sure, we've got the custom klass up-to-date, otherwise it will fail miserably
# @content_types.each do |content_type|
# if content_type.content_klass_out_of_date?
# content_type.reload
# content_type.invalidate_content_klass
# end
# end
#
# @content_types
# end
#
# def each_content_type_menu_item(which = :first, &block)
# types = fetch_content_types
# sliced = []
#
# if which == :first
# sliced = types[0..MAX_DISPLAYED_CONTENTS - 1]
# elsif types.size > MAX_DISPLAYED_CONTENTS
# sliced = types[MAX_DISPLAYED_CONTENTS, types.size - MAX_DISPLAYED_CONTENTS]
# end
#
# return [] if sliced.empty?
#
# sliced.each do |content_type|
# next if content_type.new_record?
# item_on = (content_type.slug == @content_type.slug) rescue nil
#
# label = truncate(content_type.name, :length => 15)
# url = contents_url(content_type.slug)
# css = @content_type && content_type.slug == @content_type.slug ? 'on' : ''
#
# html = submenu_entry(label, url, :i18n => false, :css => css) do
# yield(content_type)
# end
#
# haml_concat(html)
# end
# end
#
# def other_content_types(&block)
# types = fetch_content_types
#
# if types.size > MAX_DISPLAYED_CONTENTS
# sliced = types[MAX_DISPLAYED_CONTENTS, types.size - MAX_DISPLAYED_CONTENTS]
#
# html = submenu_entry('...', '#', :i18n => false) do
# yield(sliced)
# end
#
# haml_concat(html)
# end
# end
#
# def content_label_for(content)
# if content._parent.raw_item_template.blank?
# content._label # default one
# else
# assigns = {
# 'site' => current_site,
# 'content' => content.to_liquid
# }
#
# registers = {
# :controller => self,
# :site => current_site,
# :current_locomotive_account => current_locomotive_account
# }
#
# preserve(content._parent.item_template.render(::Liquid::Context.new({}, assigns, registers)))
# end
# end
end

View File

@ -17,7 +17,7 @@ module Locomotive
mount_uploader :source, ContentAssetUploader, :mount_on => :source_filename
## associations ##
referenced_in :site, :class_name => 'Locomotive::Site'
belongs_to :site, :class_name => 'Locomotive::Site'
## validations ##
validates_presence_of :source

View File

@ -9,38 +9,43 @@ module Locomotive
include Extensions::Shared::Seo
## fields (dynamic fields) ##
field :_highlighted_field
field :_slug
field :_position_in_list, :type => Integer, :default => 0
field :_visible, :type => Boolean, :default => true
field :_visible, :type => Boolean, :default => true
## validations ##
validate :require_highlighted_field
validate :validate_uniqueness_of_slug
validates_presence_of :_slug
validate :require_highlighted_field
# validate :validate_uniqueness_of_slug
validates_uniqueness_of :_slug, :scope => [:content_type_id]
validates_presence_of :_slug
## associations ##
embedded_in :content_type, :class_name => 'Locomotive::ContentType', :inverse_of => :contents
belongs_to :site
belongs_to :content_type, :class_name => 'Locomotive::ContentType', :inverse_of => :contents
# embedded_in :content_type, :class_name => 'Locomotive::ContentType', :inverse_of => :contents
## callbacks ##
before_validation :set_slug
before_save :set_visibility
before_create :add_to_list_bottom
after_create :send_notifications
before_save :set_visibility
before_create :add_to_list_bottom
after_create :send_notifications
## named scopes ##
scope :visible, :where => { :_visible => true }
scope :latest_updated, :order_by => :updated_at.desc, :limit => Locomotive.config.lastest_items_nb
## methods ##
delegate :site, :to => :content_type
# delegate :site, :to => :content_type
alias :visible? :_visible?
alias :_permalink :_slug
alias :_permalink= :_slug=
def site_id # needed by the uploader of custom fields
self.content_type.site_id
end
# def site_id # needed by the uploader of custom fields
# self.content_type.site_id
# end
def highlighted_field_value
self.send(self.content_type.highlighted_field_name)
@ -52,25 +57,25 @@ module Locomotive
self._visible || self._visible.nil?
end
def next
content_type.contents.where(:_position_in_list => _position_in_list + 1).first()
def next # TODO
# content_type.contents.where(:_position_in_list => _position_in_list + 1).first()
end
def previous
content_type.contents.where(:_position_in_list => _position_in_list - 1).first()
def previous # TODO
# content_type.contents.where(:_position_in_list => _position_in_list - 1).first()
end
def errors_to_hash
Hash.new.replace(self.errors)
end
# def errors_to_hash # TODO
# Hash.new.replace(self.errors)
# end
def reload_parent!
self.class.reload_parent!
end
def self.reload_parent!
self._parent = self._parent.reload
end
# def reload_parent! # TODO
# self.class.reload_parent!
# end
#
# def self.reload_parent! # TODO
# self._parent = self._parent.reload
# end
def to_liquid
Locomotive::Liquid::Drops::Content.new(self)
@ -84,8 +89,14 @@ module Locomotive
end
def set_visibility
field = self.content_type.contents_custom_fields.detect { |f| %w{visible active}.include?(f._alias) }
self._visible = self.send(field._name) rescue true
%w(visible active).map(&:to_sym).each do |_alias|
if self.methods.include?(_alias)
self._visible = self.send(_alias)
return
end
end
# field = self.content_type.contents_custom_fields.detect { |f| %w{visible active}.include?(f._alias) }
# self._visible = self.send(field._name) rescue true
end
def add_to_list_bottom
@ -99,11 +110,11 @@ module Locomotive
end
end
def validate_uniqueness_of_slug
if self._parent.contents.any? { |c| c._id != self._id && c._slug == self._slug }
self.errors.add(:_slug, :taken)
end
end
# def validate_uniqueness_of_slug
# if self._parent.contents.any? { |c| c._id != self._id && c._slug == self._slug }
# self.errors.add(:_slug, :taken)
# end
# end
def highlighted_field_alias
self.content_type.highlighted_field._alias.to_sym

View File

@ -18,12 +18,13 @@ module Locomotive
field :api_accounts, :type => Array
## associations ##
referenced_in :site, :class_name => 'Locomotive::Site'
embeds_many :contents, :class_name => 'Locomotive::ContentInstance', :validate => false do
def visible
@target.find_all { |c| c.visible? }
end
end
belongs_to :site, :class_name => 'Locomotive::Site'
has_many :contents
# embeds_many :contents, :class_name => 'Locomotive::ContentInstance', :validate => false do
# def visible
# @target.find_all { |c| c.visible? }
# end
# end
## named scopes ##
scope :ordered, :order_by => :updated_at.desc
@ -135,13 +136,13 @@ module Locomotive
self.slug.permalink! if self.slug.present?
end
def remove_uploaded_files # callbacks are not called on each content so we do it manually
self.contents.each do |content|
self.contents_custom_fields.each do |field|
content.send(:"remove_#{field._name}!") if field.kind == 'file'
end
end
end
# def remove_uploaded_files # callbacks are not called on each content so we do it manually
# self.contents.each do |content|
# self.contents_custom_fields.each do |field|
# content.send(:"remove_#{field._name}!") if field.kind == 'file'
# end
# end
# end
end
end

View File

@ -31,7 +31,7 @@ x edit my site
x upload many files at once
x import/export
x export
- site picker
x site picker
- content types
- change in main menu
- manage custom_fields
@ -42,7 +42,7 @@ x edit my site
- list
- crud
- disallow to click twice on the submit form button
- disallow to click twice on the submit form button (spinner ?)
- message to notify people if their browser is too old
- install a site by default at the first installation (without asking)