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

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

View File

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

View File

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

View File

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

View File

@ -31,7 +31,7 @@ x edit my site
x upload many files at once x upload many files at once
x import/export x import/export
x export x export
- site picker x site picker
- content types - content types
- change in main menu - change in main menu
- manage custom_fields - manage custom_fields
@ -42,7 +42,7 @@ x edit my site
- list - list
- crud - 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 - message to notify people if their browser is too old
- install a site by default at the first installation (without asking) - install a site by default at the first installation (without asking)