From 2522cde2f5ca3160eabde77d95b9b7c729a80f6b Mon Sep 17 00:00:00 2001 From: chris2tof Date: Fri, 27 May 2011 11:27:54 +0800 Subject: [PATCH] Edition of page part for multiple languages. --- Gemfile | 2 ++ Gemfile.lock | 5 +++-- app/controllers/admin/page_parts_controller.rb | 8 ++++++++ app/models/page_part.rb | 17 +++++++++++++++++ app/views/admin/page_parts/_edit.html.erb | 10 ---------- app/views/admin/page_parts/_edit_text.html.erb | 17 +++++++++++++++++ app/views/admin/page_parts/_form.html.erb | 2 +- .../admin/page_parts/_language_bar.html.erb | 7 +++++++ app/views/admin/page_parts/edit.html.erb | 2 +- app/views/admin/page_parts/edit.js.erb | 2 +- lib/parser.rb | 2 +- lib/tasks/dev.rake | 6 ++++-- public/javascripts/application.js | 8 ++++++++ 13 files changed, 70 insertions(+), 18 deletions(-) delete mode 100644 app/views/admin/page_parts/_edit.html.erb create mode 100644 app/views/admin/page_parts/_edit_text.html.erb create mode 100644 app/views/admin/page_parts/_language_bar.html.erb diff --git a/Gemfile b/Gemfile index 36e27834..f5c20704 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,8 @@ gem 'mini_magick' gem 'mongoid', '2.0.0' +gem 'rake', '0.8.7' + # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+) # gem 'ruby-debug' # gem 'ruby-debug19' diff --git a/Gemfile.lock b/Gemfile.lock index 49ee0438..3151b6c0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,7 +36,7 @@ GEM carrierwave (0.5.4) activesupport (~> 3.0) chronic (0.3.0) - ckeditor (3.5.3) + ckeditor (3.5.4) mime-types (>= 1.16) delorean (1.0.0) chronic @@ -88,7 +88,7 @@ GEM activesupport (= 3.0.6) rake (>= 0.8.7) thor (~> 0.14.4) - rake (0.9.0) + rake (0.8.7) rcov (0.9.9) rspec (2.6.0) rspec-core (~> 2.6.0) @@ -129,6 +129,7 @@ DEPENDENCIES mongoid (= 2.0.0) nokogiri rails (= 3.0.6) + rake (= 0.8.7) rcov rspec (~> 2.0) rspec-rails (~> 2.0) diff --git a/app/controllers/admin/page_parts_controller.rb b/app/controllers/admin/page_parts_controller.rb index 18e67d07..dc062448 100644 --- a/app/controllers/admin/page_parts_controller.rb +++ b/app/controllers/admin/page_parts_controller.rb @@ -22,6 +22,14 @@ class Admin::PagePartsController < ApplicationController def edit @part = PagePart.find(params[:id]) + case @part.kind + when 'text' + @i18n_variable = @part.i18n_variable + @partial = 'edit_text' + @part_locale = params[:part_locale] || I18n.locale.to_s + when 'module' + when 'snippet' + end end def create diff --git a/app/models/page_part.rb b/app/models/page_part.rb index 547c96b4..e4860990 100644 --- a/app/models/page_part.rb +++ b/app/models/page_part.rb @@ -6,7 +6,24 @@ class PagePart field :name field :content field :kind + field :i18n_variable_id, :type => BSON::ObjectId belongs_to :page + + # Get the i18n_variable + def i18n_variable + @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil + end + + # Update or create the i18n_variable record + def i18n_variable=(attr) + if self.i18n_variable_id + self.i18n_variable.update_attributes(attr) + else + var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.page.i18n_variable_id})) + var.save + self.i18n_variable_id = var.id + end + end end \ No newline at end of file diff --git a/app/views/admin/page_parts/_edit.html.erb b/app/views/admin/page_parts/_edit.html.erb deleted file mode 100644 index 0c2bbef4..00000000 --- a/app/views/admin/page_parts/_edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<%= flash_messages %> - -<%= form_for @part, :url => admin_page_part_path(@part), :html => { :class => 'form' } do |f| %> - - <%= render :partial => "admin/page_parts/form", :locals => { :f => f } %> - -

- <%= f.submit t(:update) %> <%= link_back %> -

-<% end %> \ No newline at end of file diff --git a/app/views/admin/page_parts/_edit_text.html.erb b/app/views/admin/page_parts/_edit_text.html.erb new file mode 100644 index 00000000..fe149f34 --- /dev/null +++ b/app/views/admin/page_parts/_edit_text.html.erb @@ -0,0 +1,17 @@ +<%= flash_messages %> + +<%= render 'language_bar' %> + + +<%= form_for @part, :url => admin_page_part_path(@part), :html => { :class => 'form' } do |f| %> + <%= fields_for "page_part[i18n_variable]", @i18n_variable, :index => nil do |f| %> + <% @site_valid_locales.each do |locale| %> +
+ <%= render :partial => "admin/page_parts/form", :locals => { :f => f, :locale => locale } %> +
+ <% end %> + <% end %> +

+ <%= f.submit t(:update) %> <%= link_back %> +

+<% end %> \ No newline at end of file diff --git a/app/views/admin/page_parts/_form.html.erb b/app/views/admin/page_parts/_form.html.erb index 7dc218db..610aeada 100644 --- a/app/views/admin/page_parts/_form.html.erb +++ b/app/views/admin/page_parts/_form.html.erb @@ -1,3 +1,3 @@ <%= f.error_messages %> -<%= f.cktext_area :content, :toolbar=>'Full', :width=>'700px', :height=>'200px' %> \ No newline at end of file +<%= f.cktext_area locale, :toolbar=>'Full', :width=>'700px', :height=>'200px'%> \ No newline at end of file diff --git a/app/views/admin/page_parts/_language_bar.html.erb b/app/views/admin/page_parts/_language_bar.html.erb new file mode 100644 index 00000000..5ba98931 --- /dev/null +++ b/app/views/admin/page_parts/_language_bar.html.erb @@ -0,0 +1,7 @@ +<%= raw(VALID_LOCALES.map{ |locale| + if @part_locale.to_s.eql?(locale) + t(:_locale, :locale => locale) + else + link_to t(:_locale, :locale => locale), edit_admin_page_part_path(@part, :part_locale => locale), :class => 'reload' + end +}.join(' | ')) %> \ No newline at end of file diff --git a/app/views/admin/page_parts/edit.html.erb b/app/views/admin/page_parts/edit.html.erb index 65bcf909..9a962fdc 100644 --- a/app/views/admin/page_parts/edit.html.erb +++ b/app/views/admin/page_parts/edit.html.erb @@ -2,4 +2,4 @@ <% end -%> -<%= render 'edit' %> \ No newline at end of file +<%= render @partial %> \ No newline at end of file diff --git a/app/views/admin/page_parts/edit.js.erb b/app/views/admin/page_parts/edit.js.erb index d44d0901..e073cd8a 100644 --- a/app/views/admin/page_parts/edit.js.erb +++ b/app/views/admin/page_parts/edit.js.erb @@ -1 +1 @@ -$('#main').html("<%= escape_javascript(render(:partial => 'edit')) %>"); \ No newline at end of file +$('#main').html("<%= escape_javascript(render(:partial => @partial)) %>"); \ No newline at end of file diff --git a/lib/parser.rb b/lib/parser.rb index a34588d5..660bb612 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -182,7 +182,7 @@ module Parser ret << "' if part - ret << part.content rescue '' + ret << part.i18n_variable[I18n.locale.to_s] rescue '' ret << tag.expand ret << '' end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 4dd451d7..518e269a 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -31,6 +31,8 @@ namespace :dev do var_10 = I18nVariable.create!( :document_class => 'Home', :key => 'home', :en => 'Homepage', :zh_tw => '首頁') var_11 = I18nVariable.create!( :document_class => 'Page', :key => 'about', :en => 'About', :zh_tw => '關於我們' ) var_12 = I18nVariable.create!( :document_class => 'Link', :key => 'google', :en => 'Google', :zh_tw => 'Google' ) + var_13 = I18nVariable.create!( :document_class => 'PagePart', :key => 'main_content', :en => 'This is the homepage', :zh_tw => '這是首頁', :parent_id => var_10.id ) + var_14 = I18nVariable.create!( :document_class => 'PagePart', :key => 'main_content', :en => 'This is about', :zh_tw => '這是關於', :parent_id => var_11.id ) # TODO: modify for the new model @@ -54,10 +56,10 @@ namespace :dev do layout.layout_parts.create!( :name => 'footer', :content => "" ) home = Page.create!( :i18n_variable_id => var_10.id, :layout_id => layout.id, :name => 'home', :is_published => true ) - home.page_parts.create!( :name => 'main_content', :content => "" ) + home.page_parts.create!( :name => 'main_content', :content => "", :kind => 'text', :i18n_variable_id => var_13.id ) about = Page.create!( :i18n_variable_id => var_11.id, :layout_id => layout.id, :name => 'about', :is_published => true, :parent_id => home.id ) - about.page_parts.create!( :name => 'main_content', :content => "This is about" ) + about.page_parts.create!( :name => 'main_content', :content => "", :kind => 'text', :i18n_variable_id => var_14.id ) Link.create!( :i18n_variable_id => var_12.id, :name => 'google', :is_published => true, :parent_id => home.id, :url => 'www.google.com' ) diff --git a/public/javascripts/application.js b/public/javascripts/application.js index ea707041..3dca2b11 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -10,6 +10,14 @@ $(function () { } ); + $('#main a.reload').live('click', + function () { + $.getScript(this.href); + history.replaceState(null, document.title, this.href); + return false; + } + ); + $('.form').live('submit', function () { $.post(this.action, $(this).serialize(), null, 'script'); return false;