Skip to content

Commit

Permalink
Rails 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Nov 17, 2010
1 parent f6bac8b commit 06235b7
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 35 deletions.
38 changes: 12 additions & 26 deletions Rakefile
@@ -1,27 +1,16 @@
if ARGV[0].to_s =~ /^rails\d$/
version = ARGV.shift
ENV['BUNDLE_GEMFILE'] = File.expand_path(version + "-Gemfile")
puts "Using #{version}"
exec("rake", *ARGV)
end
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('rails3-Gemfile')

require 'rubygems'
require 'bundler/setup'

unless ENV['BUNDLE_GEMFILE']
$stderr.puts "No Gemfile was configured"
exit(1)
end

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'cucumber/rake/task'
require 'spec/rake/spectask'
require 'yard'

desc 'Default: run specs and cucumber features'
task :default => [:spec, :cucumber]
desc 'Default: run specs and cucumber features on both Rails 2 and 3'
task :default => [:spec, :cucumber, 'cucumber:rails2']

desc 'Test the copycopter_client plugin.'
Spec::Rake::SpecTask.new do |t|
Expand All @@ -30,21 +19,18 @@ Spec::Rake::SpecTask.new do |t|
end

namespace :cucumber do
Cucumber::Rake::Task.new(:ok) do |t|
t.fork = true
t.cucumber_opts = ['--tags', '~@wip',
'--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
desc "Run cucumber features on Rails 2"
task :rails2 do
ENV['BUNDLE_GEMFILE'] = File.expand_path('rails2-Gemfile')
exec("rake cucumber")
end

Cucumber::Rake::Task.new(:wip) do |t|
t.cucumber_opts = ['--tags', '@wip',
'--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
end

task :all => [:ok, :wip]
end

task :cucumber => 'cucumber:ok'
desc "Run cucumber features"
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = ['--tags', '~@wip',
'--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
end

YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'TESTING.rdoc']
Expand Down
8 changes: 7 additions & 1 deletion features/step_definitions/rails_steps.rb
@@ -1,6 +1,12 @@
When "I generate a rails application" do
if Rails::VERSION::MAJOR == 3
subcommand = 'new'
else
subcommand = ''
end

steps %{
When I run "rails _#{Rails::VERSION::STRING}_ testapp"
When I run "rails _#{Rails::VERSION::STRING}_ #{subcommand} testapp"
And I cd to "testapp"
}
end
Expand Down
3 changes: 2 additions & 1 deletion features/support/env.rb
Expand Up @@ -3,5 +3,6 @@
require 'rails/version'

PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
require File.join(PROJECT_ROOT, 'lib', 'copycopter_client', 'version')
$LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
require "copycopter_client/version"

6 changes: 4 additions & 2 deletions lib/copycopter_client/helper.rb
Expand Up @@ -8,7 +8,7 @@ def copy_for(key, default=nil)
default
end

args = [scope_key_by_partial(key), default]
args = [scope_copycopter_key_by_partial(key), default]
introspected_args = args.map { |arg| arg.inspect }.join(', ')
warn("WARNING: #s is deprecated; use t(#{introspected_args}) instead.")
I18n.translate(*args)
Expand All @@ -18,7 +18,9 @@ def copy_for(key, default=nil)
private

def scope_copycopter_key_by_partial(key)
if key.to_s.first == "."
if respond_to?(:scope_key_by_partial, true)
scope_key_by_partial(key)
elsif key.to_s[0].chr == "."
if respond_to?(:template)
"#{template.path_without_format_and_extension.gsub(%r{/_?}, '.')}#{key}"
else
Expand Down
15 changes: 12 additions & 3 deletions lib/copycopter_client/i18n_backend.rb
Expand Up @@ -16,10 +16,11 @@ def reload!

def translate(locale, key, options = {})
content = super(locale, key, options)
if public?
content
html = wrap_with_link_in_private(content, edit_url(locale, key))
if html.respond_to?(:html_safe)
html.html_safe
else
%{#{content} <a href="#{edit_url(locale, key)}" target="_blank">Edit</a>}
html
end
end

Expand All @@ -29,6 +30,14 @@ def available_locales

private

def wrap_with_link_in_private(content, url)
if public?
content
else
%{#{content} <a href="#{url}" target="_blank">Edit</a>}
end
end

def lookup(locale, key, scope = [], options = {})
parts = I18n.normalize_keys(locale, key, scope, options[:separator])
key = parts.join('.')
Expand Down
19 changes: 19 additions & 0 deletions rails3-Gemfile
@@ -0,0 +1,19 @@
source "http://rubygems.org"

gem "rails", "~> 3.0"
gem "sqlite3-ruby", :require => false
gem "rspec"
gem "bourne"
gem "webmock"
gem "rake"
gem "httparty"
gem "sham_rack"
gem "cucumber"
gem "sinatra", :require => false
gem "yard", :require => false
gem "aruba"
gem "ruby-debug"
gem "json"
gem "thin"
gem "i18n"

132 changes: 132 additions & 0 deletions rails3-Gemfile.lock
@@ -0,0 +1,132 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.0)
actionpack (= 3.0.0)
mail (~> 2.2.5)
actionpack (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
activemodel (3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
activeresource (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
activesupport (3.0.0)
addressable (2.2.1)
arel (1.0.1)
activesupport (~> 3.0.0)
aruba (0.2.3)
background_process
cucumber (~> 0.9.0)
background_process (1.2)
bourne (1.0)
mocha (= 0.9.8)
builder (2.1.2)
columnize (0.3.1)
crack (0.1.8)
cucumber (0.9.2)
builder (~> 2.1.2)
diff-lcs (~> 1.1.2)
gherkin (~> 2.2.5)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
daemons (1.1.0)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
eventmachine (0.12.10)
gherkin (2.2.8)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
httparty (0.6.1)
crack (= 0.1.8)
i18n (0.4.1)
json (1.4.6)
linecache (0.43)
mail (2.2.7)
activesupport (>= 2.3.6)
mime-types
treetop (>= 1.4.5)
mime-types (1.16)
mocha (0.9.8)
rake
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.0)
actionmailer (= 3.0.0)
actionpack (= 3.0.0)
activerecord (= 3.0.0)
activeresource (= 3.0.0)
activesupport (= 3.0.0)
bundler (~> 1.0.0)
railties (= 3.0.0)
railties (3.0.0)
actionpack (= 3.0.0)
activesupport (= 3.0.0)
rake (>= 0.8.4)
thor (~> 0.14.0)
rake (0.8.7)
rspec (1.3.0)
ruby-debug (0.10.3)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.3.0)
ruby-debug-base (0.10.3)
linecache (>= 0.3)
sham_rack (1.3.2)
sinatra (1.0)
rack (>= 1.0)
sqlite3-ruby (1.3.1)
term-ansicolor (1.0.5)
thin (1.2.7)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.14.3)
treetop (1.4.8)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
webmock (1.3.5)
addressable (>= 2.1.1)
crack (>= 0.1.7)
yard (0.6.1)

PLATFORMS
ruby

DEPENDENCIES
aruba
bourne
cucumber
httparty
i18n
json
rails (~> 3.0)
rake
rspec
ruby-debug
sham_rack
sinatra
sqlite3-ruby
thin
webmock
yard
15 changes: 13 additions & 2 deletions spec/copycopter_client/helper_spec.rb
Expand Up @@ -29,6 +29,18 @@ def warn(*args); end # these are annoying in test output
should have_translated("test.key", 'default')
end

it "uses existing scope by partial key when present" do
subject.stubs(:scope_key_by_partial => "controller.action.key")
class << subject
private :scope_key_by_partial
end

subject.s(".key")

subject.should have_received(:scope_key_by_partial).with(".key")
should have_translated("controller.action.key", nil)
end

it "should prepend current partial when key starts with . and inside a view" do
template = stub(:path_without_format_and_extension => "controller/action")
subject.stubs(:template => template)
Expand All @@ -48,8 +60,7 @@ def warn(*args); end # these are annoying in test output

describe "default assignment" do
before do
stubs(:scope_copycopter_key_by_partial => '.key')
CopycopterClient.stubs(:copy_for)
subject.stubs(:scope_copycopter_key_by_partial => '.key')
end

it "should allow a hash with key default" do
Expand Down
6 changes: 6 additions & 0 deletions spec/copycopter_client/i18n_backend_spec.rb
Expand Up @@ -57,4 +57,10 @@ def build_backend(config = {})
backend.translate('en', 'test.key', :default => 'default').
should_not include("<a href")
end

it "marks strings as html safe" do
sync['en.test.key'] = FakeHtmlSafeString.new("Hello")
backend = build_backend(:public => true)
backend.translate('en', 'test.key').should be_html_safe
end
end
20 changes: 20 additions & 0 deletions spec/support/fake_html_safe_string.rb
@@ -0,0 +1,20 @@
class FakeHtmlSafeString < String
def initialize(*args)
super(*args)
@html_safe = false
end

def html_safe
dup.html_safe!
end

def html_safe!
@html_safe = true
self
end

def html_safe?
@html_safe
end
end

0 comments on commit 06235b7

Please sign in to comment.