Skip to content

Commit

Permalink
Removed Page#[] for Page#content_for() and refactored internals of pa…
Browse files Browse the repository at this point in the history
…ge. Documented some changes so far.
  • Loading branch information
parndt committed May 22, 2011
1 parent ed72ab3 commit edc09a8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 40 deletions.
6 changes: 6 additions & 0 deletions 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] ## 0.9.9.22 [22 May 2011]


* Fixed issue introduced with `rake 0.9.0`. [Philip Arndt](https://github.com/parndt) * Fixed issue introduced with `rake 0.9.0`. [Philip Arndt](https://github.com/parndt)
Expand Down
2 changes: 1 addition & 1 deletion core/lib/refinery/helpers/image_helper.rb
Expand Up @@ -3,7 +3,7 @@ module Helpers
module ImageHelper module ImageHelper


# replace all system images with a thumbnail version of them (handy for all images inside a page part) # 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) def content_fu(content, thumbnail)
content.gsub(%r{<img.+?src=['"](/system/images/.+?)/.+?/>}) do |image_match| content.gsub(%r{<img.+?src=['"](/system/images/.+?)/.+?/>}) do |image_match|
begin begin
Expand Down
Expand Up @@ -205,10 +205,10 @@ Replace the contents of +app/views/pages/show.html.erb+ with this:


<erb> <erb>
<section id='body_content'> <section id='body_content'>
<%=raw @page[:body] %> <%=raw @page.content_for(:body) %>
</section> </section>
<section id='side_body_content'> <section id='side_body_content'>
<%=raw @page[:side_body] %> <%=raw @page.content_for(:side_body) %>
</section> </section>
</erb> </erb>


Expand All @@ -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: 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:


<erb> <erb>
<%=raw @page[:body] %> <%=raw @page.content_for(:body) %>
</erb> </erb>


h4. Styling your views h4. Styling your views
Expand Down
Expand Up @@ -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: Now in your +app/views/pages/home.html.erb+ view you'll be able to put:


<erb> <erb>
<%= @page[:middle_body] %> <%= @page.content_for(:middle_body) %>
</erb> </erb>


to output this new content area in the view. to output this new content area in the view.
Expand Down
2 changes: 1 addition & 1 deletion doc/images.md
Expand Up @@ -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`` having to resize images in the editor then I would use the built in ``content_fu``
command like this in my view: 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 ``content_fu`` is a command we have created that automatically changes all images
with the url /system/images to use a particular size. with the url /system/images to use a particular size.
Expand Down
53 changes: 24 additions & 29 deletions pages/app/models/refinery/page.rb
Expand Up @@ -7,13 +7,12 @@ class Page < ActiveRecord::Base
translates :title, :custom_title, :meta_keywords, :meta_description, :browser_title, :include => :seo_meta translates :title, :custom_title, :meta_keywords, :meta_description, :browser_title, :include => :seo_meta


# Set up support for meta tags through translations. # Set up support for meta tags through translations.
if defined?(::Page::Translation) if self.respond_to?(:translation_class)
attr_accessible :title attr_accessible :title
# set allowed attributes for mass assignment # set allowed attributes for mass assignment
::Page::Translation.send :attr_accessible, :browser_title, :meta_description, self.translation_class.send :attr_accessible, :browser_title, :meta_description, :meta_keywords, :locale
:meta_keywords, :locale


if ::Page::Translation.table_exists? if self.translation_class.table_exists?
def translation def translation
if @translation.nil? or @translation.try(:locale) != ::Globalize.locale if @translation.nil? or @translation.try(:locale) != ::Globalize.locale
@translation = translations.with_locale(::Globalize.locale).first @translation = translations.with_locale(::Globalize.locale).first
Expand All @@ -23,15 +22,26 @@ def translation
@translation @translation
end end


# Instruct the Translation model to have meta tags. # Instruct the translation_class model to have meta tags.
::Page::Translation.send :is_seo_meta self.translation_class.send :is_seo_meta


fields = ::SeoMeta.attributes.keys.reject{|f| fields = ::SeoMeta.attributes.keys.reject{|f|
self.column_names.map(&:to_sym).include?(f) self.column_names.map(&:to_sym).include?(f)
}.map{|a| [a, :"#{a}="]}.flatten }.map{|a| [a, :"#{a}="]}.flatten
delegate *(fields << {:to => :translation}) delegate *(fields << {:to => :translation})
after_save proc {|m| m.translation.save} after_save proc {|m| m.translation.save}
end 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 end


before_create :ensure_locale, :if => proc { |c| before_create :ensure_locale, :if => proc { |c|
Expand Down Expand Up @@ -59,11 +69,11 @@ def translation
:strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => "pages") :strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => "pages")


has_many :parts, has_many :parts,
:class_name => "PagePart", :class_name => "::Refinery::PagePart",
:order => "position ASC", :order => "position ASC",
:inverse_of => :page, :inverse_of => :page,
:dependent => :destroy, :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 accepts_nested_attributes_for :parts, :allow_destroy => true


Expand All @@ -75,18 +85,6 @@ def translation
after_save :reposition_parts!, :invalidate_child_cached_url, :expire_page_caching after_save :reposition_parts!, :invalidate_child_cached_url, :expire_page_caching
after_destroy :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 :live, where(:draft => false)
scope :by_title, proc {|t| with_globalize(:title => t)} scope :by_title, proc {|t| with_globalize(:title => t)}


Expand Down Expand Up @@ -197,7 +195,7 @@ def url_marketable
end end


def url_normal def url_normal
{:controller => '/pages', :action => 'show', :path => nil, :id => to_param} {:controller => '/refinery/pages', :action => 'show', :path => nil, :id => to_param}
end end


def with_locale_param(url_hash) def with_locale_param(url_hash)
Expand Down Expand Up @@ -301,13 +299,10 @@ def expire_page_caching
# Accessor method to get a page part from a page. # Accessor method to get a page part from a page.
# Example: # Example:
# #
# Page.first[:body] # Page.first.content_for(:body)
# #
# Will return the body page part of the first page. # Will return the body page part of the first page.
def [](part_title) def content_for(part_title)
# Allow for calling attributes with [] shorthand (eg page[:parent_id])
return super if self.attributes.has_key?(part_title.to_s)

# the way that we call page parts seems flawed, will probably revert to page.parts[:title] in a future release. # 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. # 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| part = self.parts.detect do |part|
Expand All @@ -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 # In the admin area we use a slightly different title to inform the which pages are draft or hidden pages
def title_with_meta def title_with_meta
title = if self.title.nil? 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 else
[self.title.to_s] [self.title.to_s]
end end


title << "<em>(#{::I18n.t('hidden', :scope => 'admin.pages.page')})</em>" unless show_in_menu? title << "<em>(#{::I18n.t('hidden', :scope => 'refinery.admin.pages.page')})</em>" unless show_in_menu?
title << "<em>(#{::I18n.t('draft', :scope => 'admin.pages.page')})</em>" if draft? title << "<em>(#{::I18n.t('draft', :scope => 'refinery.admin.pages.page')})</em>" if draft?


title.join(' ') title.join(' ')
end end
Expand Down
@@ -1,5 +1,5 @@
<div class='page_part' id='<%= new_part ? "page_part_new_#{part_index}" : part.to_param %>'> <div class='page_part' id='<%= new_part ? "page_part_new_#{part_index}" : part.to_param %>'>
<%= hidden_field_tag "page[parts_attributes][#{part_index}][title]", part.title 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' %> <%= 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}][position]", part_index if new_part %>
</div> </div>
4 changes: 2 additions & 2 deletions pages/lib/refinery/pages/admin/instance_methods.rb
Expand Up @@ -8,8 +8,8 @@ def error_404(exception=nil)
params[:action] = 'error_404' params[:action] = 'error_404'
# change any links in the copy to the refinery_admin_root_path # change any links in the copy to the refinery_admin_root_path
# and any references to "home page" to "Dashboard" # and any references to "home page" to "Dashboard"
part_symbol = Page.default_parts.first.to_sym part_symbol = ::Refinery::Page.default_parts.first.to_sym
@page[part_symbol] = @page[part_symbol].to_s.gsub( @page.content_for(part_symbol) = @page.content_for(part_symbol).to_s.gsub(
/href=(\'|\")\/(\'|\")/, "href='#{refinery_admin_root_path}'" /href=(\'|\")\/(\'|\")/, "href='#{refinery_admin_root_path}'"
).gsub("home page", "Dashboard") ).gsub("home page", "Dashboard")


Expand Down

0 comments on commit edc09a8

Please sign in to comment.