From edc09a840177553e669e3e82cf51c399cbd9c23c Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Sun, 22 May 2011 23:34:11 +1200 Subject: [PATCH] Removed Page#[] for Page#content_for() and refactored internals of page. Documented some changes so far. --- changelog.md | 6 +++ core/lib/refinery/helpers/image_helper.rb | 2 +- .../1 - Getting Started with Refinery.textile | 6 +-- .../1 - How to change Page Parts.textile | 2 +- doc/images.md | 2 +- pages/app/models/refinery/page.rb | 53 +++++++++---------- .../admin/pages/_page_part_field.html.erb | 6 +-- .../refinery/pages/admin/instance_methods.rb | 4 +- 8 files changed, 41 insertions(+), 40 deletions(-) diff --git a/changelog.md b/changelog.md index 184d1341ed..fdd44eb46e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,9 @@ +## 1.1.0 [unreleased] + +* Finally removed `Page#[]` in favour of `Page#content_for` so instead of `@page[:body]` it's `@page.content_for(:body)`. [Philip Arndt](https://github.com/parndt) +* Migrated to [Kaminari](https://github.com/amatsuda/kaminari) for pagination. [Uģis Ozols](https://github.com/ugisozols) +* Moved everything under Refinery namespace. [wakeless](https://github.com/wakeless) + ## 0.9.9.22 [22 May 2011] * Fixed issue introduced with `rake 0.9.0`. [Philip Arndt](https://github.com/parndt) diff --git a/core/lib/refinery/helpers/image_helper.rb b/core/lib/refinery/helpers/image_helper.rb index b7af4f8be6..204c4bd463 100644 --- a/core/lib/refinery/helpers/image_helper.rb +++ b/core/lib/refinery/helpers/image_helper.rb @@ -3,7 +3,7 @@ module Helpers module ImageHelper # replace all system images with a thumbnail version of them (handy for all images inside a page part) - # for example, <%= content_fu(@page[:body], '96x96#c') %> converts all /system/images to a 96x96 cropped thumbnail + # for example, <%= content_fu(@page.content_for(:body), '96x96#c') %> converts all /system/images to a 96x96 cropped thumbnail def content_fu(content, thumbnail) content.gsub(%r{}) do |image_match| begin diff --git a/doc/guides/1 - Getting Started/1 - Getting Started with Refinery.textile b/doc/guides/1 - Getting Started/1 - Getting Started with Refinery.textile index 8f713bcc03..31aba3d128 100644 --- a/doc/guides/1 - Getting Started/1 - Getting Started with Refinery.textile +++ b/doc/guides/1 - Getting Started/1 - Getting Started with Refinery.textile @@ -205,10 +205,10 @@ Replace the contents of +app/views/pages/show.html.erb+ with this:
- <%=raw @page[:body] %> + <%=raw @page.content_for(:body) %>
- <%=raw @page[:side_body] %> + <%=raw @page.content_for(:side_body) %>
@@ -223,7 +223,7 @@ When you edit the about page you'll see something like this: You'll notice two tabs on the page "Body" and "Side Body". These are +Page Parts+, or in other words a single piece of content attached to this page that you can render in your view. There is a "Body" tab with some content on this screen, to render that same content in your view you put: -<%=raw @page[:body] %> +<%=raw @page.content_for(:body) %> h4. Styling your views diff --git a/doc/guides/4 - Customising your Design/1 - How to change Page Parts.textile b/doc/guides/4 - Customising your Design/1 - How to change Page Parts.textile index 6128228431..91a46892b0 100644 --- a/doc/guides/4 - Customising your Design/1 - How to change Page Parts.textile +++ b/doc/guides/4 - Customising your Design/1 - How to change Page Parts.textile @@ -47,7 +47,7 @@ NOTE: Your "middle body" content won't show on the front end yet. You need to ou Now in your +app/views/pages/home.html.erb+ view you'll be able to put: -<%= @page[:middle_body] %> +<%= @page.content_for(:middle_body) %> to output this new content area in the view. diff --git a/doc/images.md b/doc/images.md index 1b55a9255f..b3e1379e2c 100644 --- a/doc/images.md +++ b/doc/images.md @@ -24,7 +24,7 @@ If I wanted to replace all the images inside a content section without the user having to resize images in the editor then I would use the built in ``content_fu`` command like this in my view: - <%= content_fu @page[:body], '400x300' %> + <%= content_fu @page.content_for(:body), '400x300' %> ``content_fu`` is a command we have created that automatically changes all images with the url /system/images to use a particular size. diff --git a/pages/app/models/refinery/page.rb b/pages/app/models/refinery/page.rb index 1c12a3efcc..03a3c3d657 100644 --- a/pages/app/models/refinery/page.rb +++ b/pages/app/models/refinery/page.rb @@ -7,13 +7,12 @@ class Page < ActiveRecord::Base translates :title, :custom_title, :meta_keywords, :meta_description, :browser_title, :include => :seo_meta # Set up support for meta tags through translations. - if defined?(::Page::Translation) + if self.respond_to?(:translation_class) attr_accessible :title # set allowed attributes for mass assignment - ::Page::Translation.send :attr_accessible, :browser_title, :meta_description, - :meta_keywords, :locale + self.translation_class.send :attr_accessible, :browser_title, :meta_description, :meta_keywords, :locale - if ::Page::Translation.table_exists? + if self.translation_class.table_exists? def translation if @translation.nil? or @translation.try(:locale) != ::Globalize.locale @translation = translations.with_locale(::Globalize.locale).first @@ -23,8 +22,8 @@ def translation @translation end - # Instruct the Translation model to have meta tags. - ::Page::Translation.send :is_seo_meta + # Instruct the translation_class model to have meta tags. + self.translation_class.send :is_seo_meta fields = ::SeoMeta.attributes.keys.reject{|f| self.column_names.map(&:to_sym).include?(f) @@ -32,6 +31,17 @@ def translation delegate *(fields << {:to => :translation}) after_save proc {|m| m.translation.save} end + + # Wrap up the logic of finding the pages based on the translations table. + def self.with_globalize(conditions = {}) + conditions = {:locale => Globalize.locale}.merge(conditions) + where(:id => translation_class.where(conditions).select('page_id AS id')).includes(:children, :slugs) + end + else + # No translations, just default to normal behaviour. + def self.with_globalize(conditions = {}) + where(conditions).includes(:children, :slugs) + end end before_create :ensure_locale, :if => proc { |c| @@ -59,11 +69,11 @@ def translation :strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => "pages") has_many :parts, - :class_name => "PagePart", + :class_name => "::Refinery::PagePart", :order => "position ASC", :inverse_of => :page, :dependent => :destroy, - :include => ((:translations) if defined?(::PagePart::Translation)) + :include => ((:translations) if ::Refinery::PagePart.respond_to?(:translation_class)) accepts_nested_attributes_for :parts, :allow_destroy => true @@ -75,18 +85,6 @@ def translation after_save :reposition_parts!, :invalidate_child_cached_url, :expire_page_caching after_destroy :expire_page_caching - # Wrap up the logic of finding the pages based on the translations table. - if defined?(::Page::Translation) - def self.with_globalize(conditions = {}) - conditions = {:locale => Globalize.locale}.merge(conditions) - where(:id => ::Page::Translation.where(conditions).select('page_id AS id')).includes(:children, :slugs) - end - else - def self.with_globalize(conditions = {}) - where(conditions).includes(:children, :slugs) - end - end - scope :live, where(:draft => false) scope :by_title, proc {|t| with_globalize(:title => t)} @@ -197,7 +195,7 @@ def url_marketable end def url_normal - {:controller => '/pages', :action => 'show', :path => nil, :id => to_param} + {:controller => '/refinery/pages', :action => 'show', :path => nil, :id => to_param} end def with_locale_param(url_hash) @@ -301,13 +299,10 @@ def expire_page_caching # Accessor method to get a page part from a page. # Example: # - # Page.first[:body] + # Page.first.content_for(:body) # # Will return the body page part of the first page. - def [](part_title) - # Allow for calling attributes with [] shorthand (eg page[:parent_id]) - return super if self.attributes.has_key?(part_title.to_s) - + def content_for(part_title) # the way that we call page parts seems flawed, will probably revert to page.parts[:title] in a future release. # self.parts is already eager loaded so we can now just grab the first element matching the title we specified. part = self.parts.detect do |part| @@ -322,13 +317,13 @@ def [](part_title) # In the admin area we use a slightly different title to inform the which pages are draft or hidden pages def title_with_meta title = if self.title.nil? - [::Page::Translation.where(:page_id => self.id, :locale => Globalize.locale).first.try(:title).to_s] + [self.class.with_globalize(:page_id => self.id).first.try(:title).to_s] else [self.title.to_s] end - title << "(#{::I18n.t('hidden', :scope => 'admin.pages.page')})" unless show_in_menu? - title << "(#{::I18n.t('draft', :scope => 'admin.pages.page')})" if draft? + title << "(#{::I18n.t('hidden', :scope => 'refinery.admin.pages.page')})" unless show_in_menu? + title << "(#{::I18n.t('draft', :scope => 'refinery.admin.pages.page')})" if draft? title.join(' ') end diff --git a/pages/app/views/refinery/admin/pages/_page_part_field.html.erb b/pages/app/views/refinery/admin/pages/_page_part_field.html.erb index d19ab8d95e..1ac3e6ec71 100644 --- a/pages/app/views/refinery/admin/pages/_page_part_field.html.erb +++ b/pages/app/views/refinery/admin/pages/_page_part_field.html.erb @@ -1,5 +1,5 @@
- <%= hidden_field_tag "page[parts_attributes][#{part_index}][title]", part.title if new_part %> - <%= text_area_tag "page[parts_attributes][#{part_index}][body]", part.body, :rows => 20, :class => 'wymeditor widest' %> - <%= hidden_field_tag "page[parts_attributes][#{part_index}][position]", part_index if new_part %> + <%= hidden_field_tag "page[parts_attributes)[#{part_index}][title]", part.title if new_part %> + <%= text_area_tag "page[parts_attributes)[#{part_index}][body]", part.body, :rows => 20, :class => 'wymeditor widest' %> + <%= hidden_field_tag "page[parts_attributes)[#{part_index}][position]", part_index if new_part %>
diff --git a/pages/lib/refinery/pages/admin/instance_methods.rb b/pages/lib/refinery/pages/admin/instance_methods.rb index 012f46e2df..7280197cbc 100644 --- a/pages/lib/refinery/pages/admin/instance_methods.rb +++ b/pages/lib/refinery/pages/admin/instance_methods.rb @@ -8,8 +8,8 @@ def error_404(exception=nil) params[:action] = 'error_404' # change any links in the copy to the refinery_admin_root_path # and any references to "home page" to "Dashboard" - part_symbol = Page.default_parts.first.to_sym - @page[part_symbol] = @page[part_symbol].to_s.gsub( + part_symbol = ::Refinery::Page.default_parts.first.to_sym + @page.content_for(part_symbol) = @page.content_for(part_symbol).to_s.gsub( /href=(\'|\")\/(\'|\")/, "href='#{refinery_admin_root_path}'" ).gsub("home page", "Dashboard")