aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorAdrien Coquio <adrien.coquio@gmail.com>2012-06-03 21:37:41 +0200
committerPhilip Arndt <parndt@gmail.com>2012-06-09 11:19:38 +1200
commitd0c249af2a2e630ac993eb23a691ef996613b55f (patch)
tree6992cf3816a6f5b1e022637a6780a7d24f1753cf /app
parent7743fab73a5c60d9f5f0cf2ce65a3f0914b1376f (diff)
downloadrefinerycms-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')
-rw-r--r--app/controllers/refinery/blog/admin/posts_controller.rb7
-rw-r--r--app/controllers/refinery/blog/categories_controller.rb2
-rw-r--r--app/controllers/refinery/blog/posts_controller.rb4
-rw-r--r--app/helpers/refinery/blog/controller_helper.rb12
-rw-r--r--app/models/refinery/blog/category.rb16
-rw-r--r--app/models/refinery/blog/post.rb56
-rw-r--r--app/models/refinery/categorization.rb4
-rw-r--r--app/views/refinery/blog/admin/categories/_category.html.erb12
-rw-r--r--app/views/refinery/blog/admin/categories/_form.html.erb3
-rw-r--r--app/views/refinery/blog/admin/posts/_form.html.erb3
-rw-r--r--app/views/refinery/blog/admin/posts/_post.html.erb11
-rw-r--r--app/views/refinery/blog/admin/shared/_locale_picker.html.erb11
-rw-r--r--app/views/refinery/blog/shared/_post.html.erb2
13 files changed, 113 insertions, 30 deletions
diff --git a/app/controllers/refinery/blog/admin/posts_controller.rb b/app/controllers/refinery/blog/admin/posts_controller.rb
index b379a24..e48c00d 100644
--- a/app/controllers/refinery/blog/admin/posts_controller.rb
+++ b/app/controllers/refinery/blog/admin/posts_controller.rb
@@ -6,7 +6,8 @@ module Refinery
cache_sweeper Refinery::BlogSweeper
crudify :'refinery/blog/post',
- :order => 'published_at DESC'
+ :order => 'published_at DESC',
+ :include => [:translations]
before_filter :find_all_categories,
:only => [:new, :edit, :create, :update]
@@ -77,6 +78,10 @@ module Refinery
end
protected
+ def find_post
+ @post = Refinery::Blog::Post.find_by_slug_or_id(params[:id])
+ end
+
def find_all_categories
@categories = Refinery::Blog::Category.find(:all)
end
diff --git a/app/controllers/refinery/blog/categories_controller.rb b/app/controllers/refinery/blog/categories_controller.rb
index 467726e..23a835a 100644
--- a/app/controllers/refinery/blog/categories_controller.rb
+++ b/app/controllers/refinery/blog/categories_controller.rb
@@ -4,7 +4,7 @@ module Refinery
def show
@category = Refinery::Blog::Category.find(params[:id])
- @posts = @category.posts.live.includes(:comments, :categories).page(params[:page])
+ @posts = @category.posts.live.includes(:comments, :categories).with_globalize.page(params[:page])
end
end
diff --git a/app/controllers/refinery/blog/posts_controller.rb b/app/controllers/refinery/blog/posts_controller.rb
index caa46ec..bb242fd 100644
--- a/app/controllers/refinery/blog/posts_controller.rb
+++ b/app/controllers/refinery/blog/posts_controller.rb
@@ -12,7 +12,7 @@ module Refinery
def index
# Rss feeders are greedy. Let's give them every blog post instead of paginating.
- (@posts = Post.live.includes(:comments, :categories).all) if request.format.rss?
+ (@posts = Post.live.includes(:comments, :categories).with_globalize) if request.format.rss?
respond_with (@posts) do |format|
format.html
format.rss
@@ -22,7 +22,7 @@ module Refinery
def show
@comment = Comment.new
- @canonical = url_for(:locale => ::Refinery::I18n.default_frontend_locale) if canonical?
+ @canonical = refinery.url_for(:locale => Refinery::I18n.current_frontend_locale) if canonical?
@post.increment!(:access_count, 1)
diff --git a/app/helpers/refinery/blog/controller_helper.rb b/app/helpers/refinery/blog/controller_helper.rb
index bf4926b..87d5447 100644
--- a/app/helpers/refinery/blog/controller_helper.rb
+++ b/app/helpers/refinery/blog/controller_helper.rb
@@ -1,11 +1,11 @@
module Refinery
module Blog
module ControllerHelper
-
+
protected
-
+
def find_blog_post
- unless (@post = Refinery::Blog::Post.find(params[:id])).try(:live?)
+ unless (@post = Refinery::Blog::Post.with_globalize.find(params[:id])).try(:live?)
if refinery_user? and current_refinery_user.authorized_plugins.include?("refinerycms_blog")
@post = Refinery::Blog::Post.find(params[:id])
else
@@ -13,16 +13,16 @@ module Refinery
end
end
end
-
+
def find_all_blog_posts
- @posts = Refinery::Blog::Post.live.includes(:comments, :categories).page(params[:page])
+ @posts = Refinery::Blog::Post.live.includes(:comments, :categories).with_globalize.page(params[:page])
end
def find_tags
@tags = Refinery::Blog::Post.tag_counts_on(:tags)
end
def find_all_blog_categories
- @categories = Refinery::Blog::Category.all
+ @categories = Refinery::Blog::Category.translated
end
end
end
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
diff --git a/app/views/refinery/blog/admin/categories/_category.html.erb b/app/views/refinery/blog/admin/categories/_category.html.erb
index b7d352f..d31e4e6 100644
--- a/app/views/refinery/blog/admin/categories/_category.html.erb
+++ b/app/views/refinery/blog/admin/categories/_category.html.erb
@@ -1,7 +1,15 @@
<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(category) -%>">
<span class='title'>
- <%= category.title %>
- <span class="preview">&nbsp;</span>
+ <%= category.title.presence || category.translations.detect {|t| t.title.present?}.title %>
+ <span class="preview">
+ <% category.translations.each do |translation| %>
+ <% if translation.title.present? %>
+ <%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'),
+ refinery.edit_blog_admin_category_path(category, :switch_locale => translation.locale),
+ :class => 'locale' %>
+ <% end %>
+ <% end %>
+ </span>
</span>
<span class='actions'>
<%= link_to refinery_icon_tag("application_edit.png"),
diff --git a/app/views/refinery/blog/admin/categories/_form.html.erb b/app/views/refinery/blog/admin/categories/_form.html.erb
index b1cf16c..6b4e20d 100644
--- a/app/views/refinery/blog/admin/categories/_form.html.erb
+++ b/app/views/refinery/blog/admin/categories/_form.html.erb
@@ -5,6 +5,9 @@
:include_object_name => true
} %>
+ <%= render "/refinery/blog/admin/shared/locale_picker",
+ :current_locale => Thread.current[:globalize_locale] if Refinery.i18n_enabled? %>
+
<div class='field'>
<%= f.label :title -%>
<%= f.text_field :title, :class => 'larger widest' -%>
diff --git a/app/views/refinery/blog/admin/posts/_form.html.erb b/app/views/refinery/blog/admin/posts/_form.html.erb
index a977754..b6fc535 100644
--- a/app/views/refinery/blog/admin/posts/_form.html.erb
+++ b/app/views/refinery/blog/admin/posts/_form.html.erb
@@ -5,6 +5,9 @@
:include_object_name => true
} %>
+ <%= render "/refinery/blog/admin/shared/locale_picker",
+ :current_locale => Thread.current[:globalize_locale] if Refinery.i18n_enabled? %>
+
<div class='field'>
<%= f.label :title -%>
<%= f.text_field :title, :class => 'larger widest' -%>
diff --git a/app/views/refinery/blog/admin/posts/_post.html.erb b/app/views/refinery/blog/admin/posts/_post.html.erb
index fea3eb8..781595a 100644
--- a/app/views/refinery/blog/admin/posts/_post.html.erb
+++ b/app/views/refinery/blog/admin/posts/_post.html.erb
@@ -1,7 +1,16 @@
<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(post) -%>">
<span class='title'>
- <%= post.title %>
+ <%= post.title.presence || post.translations.detect {|t| t.title.present?}.title %>
<span class="preview">
+ <% post.translations.each do |translation| %>
+ <% if translation.title.present? %>
+ <%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'),
+ refinery.edit_blog_admin_post_path(post, :switch_locale => translation.locale),
+ :class => 'locale' %>
+ <% end %>
+ <% end %>
+
+ <%= post.published_at.try(:strftime, '%b %d, %Y') || 'draft' %>
<%= " by #{post.author.username}" if post.author.present? %>
<% if post.draft? %>
<span class="label notice"><%= t('refinery.blog.admin.posts.post.draft') %></span>
diff --git a/app/views/refinery/blog/admin/shared/_locale_picker.html.erb b/app/views/refinery/blog/admin/shared/_locale_picker.html.erb
new file mode 100644
index 0000000..e162364
--- /dev/null
+++ b/app/views/refinery/blog/admin/shared/_locale_picker.html.erb
@@ -0,0 +1,11 @@
+<input type='hidden' name='switch_locale' id='switch_locale' value='<%= local_assigns[:current_locale] %>' />
+<% if (locales ||= Refinery::I18n.frontend_locales).present? and locales.many? %>
+ <ul id='switch_locale_picker' class='clearfix'>
+ <% locales.each do |locale| %>
+ <li<%= " class='selected'" if locale.to_s == local_assigns[:current_locale].to_s %>>
+ <%= link_to refinery_icon_tag("flags/#{locale}.png", :size => "32x22"),
+ refinery.url_for(:switch_locale => locale) %>
+ </li>
+ <% end %>
+ </ul>
+<% end %>
diff --git a/app/views/refinery/blog/shared/_post.html.erb b/app/views/refinery/blog/shared/_post.html.erb
index 5b90c8e..40ae70b 100644
--- a/app/views/refinery/blog/shared/_post.html.erb
+++ b/app/views/refinery/blog/shared/_post.html.erb
@@ -7,7 +7,7 @@
<%= t('created_at', :scope => 'refinery.blog.shared.posts', :when => l(post.published_at.to_date, :format => :short)) %>
</time>
<%= "#{t('by', :scope => 'refinery.blog.posts.show')} #{post.author.username}" if post.author.present? %>.
- <% if (categories = post.categories).any? %>
+ <% if (categories = post.categories.translated).any? %>
<aside class='filed_in'>
<%= t('filed_in', :scope => 'refinery.blog.posts.show') %>
<%=raw categories.collect { |category| link_to category.title, refinery.blog_category_path(category) }.to_sentence %>