From fb148a31a4a007f9a6d68120565caf7ce72b6696 Mon Sep 17 00:00:00 2001 From: dinedine Date: Fri, 16 Jul 2010 22:36:07 +0200 Subject: [PATCH] templatized page + fix bug with nav widget --- app/models/content_instance.rb | 17 ++++++++- app/models/extensions/page/templatized.rb | 32 ++++++++++++++++ app/models/page.rb | 5 ++- app/views/admin/pages/_form.html.haml | 9 ++++- app/views/admin/pages/_page.html.haml | 2 +- config/locales/admin_ui_en.yml | 1 + config/locales/admin_ui_fr.yml | 1 + doc/TODO | 5 ++- lib/locomotive/liquid/drops/content.rb | 4 +- lib/locomotive/liquid/drops/page.rb | 10 ++++- lib/locomotive/liquid/tags/nav.rb | 6 ++- lib/locomotive/render.rb | 22 ++++++++++- public/images/admin/list/item-left.png | Bin 3388 -> 1654 bytes public/javascripts/admin/pages.js | 13 +++++++ public/javascripts/admin/plugins/toggle.js | 3 ++ public/stylesheets/admin/application.css | 7 +++- .../stylesheets/admin/formtastic_changes.css | 2 +- public/stylesheets/admin/page_parts.css | 2 +- spec/lib/locomotive/render_spec.rb | 35 +++++++++++++++--- spec/models/page_spec.rb | 22 +++++++++++ 20 files changed, 177 insertions(+), 21 deletions(-) create mode 100644 app/models/extensions/page/templatized.rb diff --git a/app/models/content_instance.rb b/app/models/content_instance.rb index 5f6a1a86..a3f5c41f 100644 --- a/app/models/content_instance.rb +++ b/app/models/content_instance.rb @@ -7,6 +7,7 @@ class ContentInstance include CustomFields::ProxyClassEnabler ## fields (dynamic fields) ## + field :_slug field :_position_in_list, :type => Integer, :default => 0 ## validations ## @@ -16,6 +17,7 @@ class ContentInstance embedded_in :content_type, :inverse_of => :contents ## callbacks ## + before_save :set_slug before_create :add_to_list_bottom ## named scopes ## @@ -29,16 +31,29 @@ class ContentInstance protected + def set_slug + _alias = self.highlighted_field_alias + self._slug = self.send(_alias).parameterize('_') + end + def add_to_list_bottom Rails.logger.debug "add_to_list_bottom" self._position_in_list = self.content_type.contents.size end def require_highlighted_field - _alias = self.content_type.highlighted_field._alias.to_sym + _alias = self.highlighted_field_alias if self.send(_alias).blank? self.errors.add(_alias, :blank) end end + def highlighted_field_value + self.send(self.content_type.highlighted_field._name) + end + + def highlighted_field_alias + self.content_type.highlighted_field._alias.to_sym + end + end \ No newline at end of file diff --git a/app/models/extensions/page/templatized.rb b/app/models/extensions/page/templatized.rb new file mode 100644 index 00000000..b5a181cf --- /dev/null +++ b/app/models/extensions/page/templatized.rb @@ -0,0 +1,32 @@ +module Models + module Extensions + module Page + module Templatized + + extend ActiveSupport::Concern + + included do + + belongs_to_related :content_type + + field :templatized, :type => Boolean, :default => false + + field :content_type_visible_column + + before_validate :set_slug_if_templatized + end + + module InstanceMethods + + def set_slug_if_templatized + self.slug = 'content_type_template' if self.templatized? + end + + end + + end + end + end +end + + \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index 53874105..8dced9d8 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -6,6 +6,7 @@ class Page include Models::Extensions::Page::Tree include Models::Extensions::Page::Parts include Models::Extensions::Page::Render + include Models::Extensions::Page::Templatized ## fields ## field :title @@ -31,8 +32,8 @@ class Page ## named scopes ## named_scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.lastest_items_nb - named_scope :index, :where => { :slug => 'index', :depth => 0, :published => true } - named_scope :not_found, :where => { :slug => '404', :depth => 0, :published => true } + named_scope :index, :where => { :slug => 'index', :depth => 0 } + named_scope :not_found, :where => { :slug => '404', :depth => 0 } named_scope :published, :where => { :published => true } ## behaviours ## diff --git a/app/views/admin/pages/_form.html.haml b/app/views/admin/pages/_form.html.haml index b1026410..61d364c2 100644 --- a/app/views/admin/pages/_form.html.haml +++ b/app/views/admin/pages/_form.html.haml @@ -11,8 +11,13 @@ - if not @page.index? and not @page.not_found? = f.input :parent_id, :as => :select, :collection => parent_pages_options, :include_blank => false - = f.input :slug, :required => false, :hint => @page.slug.blank? ? ' ' : @page.url, :input_html => { :data_url => get_path_admin_pages_url, :disabled => @page.index? || @page.not_found? } - + = f.input :slug, :required => false, :hint => @page.slug.blank? ? ' ' : @page.url, :input_html => { :data_url => get_path_admin_pages_url, :disabled => @page.index? || @page.not_found? }, :wrapper_html => { :style => "#{'display: none' if @page.templatized?}" } + + = f.custom_input :templatized, :css => 'toggle' do + = f.check_box :templatized + + = f.input :content_type_id, :as => :select, :collection => current_site.content_types.all.to_a, :include_blank => false, :wrapper_html => { :style => "#{'display: none' unless @page.templatized?}" } + = f.custom_input :published, :css => 'toggle' do = f.check_box :published diff --git a/app/views/admin/pages/_page.html.haml b/app/views/admin/pages/_page.html.haml index 0cfbcbe6..542a3140 100644 --- a/app/views/admin/pages/_page.html.haml +++ b/app/views/admin/pages/_page.html.haml @@ -1,4 +1,4 @@ -%li{ :id => "item-#{page.id}", :class => "#{'not-found' if page.not_found? }"} +%li{ :id => "item-#{page.id}", :class => "#{'not-found' if page.not_found? } #{'templatized' if page.templatized?}"} - if not page.index? and not page.children.empty? = image_tag 'admin/list/icons/node_closed.png', :class => 'toggler' %em diff --git a/config/locales/admin_ui_en.yml b/config/locales/admin_ui_en.yml index 9ce24b00..db6ab686 100644 --- a/config/locales/admin_ui_en.yml +++ b/config/locales/admin_ui_en.yml @@ -268,6 +268,7 @@ en: page: published: "Only authenticated accounts can view unpublished pages." cache_strategy: "Cache the page for better performance. The 'Simple' choice is a good compromise." + templatized: "Use the page as a template for a model you defined." snippet: slug: "You need to know it in order to insert the snippet inside a page or a layout" site: diff --git a/config/locales/admin_ui_fr.yml b/config/locales/admin_ui_fr.yml index a3cf735a..99378305 100644 --- a/config/locales/admin_ui_fr.yml +++ b/config/locales/admin_ui_fr.yml @@ -290,6 +290,7 @@ fr: page: published: "Seuls les administrateurs authentifiés peuvent voir une page non publiée." cache_strategy: "Cache la page pour de meilleure performance. L'option 'Simple' est le meilleur compromis." + templatized: "Utilise la page comme un template pour un modèle défini." snippet: slug: "Utilisé pour insérer le snippet dans une page ou un gabarit." site: diff --git a/doc/TODO b/doc/TODO index 487ebfd3..61432b7c 100644 --- a/doc/TODO +++ b/doc/TODO @@ -2,7 +2,7 @@ BOARD: - refactor slugify method (use parameterize + create a module) - send email when new content added thru api -- page templatized (bound to a model) +- shortcut for jquery include tag BACKLOG: @@ -61,4 +61,5 @@ x refactoring admin crud (pages + layouts + snippets) x flash messages in French x save layout / snippet / page / stylesheet / javascript with CMD + S (ajax) x change action icons according to the right action [Sacha] -x publish event when saving form in ajax (for instance, in order to update account name or site name) \ No newline at end of file +x publish event when saving form in ajax (for instance, in order to update account name or site name) +x page templatized (bound to a model) \ No newline at end of file diff --git a/lib/locomotive/liquid/drops/content.rb b/lib/locomotive/liquid/drops/content.rb index 687ead20..7c958fde 100644 --- a/lib/locomotive/liquid/drops/content.rb +++ b/lib/locomotive/liquid/drops/content.rb @@ -11,7 +11,9 @@ module Locomotive end end - + def highlighted_field_value + @source.highlighted_field_value + end end end diff --git a/lib/locomotive/liquid/drops/page.rb b/lib/locomotive/liquid/drops/page.rb index 32c07462..db81dc4a 100644 --- a/lib/locomotive/liquid/drops/page.rb +++ b/lib/locomotive/liquid/drops/page.rb @@ -3,7 +3,15 @@ module Locomotive module Drops class Page < Base - liquid_attributes << :title << :slug + # liquid_attributes << :title << :slug + + def title + @source.templatized? ? @context['content_instance'].highlighted_field_value : @source.title + end + + def slug + @source.templatized? ? @source.content_type.slug.singularize : @source.slug + end def children @children ||= liquify(*@source.children) diff --git a/lib/locomotive/liquid/tags/nav.rb b/lib/locomotive/liquid/tags/nav.rb index e2fae74f..def3254d 100644 --- a/lib/locomotive/liquid/tags/nav.rb +++ b/lib/locomotive/liquid/tags/nav.rb @@ -29,10 +29,12 @@ module Locomotive source = context.registers[@site_or_page.to_sym] + puts "#{@site_or_page.to_sym} / source = #{source.inspect}" + if source.respond_to?(:name) # site ? - source = source.pages.first # start from home page + source = source.pages.index.first # start from home page else - source = source.parent + source = source.parent || source end output = %{