app/models/page.rb in refinerycms-pages-0.9.9.19 vs app/models/page.rb in refinerycms-pages-0.9.9.20
- old
+ new
@@ -1,50 +1,55 @@
require 'globalize3'
class Page < ActiveRecord::Base
- translates :title, :meta_keywords, :meta_description, :browser_title, :custom_title if self.respond_to?(:translates)
+ if self.respond_to?(:translates)
+ translates :title, :custom_title, :meta_keywords, :meta_description, :browser_title, :include => :seo_meta
- attr_accessible :id, :deletable, :link_url, :menu_match, :meta_keywords,
- :skip_to_first_child, :position, :show_in_menu, :draft,
- :parts_attributes, :browser_title, :meta_description,
- :custom_title_type, :parent_id, :custom_title,
- :created_at, :updated_at, :page_id
+ # Set up support for meta tags through translations.
+ if defined?(::Page::Translation)
+ attr_accessible :title
+ # set allowed attributes for mass assignment
+ ::Page::Translation.send :attr_accessible, :browser_title, :meta_description,
+ :meta_keywords, :locale
- # Set up support for meta tags through translations.
- if defined?(::Page::Translation)
- attr_accessible :title
- # set allowed attributes for mass assignment
- ::Page::Translation.module_eval do
- attr_accessible :browser_title, :meta_description, :meta_keywords,
- :locale
- end
- if ::Page::Translation.table_exists?
- def translation
- if @translation.nil? or @translation.try(:locale) != ::Globalize.locale
- @translation = translations.with_locale(::Globalize.locale).first
- @translation ||= translations.build(:locale => ::Globalize.locale)
+ if ::Page::Translation.table_exists?
+ def translation
+ if @translation.nil? or @translation.try(:locale) != ::Globalize.locale
+ @translation = translations.with_locale(::Globalize.locale).first
+ @translation ||= translations.build(:locale => ::Globalize.locale)
+ end
+
+ @translation
end
- @translation
- end
+ # Instruct the Translation model to have meta tags.
+ ::Page::Translation.send :is_seo_meta
- # Instruct the Translation model to have meta tags.
- ::Page::Translation.send :is_seo_meta
-
- # Delegate all SeoMeta attributes to the active translation.
- fields = ::SeoMeta.attributes.keys.map{|a| [a, :"#{a}="]}.flatten
- fields << {:to => :translation}
- delegate *fields
- after_save proc {|m| m.translation.save}
+ fields = ::SeoMeta.attributes.keys.reject{|f|
+ self.column_names.map(&:to_sym).include?(f)
+ }.map{|a| [a, :"#{a}="]}.flatten
+ delegate *(fields << {:to => :translation})
+ after_save proc {|m| m.translation.save}
+ end
end
+
+ before_create :ensure_locale, :if => proc { |c|
+ defined?(::Refinery::I18n) && ::Refinery::I18n.enabled?
+ }
end
+ attr_accessible :id, :deletable, :link_url, :menu_match, :meta_keywords,
+ :skip_to_first_child, :position, :show_in_menu, :draft,
+ :parts_attributes, :browser_title, :meta_description,
+ :custom_title_type, :parent_id, :custom_title,
+ :created_at, :updated_at, :page_id
+
attr_accessor :locale # to hold temporarily
validates :title, :presence => true
- acts_as_nested_set
+ acts_as_nested_set :dependent => :destroy # rather than :delete_all
# Docs for friendly_id http://github.com/norman/friendly_id
has_friendly_id :title, :use_slug => true,
:default_locale => (::Refinery::I18n.default_frontend_locale rescue :en),
:reserved_words => %w(index new session login logout users refinery admin images wymiframe),
@@ -54,11 +59,11 @@
has_many :parts,
:class_name => "PagePart",
:order => "position ASC",
:inverse_of => :page,
:dependent => :destroy,
- :include => :translations
+ :include => ((:translations) if defined?(::PagePart::Translation))
accepts_nested_attributes_for :parts, :allow_destroy => true
# Docs for acts_as_indexed http://github.com/dougal/acts_as_indexed
acts_as_indexed :fields => [:title, :meta_keywords, :meta_description,
@@ -67,16 +72,16 @@
before_destroy :deletable?
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.
- scope :with_globalize, lambda {|t|
+ scope :with_globalize, lambda {|conditions|
if defined?(::Page::Translation)
- t = {:locale => Globalize.locale}.merge(t || {})
- where(:id => ::Page::Translation.where(t).select('page_id AS id'))
+ conditions = {:locale => Globalize.locale}.merge(conditions || {})
+ where(:id => ::Page::Translation.where(conditions).select('page_id AS id')).includes(:translations)
else
- where(t)
+ where(conditions)
end
}
scope :live, where(:draft => false)
scope :by_title, lambda {|t| with_globalize(:title => t)}
@@ -350,9 +355,15 @@
return true unless self.class.use_marketable_urls?
children.each do |child|
Rails.cache.delete(child.url_cache_key)
Rails.cache.delete(child.path_cache_key)
+ end
+ end
+
+ def ensure_locale
+ unless self.translations.present?
+ self.translations.build :locale => ::Refinery::I18n.default_frontend_locale
end
end
def expire_page_caching
self.class.expire_page_caching