diff options
author | Adrien Coquio <adrien.coquio@gmail.com> | 2012-06-03 21:37:41 +0200 |
---|---|---|
committer | Philip Arndt <parndt@gmail.com> | 2012-06-09 11:19:38 +1200 |
commit | d0c249af2a2e630ac993eb23a691ef996613b55f (patch) | |
tree | 6992cf3816a6f5b1e022637a6780a7d24f1753cf /app/models | |
parent | 7743fab73a5c60d9f5f0cf2ce65a3f0914b1376f (diff) | |
download | refinerycms-blog-d0c249af2a2e630ac993eb23a691ef996613b55f.tar.gz refinerycms-blog-d0c249af2a2e630ac993eb23a691ef996613b55f.tar.bz2 refinerycms-blog-d0c249af2a2e630ac993eb23a691ef996613b55f.zip |
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.
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/refinery/blog/category.rb | 16 | ||||
-rw-r--r-- | app/models/refinery/blog/post.rb | 56 | ||||
-rw-r--r-- | app/models/refinery/categorization.rb | 4 |
3 files changed, 60 insertions, 16 deletions
diff --git a/app/models/refinery/blog/category.rb b/app/models/refinery/blog/category.rb index 2935c89..36e6816 100644 --- a/app/models/refinery/blog/category.rb +++ b/app/models/refinery/blog/category.rb @@ -1,8 +1,11 @@ module Refinery module Blog class Category < ActiveRecord::Base + + translates :title, :slug + extend FriendlyId - friendly_id :title, :use => [:slugged] + friendly_id :title, :use => [:slugged, :globalize] has_many :categorizations, :dependent => :destroy, :foreign_key => :blog_category_id has_many :posts, :through => :categorizations, :source => :blog_post @@ -12,9 +15,18 @@ module Refinery validates :title, :presence => true, :uniqueness => true attr_accessible :title + attr_accessor :locale + + class Translation + attr_accessible :locale + end + + def self.translated + with_translations(::Globalize.locale) + end def post_count - posts.live.count + posts.live.with_globalize.count end # how many items to show per page 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 diff --git a/app/models/refinery/categorization.rb b/app/models/refinery/categorization.rb index ec51ea7..688da6a 100644 --- a/app/models/refinery/categorization.rb +++ b/app/models/refinery/categorization.rb @@ -4,7 +4,7 @@ module Refinery self.table_name = 'refinery_blog_categories_blog_posts' belongs_to :blog_post, :class_name => 'Refinery::Blog::Post', :foreign_key => :blog_post_id belongs_to :blog_category, :class_name => 'Refinery::Blog::Category', :foreign_key => :blog_category_id - + attr_accessible :blog_category_id, :blog_post_id end -end
\ No newline at end of file +end |