From d0c249af2a2e630ac993eb23a691ef996613b55f Mon Sep 17 00:00:00 2001 From: Adrien Coquio Date: Sun, 3 Jun 2012 21:37:41 +0200 Subject: Added i18n support to models through globalize3. * Added Post translation * Added Category translation * Use friendly_id globalize with category * Added migrate_data option on migrations for translations * Use Refinery locale instead of I18n * Refactored duplicate locale_picker partial * Removed useless .all call * Use presence instead if / blank? * Added with_globalize scopes when loading posts of one category * Use Globalize when creating post factory * Fix failing specs by creating blog posts/categories using needed locale. --- app/models/refinery/blog/post.rb | 56 +++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'app/models/refinery/blog/post.rb') diff --git a/app/models/refinery/blog/post.rb b/app/models/refinery/blog/post.rb index c32b775..024872e 100644 --- a/app/models/refinery/blog/post.rb +++ b/app/models/refinery/blog/post.rb @@ -4,8 +4,11 @@ require 'seo_meta' module Refinery module Blog class Post < ActiveRecord::Base + + translates :title, :body, :custom_url, :custom_teaser, :slug, :include => :seo_meta + extend FriendlyId - friendly_id :friendly_id_source, :use => [:slugged] + friendly_id :friendly_id_source, :use => [:slugged, :globalize] is_seo_meta if self.table_exists? @@ -33,6 +36,13 @@ module Refinery attr_accessible :title, :body, :custom_teaser, :tag_list, :draft, :published_at, :custom_url, :author attr_accessible :browser_title, :meta_keywords, :meta_description, :user_id, :category_ids attr_accessible :source_url, :source_url_title + attr_accessor :locale + + + class Translation + is_seo_meta + attr_accessible :browser_title, :meta_description, :meta_keywords, :locale + end self.per_page = Refinery::Blog.posts_per_page @@ -53,45 +63,67 @@ module Refinery end class << self + + # Wrap up the logic of finding the pages based on the translations table. + def with_globalize(conditions = {}) + conditions = {:locale => ::Globalize.locale}.merge(conditions) + globalized_conditions = {} + conditions.keys.each do |key| + if (translated_attribute_names.map(&:to_s) | %w(locale)).include?(key.to_s) + globalized_conditions["#{self.translation_class.table_name}.#{key}"] = conditions.delete(key) + end + end + # A join implies readonly which we don't really want. + joins(:translations).where(globalized_conditions).where(conditions).readonly(false) + end + + def find_by_slug_or_id(slug_or_id) + if slug_or_id.friendly_id? + find_by_slug(slug_or_id) + else + find(slug_or_id) + end + end + def by_month(date) - where(:published_at => date.beginning_of_month..date.end_of_month) + where(:published_at => date.beginning_of_month..date.end_of_month).with_globalize end - + def by_archive(date) Refinery.deprecate("Refinery::Blog::Post.by_archive(date)", {:replacement => "Refinery::Blog::Post.by_month(date)", :when => 2.2 }) by_month(date) end - + def by_year(date) - where(:published_at => date.beginning_of_year..date.end_of_year) + where(:published_at => date.beginning_of_year..date.end_of_year).with_globalize end def published_dates_older_than(date) - published_before(date).pluck(:published_at) + published_before(date).with_globalize.pluck(:published_at) end def recent(count) - live.limit(count) + live.limit(count).with_globalize end def popular(count) - unscoped.order("access_count DESC").limit(count) + unscoped.order("access_count DESC").limit(count).with_globalize end def previous(item) - published_before(item.published_at).first + published_before(item.published_at).with_globalize.first end def uncategorized - live.includes(:categories).where(:categories => { Refinery::Categorization.table_name => { :blog_category_id => nil } }) + live.includes(:categories).where(:categories => { Refinery::Categorization.table_name => { :blog_category_id => nil } }).with_globalize end def next(current_record) - where(["published_at > ? and draft = ?", current_record.published_at, false]).first + where(["published_at > ? and draft = ?", current_record.published_at, false]).with_globalize.first end def published_before(date=Time.now) - where("published_at < ? and draft = ?", date, false) + where("published_at < ? and draft = ?", date, false).with_globalize end alias_method :live, :published_before -- cgit v1.2.3