From 3c6e2d19a191e14b6c4645badf08cfc97c0a1da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Thu, 23 Aug 2012 12:31:51 +0200 Subject: [PATCH 01/10] Gemfile.lock --- Gemfile.lock | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 696b692..9ddcef6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - refinerycms-calendar (2.0.1) + refinerycms-calendar (2.0.2) refinerycms-core (~> 2.0.3) GEM @@ -84,7 +84,6 @@ GEM activemodel (>= 3.0.0) activerecord (>= 3.0.0) paper_trail (~> 2) - growl (1.0.3) guard (1.0.3) ffi (>= 0.5.0) thor (>= 0.14.6) @@ -98,6 +97,8 @@ GEM railties (>= 3.2.0, < 5.0) thor (~> 0.14) json (1.7.3) + libnotify (0.1.4) + ffi (>= 0.6.2) libwebsocket (0.1.3) addressable mail (2.4.4) @@ -137,7 +138,8 @@ GEM rdoc (~> 3.4) thor (~> 0.14.6) rake (0.9.2.2) - rb-fsevent (0.9.1) + rb-inotify (0.8.8) + ffi (>= 0.5.0) rdoc (3.12) json (~> 1.4) refinerycms (2.0.3) @@ -245,14 +247,14 @@ DEPENDENCIES coffee-rails factory_girl_rails generator_spec - growl (~> 1.0.3) guard-spork jquery-rails jruby-openssl + libnotify (~> 0.1.3) mysql2 pg rb-fchange (~> 0.0.5) - rb-fsevent (>= 0.3.9) + rb-inotify (>= 0.5.1) rb-notifu (~> 0.0.4) refinerycms (~> 2.0.0) refinerycms-calendar! From 1e715d25c1c5dfa32ef9419e4515ab280cb1a5c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 12:10:04 +0200 Subject: [PATCH 02/10] Added refinerycms-i18n. --- Gemfile | 1 + Gemfile.lock | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/Gemfile b/Gemfile index 0c09722..3194ed4 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source "http://rubygems.org" gemspec gem 'refinerycms', '~> 2.0.0' +gem 'refinerycms-i18n', :git => 'git://github.com/parndt/refinerycms-i18n.git' # Refinery/rails should pull in the proper versions of these group :assets do diff --git a/Gemfile.lock b/Gemfile.lock index 9ddcef6..c8db693 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,12 @@ +GIT + remote: git://github.com/parndt/refinerycms-i18n.git + revision: 2a6d35554421d4e0a2ebeba6eb291bcd0ade9291 + specs: + refinerycms-i18n (2.1.0.dev) + rails-i18n (~> 0.6.5) + refinerycms-core (~> 2.0) + routing-filter (~> 0.3.0) + PATH remote: . specs: @@ -130,6 +139,8 @@ GEM activesupport (= 3.2.3) bundler (~> 1.0) railties (= 3.2.3) + rails-i18n (0.6.5) + i18n (~> 0.5) railties (3.2.3) actionpack (= 3.2.3) activesupport (= 3.2.3) @@ -188,6 +199,8 @@ GEM rack-test (~> 0.6.0) refinerycms-core (= 2.0.3) rspec-rails (~> 2.8.1) + routing-filter (0.3.1) + actionpack rspec (2.8.0) rspec-core (~> 2.8.0) rspec-expectations (~> 2.8.0) @@ -258,6 +271,7 @@ DEPENDENCIES rb-notifu (~> 0.0.4) refinerycms (~> 2.0.0) refinerycms-calendar! + refinerycms-i18n! refinerycms-testing (~> 2.0.0) sass-rails spork (= 0.9.0.rc9) From bef290e8b8969be19c9cdda3e95b0ca7ec77988e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 12:10:35 +0200 Subject: [PATCH 03/10] Added i18n support to event model. --- app/models/refinery/calendar/event.rb | 32 ++++++++++++++++--- .../3_create_calendar_events_translations.rb | 16 ++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 db/migrate/3_create_calendar_events_translations.rb diff --git a/app/models/refinery/calendar/event.rb b/app/models/refinery/calendar/event.rb index 93f02bd..55780a8 100644 --- a/app/models/refinery/calendar/event.rb +++ b/app/models/refinery/calendar/event.rb @@ -1,9 +1,11 @@ module Refinery module Calendar class Event < Refinery::Core::BaseModel - extend FriendlyId - friendly_id :title, :use => :slugged + translates :title, :excerpt, :description, :slug + + extend FriendlyId + friendly_id :title, :use => [:slugged, :globalize] belongs_to :venue @@ -13,23 +15,43 @@ class Event < Refinery::Core::BaseModel :venue_id, :excerpt, :description, :featured, :position + attr_accessor :locale + delegate :name, :address, :to => :venue, :prefix => true, :allow_nil => true + class Translation + attr_accessible :locale + end + class << self def upcoming - where('refinery_calendar_events.from >= ?', Time.now) + where('refinery_calendar_events.from >= ?', Time.now).with_globalize end def featured - where(:featured => true) + where(:featured => true).with_globalize end def archive - where('refinery_calendar_events.from < ?', Time.now) + where('refinery_calendar_events.from < ?', Time.now).with_globalize + end + + # Wrap up the logic of finding the events 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 + end end end diff --git a/db/migrate/3_create_calendar_events_translations.rb b/db/migrate/3_create_calendar_events_translations.rb new file mode 100644 index 0000000..d931875 --- /dev/null +++ b/db/migrate/3_create_calendar_events_translations.rb @@ -0,0 +1,16 @@ +class CreateCalendarEventsTranslations < ActiveRecord::Migration + def up + Refinery::Calendar::Event.create_translation_table!({ + :title => :string, + :excerpt => :string, + :description => :text, + :slug => :string + }, { + :migrate_data => true + }) + end + + def down + Refinery::Calendar::Event.drop_translation_table! :migrate_data => true + end +end \ No newline at end of file From ad8477ddfa99627cc43c5018eb00fdf861ff7e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 12:10:55 +0200 Subject: [PATCH 04/10] Added i18n support to admin events controller. --- app/controllers/refinery/calendar/admin/events_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/refinery/calendar/admin/events_controller.rb b/app/controllers/refinery/calendar/admin/events_controller.rb index 19a1e3d..e1639b0 100644 --- a/app/controllers/refinery/calendar/admin/events_controller.rb +++ b/app/controllers/refinery/calendar/admin/events_controller.rb @@ -7,7 +7,8 @@ class EventsController < ::Refinery::AdminController crudify :'refinery/calendar/event', :xhr_paging => true, :sortable => false, - :order => "'from' DESC" + :order => "'from' DESC", + :include => [:translations] private def find_venues From 5a6567e1d56955395b841a3228e62ca75a2716d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 12:11:38 +0200 Subject: [PATCH 05/10] Added i18n support to admin events views. --- .../refinery/calendar/admin/events/_event.html.erb | 12 ++++++++++-- .../refinery/calendar/admin/events/_form.html.erb | 1 + .../calendar/admin/shared/_locale_picker.html.erb | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 app/views/refinery/calendar/admin/shared/_locale_picker.html.erb diff --git a/app/views/refinery/calendar/admin/events/_event.html.erb b/app/views/refinery/calendar/admin/events/_event.html.erb index 962decc..153a8cf 100644 --- a/app/views/refinery/calendar/admin/events/_event.html.erb +++ b/app/views/refinery/calendar/admin/events/_event.html.erb @@ -1,7 +1,15 @@
  • - <%= event.title %> - + <%= event.title.presence || event.translations.detect {|t| t.title.present?}.title %> + <% if Refinery.i18n_enabled? and Refinery::I18n.frontend_locales.many? %> + + <% event.translations.each do |translation| %> + <%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'), + refinery.edit_calendar_admin_event_path(event, :switch_locale => translation.locale), + :class => 'locale' if translation.title.present? %> + <% end %> + + <% end %> diff --git a/app/views/refinery/calendar/admin/events/_form.html.erb b/app/views/refinery/calendar/admin/events/_form.html.erb index 2127ca9..45b2d21 100644 --- a/app/views/refinery/calendar/admin/events/_form.html.erb +++ b/app/views/refinery/calendar/admin/events/_form.html.erb @@ -3,6 +3,7 @@ :object => @event, :include_object_name => true %> + <%= render '/refinery/calendar/admin/shared/locale_picker', :current_locale => Globalize.locale if Refinery.i18n_enabled? %>
    <%= f.label :title -%> diff --git a/app/views/refinery/calendar/admin/shared/_locale_picker.html.erb b/app/views/refinery/calendar/admin/shared/_locale_picker.html.erb new file mode 100644 index 0000000..9c1a3c6 --- /dev/null +++ b/app/views/refinery/calendar/admin/shared/_locale_picker.html.erb @@ -0,0 +1,11 @@ + +<% if (locales ||= (Refinery::I18n.frontend_locales if Refinery.i18n_enabled?)).present? && locales.many? %> +
      + <% locales.each do |locale| %> + > + <%= link_to refinery_icon_tag("flags/#{locale}.png", :size => "32x22"), + refinery.url_for(:switch_locale => locale) %> + + <% end %> +
    +<% end %> From 201c2c0024cc9dcd470aefe168a452256d48244c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 13:40:05 +0200 Subject: [PATCH 06/10] Testing events with i18n. --- .../refinery/calendar/admin/events_spec.rb | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/spec/requests/refinery/calendar/admin/events_spec.rb b/spec/requests/refinery/calendar/admin/events_spec.rb index 71525d9..9091e3d 100644 --- a/spec/requests/refinery/calendar/admin/events_spec.rb +++ b/spec/requests/refinery/calendar/admin/events_spec.rb @@ -95,6 +95,160 @@ end end + context "with translations" do + + before(:each) do + Globalize.locale = :en + Refinery::I18n.stub(:frontend_locales).and_return([:en, :es]) + event_page = Factory.create(:page, :link_url => "/event", :title => "Event") + Globalize.with_locale(:es) do + event_page.title = 'Evento' + event_page.save + end + visit refinery.calendar_admin_events_path + end + + describe "add a event with title for default locale" do + before do + click_link "Add New Event" + fill_in "Title", :with => "Event" + fill_in "From", :with => I18n.l(1.day.from_now, :format => :long) + fill_in "To", :with => I18n.l(2.day.from_now, :format => :long) + click_button "Save" + @e = Refinery::Calendar::Event.find_by_title("Event") + end + + it "succeeds" do + page.should have_content("'Event' was successfully added.") + Refinery::Calendar::Event.count.should eq(1) + end + + it "shows locale flag for event" do + within "#event_#{@e.id}" do + page.should have_css("img[src='/assets/refinery/icons/flags/en.png']") + end + end + + it "shows up in event page for default locale" do + visit refinery.calendar_events_path + page.should have_selector("#event_#{@e.id}") + end + + it "does not show up in event page for secondary locale" do + visit refinery.calendar_events_path(:locale => :es) + page.should_not have_selector("#event_#{@e.id}") + end + end + + describe "add a event with title only for secondary locale" do + + let(:es_event_title) { 'Evento' } + + before do + click_link "Add New Event" + within "#switch_locale_picker" do + click_link "Es" + end + fill_in "Title", :with => es_event_title + fill_in "From", :with => I18n.l(1.day.from_now, :format => :long) + fill_in "To", :with => I18n.l(2.day.from_now, :format => :long) + click_button "Save" + @e = Refinery::Calendar::Event.find_by_title(es_event_title) + end + + it "succeeds" do + page.should have_content("'#{es_event_title}' was successfully added.") + Refinery::Calendar::Event.count.should eq(1) + end + + it "shows title in secondary locale" do + within "#event_#{@e.id}" do + page.should have_content(es_event_title) + end + end + + it "shows locale flag for event" do + within "#event_#{@e.id}" do + page.should have_css("img[src='/assets/refinery/icons/flags/es.png']") + end + end + + it "does not show locale flag for primary locale" do + within "#event_#{@e.id}" do + page.should_not have_css("img[src='/assets/refinery/icons/flags/en.png']") + end + end + + it "does not show up in event page for default locale" do + visit refinery.calendar_events_path + page.should_not have_selector("#event_#{@e.id}") + end + + it "shows up in blog page for secondary locale" do + visit refinery.calendar_events_path(:locale => :es) + page.should have_selector("#event_#{@e.id}") + end + + end + + context "with a event in both locales" do + + let!(:event) do + _event = Globalize.with_locale(:en) { FactoryGirl.create(:event, + :title => "UniqueTitleOne", + :from => 1.day.from_now, + :to => 2.day.from_now) } + Globalize.with_locale(:es) do + _event.title = 'Título unico uno' + _event.save + end + _event + end + + before(:each) do + visit refinery.calendar_admin_events_path + end + + it "shows both locale flags for event" do + within "#event_#{event.id}" do + page.should have_css("img[src='/assets/refinery/icons/flags/en.png']") + page.should have_css("img[src='/assets/refinery/icons/flags/es.png']") + end + end + + describe "edit the event in english" do + it "succeeds" do + + within "#event_#{event.id}" do + click_link("En") + end + current_path.should == refinery.edit_calendar_admin_event_path(event) + fill_in "Title", :with => "New Event Title" + click_button "Save" + + page.should_not have_content(event.title) + page.should have_content("'New Event Title' was successfully updated.") + end + end + + describe "edit the event in secondary locale" do + it "succeeds" do + within "#event_#{event.id}" do + click_link("Es") + end + + fill_in "Title", :with => "Nuevo título del evento" + click_button "Save" + + page.should_not have_content(event.title) + page.should have_content("'Nuevo título del evento' was successfully updated.") + end + end + + end + + end + end end end From 6657f46947a146a3c265ddb82d5509f234c536b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 13:50:14 +0200 Subject: [PATCH 07/10] Added i18n support to venue model. --- app/models/refinery/calendar/venue.rb | 8 ++++++++ db/migrate/4_create_calendar_venues_translations.rb | 13 +++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 db/migrate/4_create_calendar_venues_translations.rb diff --git a/app/models/refinery/calendar/venue.rb b/app/models/refinery/calendar/venue.rb index 41ef5a3..ca870dc 100644 --- a/app/models/refinery/calendar/venue.rb +++ b/app/models/refinery/calendar/venue.rb @@ -1,9 +1,17 @@ module Refinery module Calendar class Venue < Refinery::Core::BaseModel + translates :name has_many :events validates :name, :presence => true, :uniqueness => true attr_accessible :name, :address, :url, :phone, :position + + attr_accessor :locale + + class Translation + attr_accessible :locale + end + end end end diff --git a/db/migrate/4_create_calendar_venues_translations.rb b/db/migrate/4_create_calendar_venues_translations.rb new file mode 100644 index 0000000..56c75ec --- /dev/null +++ b/db/migrate/4_create_calendar_venues_translations.rb @@ -0,0 +1,13 @@ +class CreateCalendarVenuesTranslations < ActiveRecord::Migration + def up + Refinery::Calendar::Venue.create_translation_table!({ + :name => :string + }, { + :migrate_data => true + }) + end + + def down + Refinery::Calendar::Venue.drop_translation_table! :migrate_data => true + end +end \ No newline at end of file From 0eab4f91f52ad9c90438a9a72534d60b9828ee62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 13:50:36 +0200 Subject: [PATCH 08/10] Added i18n support to admin venues controller. --- app/controllers/refinery/calendar/admin/venues_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/refinery/calendar/admin/venues_controller.rb b/app/controllers/refinery/calendar/admin/venues_controller.rb index a6fce1e..9fdf91a 100644 --- a/app/controllers/refinery/calendar/admin/venues_controller.rb +++ b/app/controllers/refinery/calendar/admin/venues_controller.rb @@ -7,7 +7,8 @@ class VenuesController < ::Refinery::AdminController :title_attribute => 'name', :xhr_paging => true, :sortable => false, - :order => 'created_at DESC' + :order => 'created_at DESC', + :include => [:translations] end end end From 97eac014032c39ee1b2ba5d7a421b4d926b3a49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 13:51:20 +0200 Subject: [PATCH 09/10] Added i18n support to admin venues views. --- .../refinery/calendar/admin/venues/_form.html.erb | 1 + .../refinery/calendar/admin/venues/_venue.html.erb | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/views/refinery/calendar/admin/venues/_form.html.erb b/app/views/refinery/calendar/admin/venues/_form.html.erb index 75af72b..8350584 100644 --- a/app/views/refinery/calendar/admin/venues/_form.html.erb +++ b/app/views/refinery/calendar/admin/venues/_form.html.erb @@ -3,6 +3,7 @@ :object => @venue, :include_object_name => true %> + <%= render '/refinery/calendar/admin/shared/locale_picker', :current_locale => Globalize.locale if Refinery.i18n_enabled? %>
    <%= f.label :name -%> diff --git a/app/views/refinery/calendar/admin/venues/_venue.html.erb b/app/views/refinery/calendar/admin/venues/_venue.html.erb index 5a8c4cf..db60196 100644 --- a/app/views/refinery/calendar/admin/venues/_venue.html.erb +++ b/app/views/refinery/calendar/admin/venues/_venue.html.erb @@ -1,7 +1,15 @@
  • - <%= venue.name %> - + <%= venue.name.presence || venue.translations.detect {|t| t.name.present?}.name %> + <% if Refinery.i18n_enabled? and Refinery::I18n.frontend_locales.many? %> + + <% venue.translations.each do |translation| %> + <%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'), + refinery.edit_calendar_admin_venue_path(venue, :switch_locale => translation.locale), + :class => 'locale' if translation.name.present? %> + <% end %> + + <% end %> <%= link_to refinery_icon_tag("application_edit.png"), refinery.edit_calendar_admin_venue_path(venue), From 376298480c27501bf4aa3aac9944956db18b6e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Rodr=C3=ADguez=20Mendo?= Date: Fri, 24 Aug 2012 13:51:41 +0200 Subject: [PATCH 10/10] Testing venues with i18n. --- .../refinery/calendar/admin/venues_spec.rb | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/spec/requests/refinery/calendar/admin/venues_spec.rb b/spec/requests/refinery/calendar/admin/venues_spec.rb index 030a2d5..af76043 100644 --- a/spec/requests/refinery/calendar/admin/venues_spec.rb +++ b/spec/requests/refinery/calendar/admin/venues_spec.rb @@ -95,6 +95,130 @@ end end + context "with translations" do + + before(:each) do + Globalize.locale = :en + Refinery::I18n.stub(:frontend_locales).and_return([:en, :es]) + visit refinery.calendar_admin_venues_path + end + + describe "add a venue with name for default locale" do + + before do + click_link "Add New Venue" + fill_in "Name", :with => "Venue" + click_button "Save" + @v = Refinery::Calendar::Venue.find_by_name("Venue") + end + + it "succeeds" do + page.should have_content("'Venue' was successfully added.") + Refinery::Calendar::Venue.count.should eq(1) + end + + it "shows locale flag for venue" do + within "#venue_#{@v.id}" do + page.should have_css("img[src='/assets/refinery/icons/flags/en.png']") + end + end + + end + + describe "add a venue with name only for secondary locale" do + + let(:es_venue_name) { 'Lugar' } + + before do + click_link "Add New Venue" + within "#switch_locale_picker" do + click_link "Es" + end + fill_in "Name", :with => es_venue_name + click_button "Save" + @v = Refinery::Calendar::Venue.find_by_name(es_venue_name) + end + + it "succeeds" do + page.should have_content("'#{es_venue_name}' was successfully added.") + Refinery::Calendar::Venue.count.should eq(1) + end + + it "shows name in secondary locale" do + within "#venue_#{@v.id}" do + page.should have_content(es_venue_name) + end + end + + it "shows locale flag for venue" do + within "#venue_#{@v.id}" do + page.should have_css("img[src='/assets/refinery/icons/flags/es.png']") + end + end + + it "does not show locale flag for primary locale" do + within "#venue_#{@v.id}" do + page.should_not have_css("img[src='/assets/refinery/icons/flags/en.png']") + end + end + + end + + context "with a venue in both locales" do + + let!(:venue) do + _venue = Globalize.with_locale(:en) { FactoryGirl.create(:venue, :name => "UniqueNameOne") } + Globalize.with_locale(:es) do + _venue.name = 'Nombre' + _venue.save + end + _venue + end + + before(:each) do + visit refinery.calendar_admin_venues_path + end + + it "shows both locale flags for venue" do + within "#venue_#{venue.id}" do + page.should have_css("img[src='/assets/refinery/icons/flags/en.png']") + page.should have_css("img[src='/assets/refinery/icons/flags/es.png']") + end + end + + describe "edit the venue in english" do + + it "succeeds" do + within "#venue_#{venue.id}" do + click_link("En") + end + current_path.should == refinery.edit_calendar_admin_venue_path(venue) + fill_in "Name", :with => "New Venue Name" + click_button "Save" + + page.should_not have_content(venue.name) + page.should have_content("'New Venue Name' was successfully updated.") + end + end + + describe "edit the venue in secondary locale" do + it "succeeds" do + within "#venue_#{venue.id}" do + click_link("Es") + end + + fill_in "Name", :with => "Nuevo nombre del lugar" + click_button "Save" + + page.should_not have_content(venue.name) + page.should have_content("'Nuevo nombre del lugar' was successfully updated.") + end + end + + end + + end + end end end