trendi18n 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/app/models/translation.rb +125 -0
- data/generators/trendi18n/templates/migrations/create_translations.rb +22 -0
- data/generators/trendi18n/trendi18n_generator.rb +8 -0
- data/lib/commands.rb +39 -0
- data/lib/file.rb +8 -0
- data/lib/trendi18n.rb +78 -0
- data/rails/init.rb +5 -0
- data/spec/test_application/README +243 -0
- data/spec/test_application/Rakefile +10 -0
- data/spec/test_application/app/controllers/application_controller.rb +10 -0
- data/spec/test_application/app/controllers/translations_controller.rb +41 -0
- data/spec/test_application/app/views/layouts/application.rhtml +15 -0
- data/spec/test_application/app/views/translations/_form.html.erb +11 -0
- data/spec/test_application/app/views/translations/edit.html.erb +6 -0
- data/spec/test_application/app/views/translations/index.html.erb +32 -0
- data/spec/test_application/app/views/translations/new.html.erb +7 -0
- data/spec/test_application/app/views/translations/show.html.erb +5 -0
- data/spec/test_application/config/boot.rb +110 -0
- data/spec/test_application/config/database.yml +25 -0
- data/spec/test_application/config/environment.rb +44 -0
- data/spec/test_application/config/environments/cucumber.rb +28 -0
- data/spec/test_application/config/environments/development.rb +17 -0
- data/spec/test_application/config/environments/production.rb +28 -0
- data/spec/test_application/config/environments/test.rb +34 -0
- data/spec/test_application/config/initializers/new_rails_defaults.rb +21 -0
- data/spec/test_application/config/initializers/session_store.rb +15 -0
- data/spec/test_application/config/locales/en.yml +5 -0
- data/spec/test_application/config/routes.rb +46 -0
- data/spec/test_application/db/migrate/20091208195455_create_translations.rb +22 -0
- data/spec/test_application/db/schema.rb +31 -0
- data/spec/test_application/db/seeds.rb +7 -0
- data/spec/test_application/features/dynamic_translation.feature +5 -0
- data/spec/test_application/features/managing_translations.feature +62 -0
- data/spec/test_application/features/static_translation.feature +13 -0
- data/spec/test_application/features/step_definitions/translations_steps.rb +12 -0
- data/spec/test_application/features/step_definitions/webrat_steps.rb +189 -0
- data/spec/test_application/features/support/env.rb +47 -0
- data/spec/test_application/features/support/paths.rb +42 -0
- data/spec/test_application/features/support/version_check.rb +31 -0
- data/spec/test_application/lib/tasks/cucumber.rake +46 -0
- data/spec/test_application/lib/tasks/rspec.rake +146 -0
- data/spec/test_application/public/404.html +30 -0
- data/spec/test_application/public/422.html +30 -0
- data/spec/test_application/public/500.html +30 -0
- data/spec/test_application/public/favicon.ico +0 -0
- data/spec/test_application/public/robots.txt +5 -0
- data/spec/test_application/script/about +4 -0
- data/spec/test_application/script/autospec +6 -0
- data/spec/test_application/script/console +3 -0
- data/spec/test_application/script/cucumber +17 -0
- data/spec/test_application/script/dbconsole +3 -0
- data/spec/test_application/script/destroy +3 -0
- data/spec/test_application/script/generate +3 -0
- data/spec/test_application/script/performance/benchmarker +3 -0
- data/spec/test_application/script/performance/profiler +3 -0
- data/spec/test_application/script/plugin +3 -0
- data/spec/test_application/script/runner +3 -0
- data/spec/test_application/script/server +3 -0
- data/spec/test_application/script/spec +10 -0
- data/spec/test_application/spec/backend_spec.rb +158 -0
- data/spec/test_application/spec/models/translation_spec.rb +109 -0
- data/spec/test_application/spec/rcov.opts +3 -0
- data/spec/test_application/spec/spec.opts +4 -0
- data/spec/test_application/spec/spec_helper.rb +54 -0
- metadata +156 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Łukasz Piestrzeniewicz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= Introduction
|
2
|
+
|
3
|
+
Trendi18n is database backend for Rails' i18n. Trendi18n offers some administration functionality, which offers:
|
4
|
+
|
5
|
+
* paginated list of translations in database (with locale and translation status conditions)
|
6
|
+
* autoassigned status for translations:
|
7
|
+
* new
|
8
|
+
* unfinished (for translation with many plural forms only)
|
9
|
+
* finished
|
10
|
+
* add new translate
|
11
|
+
* edit existing translate
|
12
|
+
* remove existing translate
|
13
|
+
* looking for translations in database (with locale and translation status conditions)
|
14
|
+
|
15
|
+
Real-time updating store_translations
|
16
|
+
|
17
|
+
= Copyright
|
18
|
+
|
19
|
+
Copyright © 2009 Łukasz Piestrzeniewicz. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
RAILS_ROOT = "spec/test_application"
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'tasks/rails'
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jeweler'
|
11
|
+
Jeweler::Tasks.new do |gemspec|
|
12
|
+
gemspec.name = "trendi18n"
|
13
|
+
gemspec.summary = "Database backend for i18n"
|
14
|
+
gemspec.description = "Database backend for i18n (localization files are still supported). This is beta version so give me your feedback"
|
15
|
+
gemspec.email = "p.misiurek@gmail.com"
|
16
|
+
gemspec.authors = ["Piotr Misiurek", "Piotr Marciniak", "Łukasz Piestrzeniewicz"]
|
17
|
+
files = FileList["[A-Z]*", "{generators,lib,spec,app,rails}/**/*"]
|
18
|
+
files.exclude "spec/test_application/coverage/*"
|
19
|
+
files.exclude "spec/test_application/tmp/*"
|
20
|
+
files.exclude "spec/test_application/nbproject/*"
|
21
|
+
files.exclude "spec/test_application/log/*"
|
22
|
+
files.exclude "spec/test_applcation/test/*"
|
23
|
+
files.exclude "spec/test_application/db/*sqlite3"
|
24
|
+
gemspec.files = files.to_a
|
25
|
+
gemspec.homepage = "http://github.com/bragi/trendi18n"
|
26
|
+
gemspec.add_dependency("rails", ">= 2.3.5")
|
27
|
+
end
|
28
|
+
rescue LoadError
|
29
|
+
puts "Jeweler not available"
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
desc 'Default: run unit tests.'
|
34
|
+
task :default => :test
|
35
|
+
|
36
|
+
desc 'Test the trendi18n plugin.'
|
37
|
+
Rake::TestTask.new(:test) do |t|
|
38
|
+
t.libs << 'lib'
|
39
|
+
t.libs << 'test'
|
40
|
+
t.pattern = 'test/**/*_test.rb'
|
41
|
+
t.verbose = true
|
42
|
+
end
|
43
|
+
|
44
|
+
desc 'Generate documentation for the trendi18n plugin.'
|
45
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = 'Trendi18n'
|
48
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
49
|
+
rdoc.rdoc_files.include('README.rdoc')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.9.1
|
@@ -0,0 +1,125 @@
|
|
1
|
+
class Translation < ActiveRecord::Base
|
2
|
+
before_validation :set_state
|
3
|
+
before_validation :set_defaults
|
4
|
+
after_save :reload_backend
|
5
|
+
|
6
|
+
|
7
|
+
named_scope :untranslated, :conditions => {:state => %w(new unfinished)}
|
8
|
+
named_scope :translated, :conditions => {:state => "finished"}
|
9
|
+
named_scope :localization, lambda {|*args| args.first ? {:conditions => {:locale => args.first}} : nil }
|
10
|
+
|
11
|
+
validates_presence_of :key
|
12
|
+
validates_length_of :locale, :within => 2..6
|
13
|
+
validates_uniqueness_of :key, :scope => %w(locale scope)
|
14
|
+
|
15
|
+
@@locales = []
|
16
|
+
|
17
|
+
# return locales if their exists and translations are up-to-date. In other case assign are locales values from db to locales and return this value
|
18
|
+
def self.get_locales
|
19
|
+
@@locales
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.set_locales
|
23
|
+
@@locales = self.all(:select => "DISTINCT(locale)", :order => "locale ASC").map { |obj| obj.locale }
|
24
|
+
end
|
25
|
+
|
26
|
+
# This method is running after save translation into db and it is reloading I18n backend (trendi18n)
|
27
|
+
def reload_backend
|
28
|
+
I18n.backend.reload!
|
29
|
+
Translation.set_locales
|
30
|
+
end
|
31
|
+
|
32
|
+
# auto-magic finder using string normalized key to find translation. The first value in scope is the locale, the last is the key and all between are scopes
|
33
|
+
def self.find_by_string_normalized_key(key)
|
34
|
+
scope = I18n.send(:normalize_translation_keys, I18n.locale, key, nil).map(&:to_s)
|
35
|
+
self.first(:conditions => {:key => scope.last, :locale => scope.first, :scope => scope[1..-2].empty? ? nil : scope[1..-2].join(".") }) || raise(ActiveRecord::RecordNotFound, "Could not find translation with key #{key}")
|
36
|
+
end
|
37
|
+
|
38
|
+
# Assign state of translation
|
39
|
+
# - if there is no count in the key, set new if there is not translation and set finished when translation exists
|
40
|
+
# - if there is count in the key, then:
|
41
|
+
# * if translation has all plural forms, assign finished
|
42
|
+
# * if translation has some plural forms, then assign unfinished
|
43
|
+
# * if translation does not have plural forms, then assign new
|
44
|
+
def set_state
|
45
|
+
self.state = self.with_count? ? self.has_all_plural_forms? ? "finished" : self.has_some_plural_forms? ? "unfinished" : "new" : self.translation.blank? ? "new" : "finished"
|
46
|
+
end
|
47
|
+
|
48
|
+
# assign default values to translation
|
49
|
+
def set_defaults
|
50
|
+
self.locale = I18n.locale.to_s if self.locale.blank? # if translation does not have locale, set I18n default
|
51
|
+
self.default = key unless self.default # set key to default if it is not exists
|
52
|
+
self.scope = nil if self.scope.blank? #set nil to scope if it is blank (empty string)
|
53
|
+
self.translation = nil if self.translation.blank? # set nil to translation if it is blank (empty string)
|
54
|
+
end
|
55
|
+
|
56
|
+
# time of the last db update
|
57
|
+
def self.base_updated_at
|
58
|
+
self.exists?(["updated_at > created_at"]) ? self.first(:order => "updated_at DESC", :conditions => "updated_at > created_at").updated_at : Time.at(0)
|
59
|
+
end
|
60
|
+
|
61
|
+
# time of the db read on current db update
|
62
|
+
def self.read_base
|
63
|
+
@base_read_at = Time.zone.now if @base_read_at.nil?
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.clear_base_read_at
|
67
|
+
@base_read_at = nil
|
68
|
+
end
|
69
|
+
|
70
|
+
# db is up-to-date when it wasn't read or read time is larger then update time
|
71
|
+
def self.up_to_date?
|
72
|
+
@base_read_at.nil? || @base_read_at.to_i > self.base_updated_at.to_i
|
73
|
+
end
|
74
|
+
|
75
|
+
# don't be nil if there is "{{count}}" in key
|
76
|
+
def with_count?
|
77
|
+
/\{\{count\}\}/.match(self.key)
|
78
|
+
end
|
79
|
+
|
80
|
+
# true if translation has all plural forms
|
81
|
+
def has_all_plural_forms?
|
82
|
+
!self.zero.blank? && !self.one.blank? && !self.many.blank?
|
83
|
+
end
|
84
|
+
|
85
|
+
# get correct pluralization form (using count)
|
86
|
+
def pluralize(count)
|
87
|
+
if count
|
88
|
+
case count
|
89
|
+
when 0 then self.zero
|
90
|
+
when 1 then self.one
|
91
|
+
else self.many
|
92
|
+
end || self.default
|
93
|
+
else
|
94
|
+
self.translation || self.default
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# true if translation has some plural forms
|
99
|
+
def has_some_plural_forms?
|
100
|
+
!self.zero.blank? || !self.one.blank? || !self.many.blank?
|
101
|
+
end
|
102
|
+
|
103
|
+
# look up for translation and return it, if exists, or create if not
|
104
|
+
def self.lookup(locale, key, default = nil, scope = nil)
|
105
|
+
self.read_base
|
106
|
+
scope = I18n.send(:normalize_translation_keys, locale, key, scope)
|
107
|
+
locale = scope.delete_at(0).to_s
|
108
|
+
key = scope.delete_at(scope.size - 1).to_s
|
109
|
+
scope = scope.empty? ? nil : scope.join(".")
|
110
|
+
if self.exists?(:locale => locale, :scope => scope, :key => key)
|
111
|
+
translation = self.first(:conditions => {:locale => locale, :scope => scope, :key => key})
|
112
|
+
else
|
113
|
+
translation = self.new(:key => key, :default => default.nil? ? key : default, :locale => locale, :scope => scope)
|
114
|
+
translation.save
|
115
|
+
end
|
116
|
+
translation
|
117
|
+
end
|
118
|
+
|
119
|
+
# return hash ready to by stored as translation
|
120
|
+
def to_translation_hash
|
121
|
+
path = self.scope.nil? ? [self.key] : self.scope.split(".") << self.key
|
122
|
+
return path.reverse.inject(self) {|before, step| { step => before}}
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class CreateTranslations < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table ActiveRecord::Base.pluralize_table_names ? :translations : :translation do |t|
|
4
|
+
t.string :locale, :null => false, :limit => 6
|
5
|
+
t.string :key, :null => false
|
6
|
+
t.string :scope
|
7
|
+
t.text :default, :null => false
|
8
|
+
t.text :translation
|
9
|
+
t.text :zero
|
10
|
+
t.text :one
|
11
|
+
t.text :many
|
12
|
+
t.text :few
|
13
|
+
t.string :state, :null => false
|
14
|
+
t.timestamps
|
15
|
+
end
|
16
|
+
add_index :translations, [:locale, :key, :scope], :unique => true
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table ActiveRecord::Base.pluralize_table_names ? :translations : :translation
|
21
|
+
end
|
22
|
+
end
|
data/lib/commands.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "rails_generator"
|
2
|
+
require "rails_generator/commands"
|
3
|
+
|
4
|
+
module Trendi18n
|
5
|
+
module Generator
|
6
|
+
module Commands
|
7
|
+
|
8
|
+
module Create
|
9
|
+
|
10
|
+
def installer
|
11
|
+
puts "Creating migration file..."
|
12
|
+
migration_template(File.join("migrations", "create_translations.rb"), File.join("db", "migrate"),
|
13
|
+
:migration_file_name => ActiveRecord::Base.pluralize_table_names ? "create_translations" : "create_translation")
|
14
|
+
puts "Inserting migration into db..."
|
15
|
+
version = File.get_migration_version_from_file_name(ActiveRecord::Base.pluralize_table_names ? /create_translations\.rb$/ : /create_translation\.rb$/)
|
16
|
+
system "rake db:migrate:up VERSION=#{version}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
module Destroy
|
22
|
+
|
23
|
+
def installer
|
24
|
+
puts "Removing migration from db..."
|
25
|
+
version = File.get_migration_version_from_file_name(ActiveRecord::Base.pluralize_table_names ? /create_translations\.rb$/ : /create_translation\.rb$/)
|
26
|
+
system "rake db:migrate:down VERSION=#{version}"
|
27
|
+
puts "Removing migration file..."
|
28
|
+
migration_template(File.join("migrations", "create_translations.rb"), File.join("db", "migrate"),
|
29
|
+
:migration_file_name => ActiveRecord::Base.pluralize_table_names ? "create_translations" : "create_translation")
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Rails::Generator::Commands::Create.send :include, Trendi18n::Generator::Commands::Create
|
39
|
+
Rails::Generator::Commands::Destroy.send :include, Trendi18n::Generator::Commands::Destroy
|
data/lib/file.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
class File
|
2
|
+
|
3
|
+
# get migration version from filename matching to regexp
|
4
|
+
def self.get_migration_version_from_file_name(migration_name_regexp)
|
5
|
+
Dir[File.join("db", "migrate", "*.rb")].each { |file| return File.basename(file).split("_")[0].to_i if migration_name_regexp.match(File.basename(file)) }
|
6
|
+
end
|
7
|
+
|
8
|
+
end
|
data/lib/trendi18n.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
|
2
|
+
module Trendi18n
|
3
|
+
module Backend
|
4
|
+
|
5
|
+
class Trendi18n < I18n::Backend::Simple
|
6
|
+
delegate :up_to_date?, :to => Translation # delgate up_to_date? method to Translation model
|
7
|
+
|
8
|
+
# return available locales, based on informaton form Translation model
|
9
|
+
def available_locales
|
10
|
+
Translation.get_locales
|
11
|
+
end
|
12
|
+
|
13
|
+
# translate key in locale using options
|
14
|
+
def translate(locale, key, options = {})
|
15
|
+
raise InvalidLocale.new(locale) if locale.nil?
|
16
|
+
|
17
|
+
#if key is an array then as a result run yourself for all subkey in key
|
18
|
+
return key.map { |subkey| translate(locale, subkey, options)} if key.is_a?(Array)
|
19
|
+
|
20
|
+
# read count, scope and default from options
|
21
|
+
count, scope, default = options.values_at(:count, :scope, :default)
|
22
|
+
options.delete(:default) #delete default from option
|
23
|
+
|
24
|
+
# read values of scope and default options and delete them form options
|
25
|
+
values = options.reject {|name, value| [:scope, :default].include?(name)}
|
26
|
+
|
27
|
+
if key.is_a?(Symbol) # if key is a Symbol
|
28
|
+
begin
|
29
|
+
return nested.translate(locale, key, options) # use nested translete
|
30
|
+
rescue
|
31
|
+
I18n::MissingTranslationData # if its exists
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
entry = lookup(locale, key, default, scope) # lookup for translation
|
36
|
+
entry = entry.pluralize(count) # run pluralization for translation
|
37
|
+
entry = interpolate(locale, entry, values) # run interpolation for translation
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def reload!
|
42
|
+
super # run standard I18n::Backend::Simple reload! method
|
43
|
+
Translation.clear_base_read_at # and clear information about time of last translation's base read
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def init_translations
|
49
|
+
# only set @initialized. Translations will be caching in real-time
|
50
|
+
@initialized = true
|
51
|
+
Translation.set_locales
|
52
|
+
end
|
53
|
+
|
54
|
+
# look up for translation. When find cache it and return
|
55
|
+
def lookup(locale, key, default, scope)
|
56
|
+
# cache and return translation. Translation can be find by:
|
57
|
+
# - standard I18n::Backend::Simple.lookup method. If its failed, then:
|
58
|
+
# - Translation model lookup method
|
59
|
+
cache_translation(translation = Translation.lookup(locale, key, default, scope)) unless translation = super(locale, key, scope)
|
60
|
+
translation
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# assign new instance of I18n::Backend::Simple for @nested if its not exists
|
67
|
+
def nested
|
68
|
+
@nested ||= I18n::Backend::Simple.new
|
69
|
+
end
|
70
|
+
|
71
|
+
# add translation to stored
|
72
|
+
def cache_translation(translation)
|
73
|
+
store_translations(translation.locale, translation.to_translation_hash)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
== Welcome to Rails
|
2
|
+
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
5
|
+
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb" templates
|
7
|
+
that are primarily responsible for inserting pre-built data in between HTML tags.
|
8
|
+
The model contains the "smart" domain objects (such as Account, Product, Person,
|
9
|
+
Post) that holds all the business logic and knows how to persist themselves to
|
10
|
+
a database. The controller handles the incoming requests (such as Save New Account,
|
11
|
+
Update Product, Show Post) by manipulating the model and directing data to the view.
|
12
|
+
|
13
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
14
|
+
layer entitled Active Record. This layer allows you to present the data from
|
15
|
+
database rows as objects and embellish these data objects with business logic
|
16
|
+
methods. You can read more about Active Record in
|
17
|
+
link:files/vendor/rails/activerecord/README.html.
|
18
|
+
|
19
|
+
The controller and view are handled by the Action Pack, which handles both
|
20
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
21
|
+
are bundled in a single package due to their heavy interdependence. This is
|
22
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
23
|
+
more separate. Each of these packages can be used independently outside of
|
24
|
+
Rails. You can read more about Action Pack in
|
25
|
+
link:files/vendor/rails/actionpack/README.html.
|
26
|
+
|
27
|
+
|
28
|
+
== Getting Started
|
29
|
+
|
30
|
+
1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
|
31
|
+
and your application name. Ex: rails myapp
|
32
|
+
2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
|
33
|
+
3. Go to http://localhost:3000/ and get "Welcome aboard: You're riding the Rails!"
|
34
|
+
4. Follow the guidelines to start developing your application
|
35
|
+
|
36
|
+
|
37
|
+
== Web Servers
|
38
|
+
|
39
|
+
By default, Rails will try to use Mongrel if it's are installed when started with script/server, otherwise Rails will use WEBrick, the webserver that ships with Ruby. But you can also use Rails
|
40
|
+
with a variety of other web servers.
|
41
|
+
|
42
|
+
Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
|
43
|
+
suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
|
44
|
+
getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
|
45
|
+
More info at: http://mongrel.rubyforge.org
|
46
|
+
|
47
|
+
Say other Ruby web servers like Thin and Ebb or regular web servers like Apache or LiteSpeed or
|
48
|
+
Lighttpd or IIS. The Ruby web servers are run through Rack and the latter can either be setup to use
|
49
|
+
FCGI or proxy to a pack of Mongrels/Thin/Ebb servers.
|
50
|
+
|
51
|
+
== Apache .htaccess example for FCGI/CGI
|
52
|
+
|
53
|
+
# General Apache options
|
54
|
+
AddHandler fastcgi-script .fcgi
|
55
|
+
AddHandler cgi-script .cgi
|
56
|
+
Options +FollowSymLinks +ExecCGI
|
57
|
+
|
58
|
+
# If you don't want Rails to look in certain directories,
|
59
|
+
# use the following rewrite rules so that Apache won't rewrite certain requests
|
60
|
+
#
|
61
|
+
# Example:
|
62
|
+
# RewriteCond %{REQUEST_URI} ^/notrails.*
|
63
|
+
# RewriteRule .* - [L]
|
64
|
+
|
65
|
+
# Redirect all requests not available on the filesystem to Rails
|
66
|
+
# By default the cgi dispatcher is used which is very slow
|
67
|
+
#
|
68
|
+
# For better performance replace the dispatcher with the fastcgi one
|
69
|
+
#
|
70
|
+
# Example:
|
71
|
+
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
|
72
|
+
RewriteEngine On
|
73
|
+
|
74
|
+
# If your Rails application is accessed via an Alias directive,
|
75
|
+
# then you MUST also set the RewriteBase in this htaccess file.
|
76
|
+
#
|
77
|
+
# Example:
|
78
|
+
# Alias /myrailsapp /path/to/myrailsapp/public
|
79
|
+
# RewriteBase /myrailsapp
|
80
|
+
|
81
|
+
RewriteRule ^$ index.html [QSA]
|
82
|
+
RewriteRule ^([^.]+)$ $1.html [QSA]
|
83
|
+
RewriteCond %{REQUEST_FILENAME} !-f
|
84
|
+
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
|
85
|
+
|
86
|
+
# In case Rails experiences terminal errors
|
87
|
+
# Instead of displaying this message you can supply a file here which will be rendered instead
|
88
|
+
#
|
89
|
+
# Example:
|
90
|
+
# ErrorDocument 500 /500.html
|
91
|
+
|
92
|
+
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
|
93
|
+
|
94
|
+
|
95
|
+
== Debugging Rails
|
96
|
+
|
97
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
98
|
+
will help you debug it and get it back on the rails.
|
99
|
+
|
100
|
+
First area to check is the application log files. Have "tail -f" commands running
|
101
|
+
on the server.log and development.log. Rails will automatically display debugging
|
102
|
+
and runtime information to these files. Debugging info will also be shown in the
|
103
|
+
browser on requests from 127.0.0.1.
|
104
|
+
|
105
|
+
You can also log your own messages directly into the log file from your code using
|
106
|
+
the Ruby logger class from inside your controllers. Example:
|
107
|
+
|
108
|
+
class WeblogController < ActionController::Base
|
109
|
+
def destroy
|
110
|
+
@weblog = Weblog.find(params[:id])
|
111
|
+
@weblog.destroy
|
112
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
The result will be a message in your log file along the lines of:
|
117
|
+
|
118
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
|
119
|
+
|
120
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
121
|
+
|
122
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
|
123
|
+
|
124
|
+
* The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
|
125
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
126
|
+
|
127
|
+
These two online (and free) books will bring you up to speed on the Ruby language
|
128
|
+
and also on programming in general.
|
129
|
+
|
130
|
+
|
131
|
+
== Debugger
|
132
|
+
|
133
|
+
Debugger support is available through the debugger command when you start your Mongrel or
|
134
|
+
Webrick server with --debugger. This means that you can break out of execution at any point
|
135
|
+
in the code, investigate and change the model, AND then resume execution!
|
136
|
+
You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
|
137
|
+
Example:
|
138
|
+
|
139
|
+
class WeblogController < ActionController::Base
|
140
|
+
def index
|
141
|
+
@posts = Post.find(:all)
|
142
|
+
debugger
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
So the controller will accept the action, run the first line, then present you
|
147
|
+
with a IRB prompt in the server window. Here you can do things like:
|
148
|
+
|
149
|
+
>> @posts.inspect
|
150
|
+
=> "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
|
151
|
+
#<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
|
152
|
+
>> @posts.first.title = "hello from a debugger"
|
153
|
+
=> "hello from a debugger"
|
154
|
+
|
155
|
+
...and even better is that you can examine how your runtime objects actually work:
|
156
|
+
|
157
|
+
>> f = @posts.first
|
158
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
159
|
+
>> f.
|
160
|
+
Display all 152 possibilities? (y or n)
|
161
|
+
|
162
|
+
Finally, when you're ready to resume execution, you enter "cont"
|
163
|
+
|
164
|
+
|
165
|
+
== Console
|
166
|
+
|
167
|
+
You can interact with the domain model by starting the console through <tt>script/console</tt>.
|
168
|
+
Here you'll have all parts of the application configured, just like it is when the
|
169
|
+
application is running. You can inspect domain models, change values, and save to the
|
170
|
+
database. Starting the script without arguments will launch it in the development environment.
|
171
|
+
Passing an argument will specify a different environment, like <tt>script/console production</tt>.
|
172
|
+
|
173
|
+
To reload your controllers and models after launching the console run <tt>reload!</tt>
|
174
|
+
|
175
|
+
== dbconsole
|
176
|
+
|
177
|
+
You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
|
178
|
+
You would be connected to the database with the credentials defined in database.yml.
|
179
|
+
Starting the script without arguments will connect you to the development database. Passing an
|
180
|
+
argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
|
181
|
+
Currently works for mysql, postgresql and sqlite.
|
182
|
+
|
183
|
+
== Description of Contents
|
184
|
+
|
185
|
+
app
|
186
|
+
Holds all the code that's specific to this particular application.
|
187
|
+
|
188
|
+
app/controllers
|
189
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
190
|
+
automated URL mapping. All controllers should descend from ApplicationController
|
191
|
+
which itself descends from ActionController::Base.
|
192
|
+
|
193
|
+
app/models
|
194
|
+
Holds models that should be named like post.rb.
|
195
|
+
Most models will descend from ActiveRecord::Base.
|
196
|
+
|
197
|
+
app/views
|
198
|
+
Holds the template files for the view that should be named like
|
199
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby
|
200
|
+
syntax.
|
201
|
+
|
202
|
+
app/views/layouts
|
203
|
+
Holds the template files for layouts to be used with views. This models the common
|
204
|
+
header/footer method of wrapping views. In your views, define a layout using the
|
205
|
+
<tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
|
206
|
+
call <% yield %> to render the view using this layout.
|
207
|
+
|
208
|
+
app/helpers
|
209
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are generated
|
210
|
+
for you automatically when using script/generate for controllers. Helpers can be used to
|
211
|
+
wrap functionality for your views into methods.
|
212
|
+
|
213
|
+
config
|
214
|
+
Configuration files for the Rails environment, the routing map, the database, and other dependencies.
|
215
|
+
|
216
|
+
db
|
217
|
+
Contains the database schema in schema.rb. db/migrate contains all
|
218
|
+
the sequence of Migrations for your schema.
|
219
|
+
|
220
|
+
doc
|
221
|
+
This directory is where your application documentation will be stored when generated
|
222
|
+
using <tt>rake doc:app</tt>
|
223
|
+
|
224
|
+
lib
|
225
|
+
Application specific libraries. Basically, any kind of custom code that doesn't
|
226
|
+
belong under controllers, models, or helpers. This directory is in the load path.
|
227
|
+
|
228
|
+
public
|
229
|
+
The directory available for the web server. Contains subdirectories for images, stylesheets,
|
230
|
+
and javascripts. Also contains the dispatchers and the default HTML files. This should be
|
231
|
+
set as the DOCUMENT_ROOT of your web server.
|
232
|
+
|
233
|
+
script
|
234
|
+
Helper scripts for automation and generation.
|
235
|
+
|
236
|
+
test
|
237
|
+
Unit and functional tests along with fixtures. When using the script/generate scripts, template
|
238
|
+
test files will be generated for you and placed in this directory.
|
239
|
+
|
240
|
+
vendor
|
241
|
+
External libraries that the application depends on. Also includes the plugins subdirectory.
|
242
|
+
If the app has frozen rails, those gems also go here, under vendor/rails/.
|
243
|
+
This directory is in the load path.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
|
5
|
+
|
6
|
+
require 'rake'
|
7
|
+
require 'rake/testtask'
|
8
|
+
require 'rake/rdoctask'
|
9
|
+
|
10
|
+
require 'tasks/rails'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Filters added to this controller apply to all controllers in the application.
|
2
|
+
# Likewise, all the methods added will be available for all controllers.
|
3
|
+
|
4
|
+
class ApplicationController < ActionController::Base
|
5
|
+
helper :all # include all helpers, all the time
|
6
|
+
protect_from_forgery # See ActionController::RequestForgeryProtection for details
|
7
|
+
|
8
|
+
# Scrub sensitive parameters from your log
|
9
|
+
# filter_parameter_logging :password
|
10
|
+
end
|