履歴: ソフトウェア/rails/言語ネゴシエーション
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 21 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2014/5/20 19:20:23 | ソフトウェア/rails/言語ネゴシエーション 最新版 | 3,891 | +1 | -1 | 表示 | 差分 | — |
| 2014/5/20 19:20:23 | ソフトウェア/rails/言語ネゴシエーション | 3,897 | +8 | -1 | 表示 | 差分 | 選択 |
| 2013/1/5 18:30:15 | ソフトウェア/rails/言語ネゴシエーション | 3,842 | +3 | -0 | 表示 | 差分 | 選択 |
| 2009/7/14 9:35:50 | ソフトウェア/rails/言語ネゴシエーション | 3,732 | +43 | -27 | 表示 | 差分 | 選択 |
| 2009/7/11 15:06:20 | ソフトウェア/rails/言語ネゴシエーション | 3,727 | +9 | -0 | 表示 | 差分 | 選択 |
| 2009/6/28 2:02:38 | ソフトウェア/rails/言語ネゴシエーション | 3,515 | +2 | -0 | 表示 | 差分 | 選択 |
| 2009/6/27 8:15:42 | ソフトウェア/rails/言語ネゴシエーション | 3,458 | +4 | -0 | 表示 | 差分 | 選択 |
| 2009/6/21 9:14:00 | ソフトウェア/rails/言語ネゴシエーション | 3,403 | +15 | -0 | 表示 | 差分 | 選択 |
| 2009/6/21 6:41:27 | ソフトウェア/rails/言語ネゴシエーション | 3,126 | +62 | -2793 | 表示 | 差分 | 選択 |
| 2009/6/10 11:14:13 | ソフトウェア/rails/言語ネゴシエーション | 84,051 | +327 | -1 | 表示 | 表示中 | 選択 |
| 2009/6/10 3:14:21 | ソフトウェア/rails/言語ネゴシエーション | 73,730 | +459 | -3 | 表示 | 差分 | 選択 |
| 2009/6/9 15:42:54 | ソフトウェア/rails/言語ネゴシエーション | 58,379 | +247 | -473 | 表示 | 差分 | 選択 |
| 2009/6/9 11:29:37 | ソフトウェア/rails/言語ネゴシエーション | 66,074 | +317 | -40 | 表示 | 差分 | 基準 |
| 2009/6/8 20:22:16 | ソフトウェア/rails/言語ネゴシエーション | 58,607 | +75 | -2 | 表示 | 差分 | 選択 |
| 2009/6/8 15:18:39 | ソフトウェア/rails/言語ネゴシエーション | 55,917 | +958 | -223 | 表示 | 差分 | 選択 |
| 2009/6/8 8:00:44 | ソフトウェア/rails/言語ネゴシエーション | 33,858 | +247 | -35 | 表示 | 差分 | 選択 |
| 2009/6/7 13:55:06 | ソフトウェア/rails/言語ネゴシエーション | 28,314 | +195 | -18 | 表示 | 差分 | 選択 |
| 2009/6/7 4:15:30 | ソフトウェア/rails/言語ネゴシエーション | 23,332 | +140 | -3 | 表示 | 差分 | 選択 |
| 2009/6/6 14:50:13 | ソフトウェア/rails/言語ネゴシエーション | 19,622 | +30 | -27 | 表示 | 差分 | 選択 |
| 2009/6/6 6:50:47 | ソフトウェア/rails/言語ネゴシエーション | 19,701 | +581 | -3 | 表示 | 差分 | 選択 |
| 2009/6/6 2:59:56 | ソフトウェア/rails/言語ネゴシエーション | 2,869 | — | — | 表示 | — | 選択 |
基準: ソフトウェア/rails/言語ネゴシエーション (2009/6/9 11:29:37) → 比較: ソフトウェア/rails/言語ネゴシエーション (2009/6/10 11:14:13)
@@ -91,7 +91,7 @@
Action Mailer version 2.2.2
Active Support version 2.2.2
Edge Rails revision unknown
- Application root /home/samba/www/rails/negotiation
+ Application root (rails)/negotiation
Environment development
Database adapter sqlite3
Database schema version 0
@@ -936,12 +936,12 @@
日本語: テスト ばっちり
$ tail /var/log/apache2/rewrite.log
- [perdir /home/samba/www/negotiation/] pass through /home/samba/www/negotiation/test.html.ja
+ [perdir (DOCROOT)/negotiation/] pass through (DOCROOT)/negotiation/test.html.ja
$ wgetc http://localhost/negotiation/test.ja
日本語: テスト ばっちり
$ tail /var/log/apache2/rewrite.log
- [perdir /home/samba/www/negotiation/] pass through /home/samba/www/negotiation/dispatch.cgi
+ [perdir (DOCROOT)/negotiation/] pass through (DOCROOT)/negotiation/dispatch.cgi
$ wget -O- --header="Accept-Language: en" http://localhost/negotiation/test
エラー 406: Not Acceptable
$ wgetc http://localhost/negotiation/test.en
@@ -953,7 +953,7 @@
日本語: テスト ばっちり
$ tail /var/log/apache2/rewrite.log
- [perdir /home/samba/www/negotiation/] pass through /home/samba/www/negotiation/test.html.ja
+ [perdir (DOCROOT)/negotiation/] pass through (DOCROOT)/negotiation/test.html.ja
$ wget -qO- --header="Accept-Language: en" http://localhost/negotiation/test
English: Test OK
@@ -1162,7 +1162,7 @@
English: Test OK
$ tail /var/log/apache2/rewrite.log
- initial URL equal rewritten URL: /home/samba/www/negotiation/test.html.en
+ initial URL equal rewritten URL: (DOCROOT)/negotiation/test.html.en
$ wget -qO- --header="Accept: text/html" --header="Accept-Language: ja" http://localhost/negotiation/test
日本語: テスト ばっちり
@@ -1172,10 +1172,10 @@
日本語: テスト ばっちり
$ tail /var/log/apache2/rewrite.log
- initial URL equal rewritten URL: /home/samba/www/negotiation/test.html.ja
+ initial URL equal rewritten URL: (DOCROOT)/negotiation/test.html.ja
$ wget -qO- --header="Accept: text/html" --header="Accept-Language: en" http://localhost/negotiation/test
$ tail /var/log/apache2/rewrite.log
- initial URL equal rewritten URL: /home/samba/www/negotiation/test.html.en
+ initial URL equal rewritten URL: (DOCROOT)/negotiation/test.html.en
$ rm public/test*
$ wget -qO- --header="Accept: text/html" http://localhost/negotiation/test.ja
日本語: テスト ばっちり
@@ -1186,7 +1186,7 @@
日本語: テスト ばっちり
$ tail /var/log/apache2/rewrite.log
- initial URL equal rewritten URL: /home/samba/www/negotiation/test.html.ja
+ initial URL equal rewritten URL: (DOCROOT)/negotiation/test.html.ja
$ wget -qO- --header="Accept: text/html" http://localhost/negotiation/test.en
English: Test OK
@@ -1434,405 +1434,6 @@
うまく行っているようだ。
-* ここまでのコード [#o65761f9]
-
-以下にここまでのコードをまとめておく。
-
-public/.htaccess
- LANG:shell(linenumber)
- # rails setting
- SetEnv RAILS_RELATIVE_URL_ROOT /negotiation
- SetEnv RAILS_ENV production
- SetEnv RAILS_ACCEPTABLE_LANGUAGES ja|en
-
- # save preferential language in cookie
- SetEnvIf Cookie "rails_language=([a-z][a-z])" prefer-language=$1
-
- # avoid error due to cache miss
- LanguagePriority ja en
- ForceLanguagePriority Prefer Fallback
-
- # rewrite start
- RewriteEngine On
-
- # default format of ".html"
- RewriteCond %{HTTP_ACCEPT} html
- RewriteCond %{SCRIPT_FILENAME} !\.html\.
- RewriteCond %{SCRIPT_FILENAME} (.*)\.([a-z][a-z])$
- RewriteCond %1.html.%2 -f
- RewriteRule (.*)\.([a-z][a-z])$ $1.html.$2 [L]
-
- # rails' default rules
- RewriteRule ^$ index.html [QSA]
- RewriteRule ^([^.]+)$ $1.html [QSA]
- RewriteCond %{REQUEST_FILENAME} !-f
- # change this to adapt to your server
- RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
-
- # detect cache miss for prefer-language
- RewriteCond %{ENV:prefer-language} ^[a-z][a-z]$
- RewriteCond %{ENV:prefer-language} !=$2
- RewriteRule (.*\.html\.)([a-z][a-z]) $1%{ENV:prefer-language} [L]
-
- # detect cache miss for accept-language
- # omit this section if performance matters
- RewriteCond %{HTTP:Accept-Language} !$2 [NC]
- RewriteCond %{HTTP:Accept-Language} ([a-z][a-z])
- RewriteRule (.*\.html\.)([a-z][a-z]) $1%1
-
-config/routes.rb
- LANG:ruby(linenumber)
- ENV['RAILS_ACCEPTABLE_LANGUAGES'] ||= 'ja|en'
-
- ActionController::Routing::Routes.draw do |map|
-
- lang_regexp= Regexp.new( ENV['RAILS_ACCEPTABLE_LANGUAGES'] )
-
- map.connect ':controller.:rails_language', :format => 'html', :action => 'index',
- :requirements => { :rails_language => lang_regexp }
- map.connect ':controller/:action.:rails_language', :format => 'html',
- :requirements => { :rails_language => lang_regexp }
- map.connect ':controller/:action/:id.:rails_language', :format => 'html',
- :requirements => { :rails_language => lang_regexp }
- map.connect ':controller/:action/:id', :format => 'html'
-
- map.connect ':controller/:action/:id.:format.:rails_language',
- :requirements => { :rails_language => lang_regexp }
- map.connect ':controller/:action/:id.:format',
- :defaults => { :action => 'index', :format => 'html' }
-
- end
-
-config/environment.rb
- LANG:ruby(linenumber)
- #### acceptable languages
-
- class ActionController::AbstractRequest
-
- def self.acceptable_languages
- @acceptable_languages ||=
- ENV['RAILS_ACCEPTABLE_LANGUAGES'].split('|').map {|l| l.to_sym}
- end
-
- def self.acceptable_language?(l)
- acceptable_languages.include?(l.to_sym)
- end
-
- end
-
- #### language preference order
-
- class ActionController::AbstractRequest
-
- # will be set by ActionView::Base._pick_template
- attr :language, true
-
- def accepts_languages!(language=nil)
- @accepts_languages= [
- language ? language.to_sym : nil,
- ( cookie = cookies['rails_language'] and l = cookie.first and
- ActionController::AbstractRequest.acceptable_language?(l) ) ? l.to_sym : nil,
- ( @env['HTTP_ACCEPT_LANGUAGE'] || '' ).split(",").collect {|l|
- lsym= l.split(/;|\-/,2)[0].downcase.to_sym
- ActionController::AbstractRequest.acceptable_language?(lsym) ? lsym : nil
- },
- ActionController::AbstractRequest.acceptable_languages
- ].flatten.compact.uniq
- end
-
- def accepts_languages
- @accepts_languages || accepts_languages!
- end
-
- end
-
- #### store specified language into cookie
-
- class ActionController::Base
-
- # before_filter :set_language
- # fails for some unknown reason.
- # following is a workaround.
-
- def perform_action_with_set_rails_language
- set_rails_language
- perform_action_without_set_rails_language
- end
- alias_method_chain :perform_action, :set_rails_language
-
- def set_rails_language
- rails_language= params[:rails_language]
- if rails_language && ActionController::AbstractRequest.acceptable_language?(rails_language)
- cookies['rails_language']= { :value => rails_language }
- request.accepts_languages!(rails_language)
- end
- end
- end
-
- #### unmemoize
-
- module ActiveSupport::Memoizable
-
- def memoized?(symbol)
- original_method = :"_unmemoized_#{symbol}"
- class_eval <<-EOS, __FILE__, __LINE__
- method_defined?(:#{original_method})
- EOS
- end
-
- def unmemoize(*symbols)
- symbols.each do |symbol|
- original_method = :"_unmemoized_#{symbol}"
- memoized_ivar = MEMOIZED_IVAR.call(symbol)
- class_eval <<-EOS, __FILE__, __LINE__
- raise "Not memoized #{symbol}" if !method_defined?(:#{original_method})
- alias #{symbol} #{original_method}
- undef #{original_method}
- #{memoized_ivar}= nil
- EOS
- end
- end
-
- end
-
- #### to pick language-specific templates
-
- class ActionView::Base
-
- private
-
- def _pick_template_sub(template_file_name, lang="")
- if template = self.view_paths["#{template_file_name}.#{template_format}#{lang}"]
- return template
- elsif template = self.view_paths["#{template_file_name}#{lang}"]
- return template
- elsif (first_render = @_render_stack.first) && first_render.respond_to?(:format_and_extension)
- m= first_render.format_and_extension.match(/(.*)(\.\w+)?$/)
- template = self.view_paths["#{template_file_name}.#{m[1]}#{lang}#{m[2]}"]
- return template
- end
- if template_format == :js && template = self.view_paths["#{template_file_name}.html#{lang}"]
- @template_format = :html
- return template
- end
- nil
- end
- memoize :_pick_template_sub
-
- unmemoize :_pick_template if memoized? :_pick_template
- def _pick_template(template_path)
- return template_path if template_path.respond_to?(:render)
-
- path = template_path.sub(/^\//, '')
- if ( m = path.match(/(.*)\.(\w+)$/) ) &&
- ActionView::Template.template_handler_extensions.include?(m[2])
- template_file_name, template_file_extension = m[1], m[2]
- else
- template_file_name = path
- end
-
- # search for localized version
- if controller && controller.respond_to?(:request)
- controller.request.accepts_languages.each do |lang|
- if template = _pick_template_sub(template_file_name, ".#{lang}")
- controller.request.language= lang
- return template
- end
- end
- end
-
- # search for not localized version
- if template = _pick_template_sub(template_file_name)
- template
- else
- # not found in view_paths
- template = ActionView::Template.new(template_path, view_paths)
-
- if self.class.warn_cache_misses && logger
- logger.debug "[PERFORMANCE] Rendering a template that was " +
- "not found in view path. Templates outside the view path are " +
- "not cached and result in expensive disk operations. Move this " +
- "file into #{view_paths.join(':')} or add the folder to your " +
- "view path list"
- end
-
- template
- end
- end
-
- end
-
- #### for correct mime-type detection
-
- module ActionView
- class Template
- attr_accessor :language
-
- def initialize(template_path, load_paths = [])
- template_path = template_path.dup
- @base_path, @name, @format, @language, @extension = split(template_path)
- @base_path.to_s.gsub!(/\/$/, '') # Push to split method
- @load_path, @filename = find_full_path(template_path, load_paths)
-
- # Extend with partial super powers
- extend RenderablePartial if @name =~ /^_/
- end
-
- unmemoize :format_and_extension
- def format_and_extension
- (extensions = [format, language, extension].compact.join(".")).blank? ? nil : extensions
- end
- memoize :format_and_extension
-
- unmemoize :path
- def path
- [base_path, [name, format, language, extension].compact.join('.')].compact.join('/')
- end
- memoize :path
-
- unmemoize :path_without_extension
- def path_without_extension
- [base_path, [name, format, language].compact.join('.')].compact.join('/')
- end
- memoize :path_without_extension
-
- unmemoize :path_without_format_and_extension
- def path_without_format_and_extension
- [base_path, name].compact.join('/')
- end
- memoize :path_without_format_and_extension
-
- private
-
- # Returns file split into an array
- # [base_path, name, format, language, extension]
- def split(file)
- if m = file.match(/^(.*\/)?([^\.]+)(?:\.(\w+)(?:\.(\w+)(?:\.(\w+)(?:\.(\w+))?)?)?)?$/)
- if m[6] # multi part format
- [m[1], m[2], "#{m[3]}.#{m[4]}", m[5], m[6]]
- elsif m[5]
- if ActionController::AbstractRequest.acceptable_language?(m[4])
- [m[1], m[2], m[3], m[4], m[5]]
- else # multi part format
- [m[1], m[2], "#{m[3]}.#{m[4]}", m[5]]
- end
- elsif m[4] # no format
- if valid_extension?(m[4])
- if ActionController::AbstractRequest.acceptable_language?(m[3])
- [m[1], m[2], nil, m[3], m[4]]
- else # Single format
- [m[1], m[2], m[3], nil, m[4]]
- end
- else
- [m[1], m[2], m[3], m[4], nil]
- end
- else
- if valid_extension?(m[3])
- [m[1], m[2], nil, nil, m[3]]
- elsif ActionController::AbstractRequest.acceptable_language?(m[3])
- [m[1], m[2], nil, m[3], nil]
- else
- [m[1], m[2], m[3], nil, nil]
- end
- end
- end
- end
- end
- end
-
- #### for page cache
-
- module ActionController::Caching::Pages
- def cache_page(content = nil, options = nil)
- return unless perform_caching && caching_allowed
-
- path = case options
- when Hash
- url_for(options.merge(
- :only_path => true,
- :skip_relative_url_root => true,
- :format => params[:format],
- :rails_language => request.accepts_languages.first))
- when String
- options
- else
- p= request.path.split('.')
- p.pop if ActionController::AbstractRequest.acceptable_language?(p.last)
- p[0]+= self.class.page_cache_extension if p.count==1
- p << request.accepts_languages.first
- p.join('.')
- end
-
- self.class.cache_page(content || response.body, path)
- end
- end
-
- #### for action cache
-
- module ActionController::Caching::Actions
- class ActionCachePath
- attr_reader :language
-
- def initialize(controller, options = {}, infer_extension=true)
- if infer_extension and options.is_a? Hash
- request_extension = extract_extension(controller.request)
- options = controller.params.merge(
- options.reverse_merge(
- :format => request_extension,
- :rails_language => controller.request.accepts_languages.first))
- end
- path = controller.url_for(options).split('://').last
- if infer_extension
- @extension = request_extension
- add_extension!(path, @extension)
- end
- @path = URI.unescape(path)
- end
-
- private
-
- def add_extension!(path, extension)
- if extension
- p= path.split('.')
- p.insert(-2, extension) unless path =~ /\b#{Regexp.escape(extension)}\b/
- p.join('.')
- end
- end
-
- def extract_extension(request)
- p= request.path.split('.')
- # drop file name
- p.shift
- # drop language
- p.pop if !p.empty? && ActionController::AbstractRequest.acceptable_language?(p.last)
- extension = p.join('.')
-
- # If there's no extension in the path, check request.format
- extension = request.cache_format if extension==""
-
- extension
- end
- end
- end
-
- #### for fragment cache
-
- module ActionController
- module Caching
- module Fragments
- def fragment_cache_key(key)
- ActiveSupport::Cache.expand_cache_key(
- key.is_a?(Hash) ?
- url_for(key.reverse_merge(
- :rails_language=>request.accepts_languages.first)
- ).split("://").last :
- key,
- :views)
- end
- end
- end
- end
-
* プラグイン化 [#j4d21556]
http://akimichi.homeunix.net/hiki/rails/?%A5%D7%A5%E9%A5%B0%A5%A4%A5%F3 ~
@@ -1864,6 +1465,7 @@
動作チェック
+ LANG:console
$ wgetc http://localhost/negotiation/test
English: Test OK
@@ -1898,6 +1500,7 @@
まずいきなりテストしてみる。
+ LANG:console
$ cd vendor/plugins/lanugage_negotiation/
$ rake
(in (rails)/negotiation/vendor/plugins/lanugage_negotiation)
@@ -1928,10 +1531,11 @@
確かに通ってないんだけど、いくら何でも通ってなさすぎ。~
どうしてこんな事に?
-この rake をいきなり起動しちゃだめなのね。
+ああ、この rake をいきなり起動しちゃだめなのね。
アプリケーションのホームから、
+ LANG:console
$ cd ../../..
$ pwd
(rails)/negotiation
@@ -1948,11 +1552,13 @@
うまく行ってる。
-** テストの作成 [#n11b0fa7]
+** プラグインのテストの作成 [#n11b0fa7]
-あとはもりもり書くのみ。
+あとは vendor/plugins/lanugage_negotiation/test/lanugage_negotiation_test.rb に
+テストケースをもりもり書いていけばいい。
テストの書き方は~
+ LANG:ruby(linenumber)
test "what to test" do
assert true
assert_equal 1, 1
@@ -2044,12 +1650,12 @@
class ActionController::AbstractRequest.accepts_languages!(language=nil)
...
- lsym= l.split(/;|\-/,2)[0].strip.downcase.to_sym
+ lsym= l.split(/;|\-/,2)[0].downcase.to_sym
の部分、
LANG:ruby
- lsym= l.split(/;|\-/,2)[0].downcase.to_sym
+ lsym= l.split(/;|\-/,2)[0].strip.downcase.to_sym
とする必要有り。
@@ -2099,6 +1705,7 @@
unmemoize 後にもう一度 memoize した場合には、
以前の計算結果が残ってしまうことに注意。
+ LANG:ruby(linenumber)
module ActiveSupport::Memoizable
def unmemoize(*symbols)
class_eval <<-EOS, __FILE__, __LINE__
@@ -2109,11 +1716,71 @@
この行は削除した。
+ LANG:console
+ $ rake test:plugins
+ /usr/bin/ruby1.8 -I"(rails)/negotiation/lib" -I"(rails)/negotiation/test" "/usr/lib/ruby/1.8/rake/rake_test_loader.rb" "vendor/plugins/lanugage_negotiation/test/lanugage_negotiation_test.rb"
+ Loaded suite /usr/lib/ruby/1.8/rake/rake_test_loader
+ Started
+ ...
+ Finished in 0.038165 seconds.
+
+ 3 tests, 25 assertions, 0 failures, 0 errors
-以下、コントローラ込みのファンクショナルテストについては
-http://d.hatena.ne.jp/elm200/20070724/1185282738 が参考になる。
+テスト成功。
- LANG:ruby(linenumber)
+他にももう少し plugin test でちまちま書いてく事もできるんだけど、
+controller の test と合わせていっきにやってしまった方が楽そう?
+
+** negotiation アプリケーションのテストケース [#p80291d3]
+
+コントローラ込みのファンクショナルテストについては、
+プラグインのテストケースに入れるわけに行かないので、
+negotiation アプリケーションの
+test コントローラのテストケースとして書く事になる。
+
+まずテストに必要なアクションとビューを追加。
+
+ LANG:console
+ $ jed app/controllers/test_controller.rb
+ class TestController < ApplicationController
+ caches_page :pagecached
+ caches_action :actioncached
+ def index
+ end
+ def pagecached
+ end
+ def actioncached
+ end
+ def fragmentcached
+ end
+ def render_action
+ render :action => :acceptid
+ end
+ def render_file
+ render :file => "test/acceptid.html"
+ end
+ def acceptid
+ end
+ end
+ $ echo "Test <%= render :partial=>'ok' %>" > app/views/test/index.html.en.erb
+ $ echo "テスト <%= render :partial=>'ok' %>" > app/views/test/index.html.ja.erb
+ $ echo "Page cached" > app/views/test/pagecached.html.en.erb
+ $ echo "ページキャッシュ" > app/views/test/pagecached.html.ja.erb
+ $ echo "Action cached" > app/views/test/actioncached.html.en.erb
+ $ echo "アクションキャッシュ" > app/views/test/actioncached.html.ja.erb
+ $ echo "<% cache :param1=>:test do %>Fragment cached <%= render :partial=>'ok' %><% end %>" > app/views/test/fragmentcached.html.en.erb
+ $ echo "<% cache do %>フラグメントキャッシュ <%= render :partial=>'ok' %><% end %>" > app/views/test/fragmentcached.html.ja.erb
+ $ echo "File" > app/views/test/renderfile_.html.en.erb
+ $ echo "ファイル" > app/views/test/renderfile_.html.ja.erb
+ $ echo "html <%= params[:id] %>" > app/views/test/acceptid.html.ja.erb
+ $ echo "text <%= params[:id] %>" > app/views/test/acceptid.text.ja.erb
+ $ echo "English <%= @content_for_layout %>" > app/views/layouts/application.text.en.erb
+
+テストは test/functional/test_controller_test.rb に書き込む。
+
+書き方は http://d.hatena.ne.jp/elm200/20070724/1185282738 が参考になる。
+
+ LANG:ruby(linenumber)
def setup
@ar = ActionController::AbstractRequest
@controller = TestController.new
@@ -2123,7 +1790,7 @@
としておくと、
- LANG:ruby(linenumber)
+ LANG:ruby(linenumber)
test "server default1" do
@ar.acceptable_languages = :ja, :en, :fr
@request.cookies = {}
@@ -2136,13 +1803,18 @@
のようなコードを書く事ができる。
-キャッシュをテストするには、~
+キャッシュをテストするために、~
config/environments/test.rb
+ LANG:ruby(linenumber)
+ config.action_controller.perform_caching = true
-config.action_controller.perform_caching = true
-
さらに、
- LANG:ruby(linenumber)
+ LANG:ruby(linenumber)
+ def assert_cached_page_exists( page )
+ full_path= ActionController::Base.__send__(:page_cache_path, page)
+ assert File.exist?(full_path), "Cached file '#{full_path}' not created."
+ end
+
def assert_cached_page( page )
ActionController::Base.class_eval <<-EOS
@@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
@@ -2150,13 +1822,12 @@
EOS
@controller.expire_page page
yield
- full_path= ActionController::Base.__send__(:page_cache_path, page)
- assert File.exist?(full_path), "Cached file '#{full_path}' not created."
+ assert_cached_page_exists page
end
として、development や production 環境にキャッシュを作成してしまわないようにする。
- LANG:ruby(linenumber)
+ LANG:ruby(linenumber)
test "page cache 1" do
@ar.acceptable_languages = :fr, :ja, :en
@request.cookies = {}
@@ -2173,84 +1844,181 @@
のようにテストする事ができる。
-ミスして入力したテストで気づいた。
+その他作ったヘルパー関数達
+ LANG:ruby(linenumber)
+ def assert_content_type(type)
+ flunk "Content-Type '#{type}' was expected but "+
+ "'#{@response.content_type}' found." unless
+ @response.content_type == type
+ end
+
+ def assert_cached_fragment_exists(key, message="Fragment cache ['#{key}'] not exists.")
+ flunk message unless @controller.fragment_exist? key
+ end
+
+ def assert_cached_fragment(key)
+ @controller.expire_fragment key
+ yield
+ assert_cached_fragment_exists key
+ end
+
+ def assert_layout(v)
+ assert_equal v, @response.template.send(
+ :_pick_template, @response.layout).path_without_extension
+ end
+
+ def log(s)
+ if s.is_a? String
+ Rails.logger.warn s
+ else
+ Rails.logger.warn s.inspect
+ end
+ end
+
+テストはかなりたくさん書いたので、ここに全部は載せない。~
+一部を取り出すと、
+
+ LANG:ruby(linenumber)
+ test "page cache 3" do
+ @ar.acceptable_languages = :fr, :ja, :en
+ @request.cookies = {}
+ @request.env = { 'HTTP_ACCEPT_LANGUAGE' => 'xx' }
+ page = "/test/pagecached.html.fr"
+ assert_cached_page( page ) do
+ get :pagecached, :format=>"html"
+ assert_response :success
+ assert_template 'pagecached.html.ja'
+ assert_equal "/test/pagecached", @request.request_uri
+ assert_layout "layouts/application.html.ja"
+ assert_content_type "text/html"
+ end
+ end
+
+ test "page cache 4" do
+ @ar.acceptable_languages = :fr, :ja, :en
+ @request.cookies = {}
+ @request.env = { 'HTTP_ACCEPT_LANGUAGE' => 'en' }
+ page = "/test/pagecached.html.en"
+ assert_cached_page( page ) do
+ get :pagecached, :format=>"html", :extra=>:param
+ assert_response :success
+ assert_template 'pagecached.html.en'
+ assert_equal "/test/pagecached?extra=param", @request.request_uri
+ assert_layout "layouts/application.html.en"
+ assert_content_type "text/html"
+ end
+ end
+
+ test "action cache 1" do
+ @ar.acceptable_languages = :fr, :ja, :en
+ @request.cookies = {}
+ key = "test.host/test/actioncached?rails_language=fr"
+ assert_cached_fragment( key ) do
+ get :actioncached, :format=>"html"
+ assert_response :success
+ assert_template 'actioncached.html.ja'
+ assert_equal "/test/actioncached", @request.request_uri
+ assert_layout "layouts/application.html.ja"
+ assert_content_type "text/html"
+ end
+ end
+
+ test "action cache 2" do
+ @ar.acceptable_languages = :fr, :ja, :en
+ @request.cookies = {}
+ key = "test.host/test/actioncached.ja"
+ assert_cached_fragment( key ) do
+ get :actioncached, :format=>"html", :rails_language=>"ja"
+ assert_response :success
+ assert_template 'actioncached.html.ja'
+ assert_equal "/test/actioncached.ja", @request.request_uri
+ assert_layout "layouts/application.html.ja"
+ assert_content_type "text/html"
+ end
+ end
+
+こんな感じ。
+
+実際にこれを走らせる。
+
+ LANG:console
+ $ rake test:functionals
+ rake aborted!
+ no such file to load -- (rails)/negotiation/db/schema.rb
+
+ (See full trace by running task with --trace)
+ $ rake db:schema:dump
+ $ rake test:functionals
+ /usr/bin/ruby1.8 -I"(rails)/negotiation/lib" -I"(rails)/negotiation/test" "/usr/lib/ruby/1.8/rake/rake_test_loader.rb" "test/functional/test_controller_test.rb"
+ Loaded suite /usr/lib/ruby/1.8/rake/rake_test_loader
+ Started
+ ..........................
+ Finished in 0.358801 seconds.
+
+ 26 tests, 96 assertions, 0 failures, 0 errors
+
+実行は rake test:functionals で良い。~
+schema.rb が無いと言われたので、rake db:schema:dump で作成した。
+
+ミスして入力したテストで気づいた点。
+
+ LANG:ruby(linenumber)
module ActionView
class Template
def split(file)
- elsif && ActionController::AbstractRequest.acceptable_language?(m[3])
+ elsif ActionController::AbstractRequest.acceptable_language?(m[3])
-の部分
- elsif m[3] && ActionController::AbstractRequest.acceptable_language?(m[3])
+の部分などで、m[3] が nil になった場合、
+ LANG:ruby(linenumber)
+ class ActionController::AbstractRequest
+ def self.acceptable_language?(l)
+ acceptable_languages.include?(l.to_sym)
+ end
+ end
+
+の l.to_sym でこける。
+
+ LANG:ruby(linenumber)
+ def self.acceptable_language?(l)
+ l.respond_to?(:to_sym) && acceptable_languages.include?(l.to_sym)
+ end
+
とすべき。
-routes.rb では
+また、routes.rb では
+ LANG:ruby(linenumber)
map.connect ':controller/:action/:id', :format => 'html'
を始めの方に書いておかないと、
+ LANG:ruby(linenumber)
url_for(:controller=>:test, :action=>:acceptid, :index=>1, :format=>"html")
が
+ LANG:ruby(linenumber)
map.connect ':controller/:action/:id.:rails_language', :format => 'html',
:requirements => { :rails_language => lang_regexp }
を使って変換されてしまい、
/test/acceptid/1.
-となるので、順番を入れ替えた。
+となるので、順番を入れ替える。
+* fcgi 化 [#wd595311]
+ちょっとオフトピックだけれど、はまったので。
-* 手動テスト [#wd595311]
+fcgi 化するには基本的には RewriteRule で dispatch.cgi となっていたところを
+dispatch.fcgi にするだけでよい。
LANG:console
- $ jed app/controllers/test_controller.rb
- class TestController < ApplicationController
- caches_page :pagecached
- caches_action :actioncached
- def index
- end
- def pagecached
- end
- def actioncached
- end
- def fragmentcached
- end
- def onlyja
- end
- def rendereaction
- render :action=>:onlyja
- end
- def renderfile
- render :file=>"renderfile_"
- end
- def acceptid
- end
- end
- $ echo "Test <%= render :partial=>'ok' %>" > app/views/test/index.html.en.erb
- $ echo "テスト <%= render :partial=>'ok' %>" > app/views/test/index.html.ja.erb
- $ echo "Page cached" > app/views/test/pagecached.html.en.erb
- $ echo "ページキャッシュ" > app/views/test/pagecached.html.ja.erb
- $ echo "Action cached" > app/views/test/actioncached.html.en.erb
- $ echo "アクションキャッシュ" > app/views/test/actioncached.html.ja.erb
- $ echo "Fragment cached" > app/views/test/fragmentcached.html.en.erb
- $ echo "フラグメントキャッシュ" > app/views/test/fragmentcached.html.ja.erb
- $ echo "日本語のみ" > app/views/test/onlyja.html.ja.erb
- $ echo "File" > app/views/test/renderfile_.html.en.erb
- $ echo "ファイル" > app/views/test/renderfile_.html.ja.erb
- $ echo "html <%= params[:id] %>" > app/views/test/acceptid.html.ja.erb
- $ echo "text <%= params[:id] %>" > app/views/test/acceptid.text.ja.erb
- $ echo "English <%= @content_for_layout %>" > app/views/layouts/application.text.en.erb
- $ rm public/test*
- rm: cannot remove `public/test*': そのようなファイルやディレクトリはありません
- $ alias wgetc="wget -qO- --load-cookies=tmp/wget.cookie --save-cookies=tmp/wget.cookie --keep-session-cookies --header='ACCEPT: text/html'"
$ jed public/.htaccess
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
$ wgetc --header="Accept-Language: en" http://localhost/negotiation/test
エラー: ActionController::RoutingError (no route found to match to "/negotiation/test")
-うまくいかない。~
+でも、なんでかうまくいかない。~
.htaccess の SetEnv RAILS_RELATIVE_URL_ROOT /negotiation の効果が得られていないみたい。
vendor/actionpack/CHANGELOG
@@ -2260,7 +2028,7 @@
config.action_controller.relative_url_root [Josh Peek]
どうやらこれらしい。~
-でもなんで fcgi の時だけだめなんだろう?
+なんで fcgi の時だけだめなんだろう?
changelog に書かれているのだからバグという事でもないのだろうし、
コードを追うのはやめにして、素直に environments/production.rb に
@@ -2288,8 +2056,796 @@
$ ls public/test/*
public/test/pagecached.html.en public/test/pagecached.html.ja
+* git リポジトリ(ローカル)の作成 [#a85568da]
+
+時代は subversion ではなく git なのだそうで。
+
+http://www.kaeruspoon.net/articles/477 とか、~
+http://www.kaeruspoon.net/articles/479 とか、~
+http://www.netfort.gr.jp/~dancer/diary/200812.html.ja とか、~
+http://blog.champierre.com/archives/670 ~
+http://www.tempus.org/n-miyo/git-course-trans-ja/svn.ja.html~
+あたりを参考に。
+
+まずは git のインストール。
+
+ LANG:console
+ $ sudo aptitude install git-core
+ 以下の新規パッケージがインストールされます:
+ git-core liberror-perl{a} rsync{a}
+ 先に進みますか? [Y/n/?] Y
+ liberror-perl (0.17-1) を設定しています ...
+ git-core (1:1.6.3.1-1) を設定しています ...
+ rsync (3.0.5-1) を設定しています ...
+ $ git --version
+ git version 1.6.3.1
+ $ git config --global user.name "Osamu TAKEUCHI"
+ $ git config --global user.email "osamu@big.jp"
+ $ git config --global color.diff auto
+ $ git config --global color.status auto
+ $ git config --global color.branch auto
+ $ git config --global color.interactive auto
+ $ git config --global color.ui auto
+ $ git config --global core.pager "lv -c"
+ $ cat ~/.gitconfig
+ [user]
+ name = Osamu TAKEUCHI
+ email = osamu@big.jp
+ [color]
+ diff = auto
+ status = auto
+ branch = auto
+ interactive = auto
+ ui = auto
+ [core]
+ pager = lv -c
+
+vendor/plugins/language_negotiation 以下にあるプラグインと、~
+テスト用の Rails アプリケーションと、~
+2つのリポジトリを別々に作成する。
+
+まずはプラグインの方から
+
+ LANG:console
+ $ cd vendor/plugins/lanugage_negotiation/
+ $ git init
+ Initialized empty Git repository in (rails)/negotiation/vendor/plugins/lanugage_negotiation/.git/
+ $ git add .
+ $ git commit
+ First commit of LanguageNegotiation plugin for Ruby on Rails
+
+ Almost working with:
+ Ruby version 1.8.7 (i486-linux)
+ RubyGems version 1.3.2
+ Rails version 2.2.2
+ apache2 2.2.11-3
+
+ Documentation should be completed.
+
+ [master (root-commit) 3df21b6] First commit of LanguageNegotiation plugin for Ruby on Rails
+ 10 files changed, 636 insertions(+), 0 deletions(-)
+ create mode 100644 MIT-LICENSE
+ create mode 100644 README
+ create mode 100644 Rakefile
+ create mode 100644 init.rb
+ create mode 100644 install.rb
+ create mode 100644 lib/lanugage_negotiation.rb
+ create mode 100644 tasks/lanugage_negotiation_tasks.rake
+ create mode 100644 test/lanugage_negotiation_test.rb
+ create mode 100644 test/test_helper.rb
+ create mode 100644 uninstall.rb
+ $ git status
+ # On branch master
+ nothing to commit (working directory clean)
+
+次にテストアプリケーション
+
+ LANG:console
+ $ cd ../../..
+ $ pwd
+ (rails)/negotiation
+ $ git init
+ Initialized empty Git repository in (rails)/negotiation/.git/
+ $ mkdir db/migration
+ $ find . -type d -empty|xargs -L1 -I{} touch {}/.gitignore # 空のディレクトリに .gitignore を作成
+ $ touch log/.gitignore tmp/.gitignore vendor/plugin/.gitignore
+ $ cp config/database.yml config/database.sample.yml
+ $ cat > .gitignore
+ .DS_Store
+ *~
+ /log/*.log
+ /db/*.sqlite3
+ /config/database.yml
+ /tmp/*
+ !/tmp/*.gitignore
+ !/tmp/cache
+ !/tmp/pids
+ !/tmp/sessions
+ !/tmp/sockets
+ /public/cahce_in_test_environment
+ /vendor/plugins/language_negotiation
+ $ git add .
+ $ git status
+ # On branch master
+ #
+ # Initial commit
+ #
+ # Changes to be committed:
+ # (use "git rm --cached <file>..." to unstage)
+ #
+ # new file: .gitignore
+ # new file: README
+ # new file: Rakefile
+ # new file: app/controllers/application.rb
+ # new file: app/controllers/test_controller.rb
+ # new file: app/helpers/application_helper.rb
+ # new file: app/helpers/test_helper.rb
+ # new file: app/models/.gitignore
+ # new file: app/views/layouts/application.html.en.erb
+ # new file: app/views/layouts/application.html.ja.erb
+ # new file: app/views/layouts/application.text.en.erb
+ # new file: app/views/test/_ok.html.en.erb
+ # new file: app/views/test/_ok.html.ja.erb
+ # new file: app/views/test/acceptid.html.ja.erb
+ # new file: app/views/test/acceptid.text.ja.erb
+ # new file: app/views/test/actioncached.html.en.erb
+ # new file: app/views/test/actioncached.html.ja.erb
+ # new file: app/views/test/fragmentcached.html.en.erb
+ # new file: app/views/test/fragmentcached.html.ja.erb
+ # new file: app/views/test/index.html.en.erb
+ # new file: app/views/test/index.html.ja.erb
+ # new file: app/views/test/onlyja.html.ja.erb
+ # new file: app/views/test/pagecached.html.en.erb
+ # new file: app/views/test/pagecached.html.ja.erb
+ # new file: app/views/test/renderfile_.html.en.erb
+ # new file: app/views/test/renderfile_.html.ja.erb
+ # new file: config/boot.rb
+ # new file: config/database.sample.yml
+ # new file: config/environment.rb
+ # new file: config/environments/development.rb
+ # new file: config/environments/production.rb
+ # new file: config/environments/test.rb
+ # new file: config/initializers/inflections.rb
+ # new file: config/initializers/mime_types.rb
+ # new file: config/initializers/new_rails_defaults.rb
+ # new file: config/locales/en.yml
+ # new file: config/routes.rb
+ # new file: db/migration/.gitignore
+ # new file: db/schema.rb
+ # new file: doc/README_FOR_APP
+ # new file: doc/api
+ # new file: lib/tasks/.gitignore
+ # new file: log/.gitignore ...
+ # new file: public/.htaccess
+ # new file: public/404.html
+ # new file: public/422.html
+ # new file: public/500.html
+ # new file: public/dispatch.cgi
+ # new file: public/dispatch.fcgi
+ # new file: public/dispatch.rb
+ # new file: public/favicon.ico
+ # new file: public/images/rails.png
+ # new file: public/index.html
+ # new file: public/javascripts/application.js
+ # new file: public/javascripts/controls.js
+ # new file: public/javascripts/dragdrop.js
+ # new file: public/javascripts/effects.js
+ # new file: public/javascripts/prototype.js
+ # new file: public/robots.txt
+ # new file: public/stylesheets/.gitignore
+ # new file: script/about
+ # new file: script/console
+ # new file: script/dbconsole
+ # new file: script/destroy
+ # new file: script/generate
+ # new file: script/performance/benchmarker
+ # new file: script/performance/profiler
+ # new file: script/performance/request
+ # new file: script/plugin
+ # new file: script/process/inspector
+ # new file: script/process/reaper
+ # new file: script/process/spawner
+ # new file: script/runner
+ # new file: script/server
+ # new file: test/fixtures/.gitignore
+ # new file: test/functional/test_controller_test.rb
+ # new file: test/performance/browsing_test.rb
+ # new file: test/test_helper.rb
+ # new file: test/unit/.gitignore
+ # new file: tmp/.gitignore
+ # new file: tmp/cache/.gitignore
+ # new file: tmp/pids/.gitignore
+ # new file: tmp/sessions/.gitignore
+ # new file: tmp/sockets/.gitignore
+ # new file: vendor/actionmailer
+ # new file: vendor/actionpack
+ # new file: vendor/activemodel
+ # new file: vendor/activerecord
+ # new file: vendor/activeresource
+ # new file: vendor/activesupport
+ # new file: vendor/plugins/.gitignore
+ # new file: vendor/rails
+ # new file: vendor/railties
+ #
+ $ git commit
+ Testing framework of LanguageNegotiation plugin for Ruby on Rails
+
+ Works with:
+ Ruby version 1.8.7 (i486-linux)
+ RubyGems version 1.3.2
+ Rails version 2.2.2
+ apache2 2.2.11-3
+ ...
+ $
+
+git は空のディレクトリを無視するので、空のディレクトリ全てに
+.gitignore というファイルを作っておく。
+
+個々のディレクトリの .gitignore にルールを書くことを勧める記述が
+多いのだけれど、見通しが悪くなるのでルートディレクトリの .gitignore に
+一元化するのがよいと思う。
+
+ルールを / から始めるとリポジトリのルートディレクトリからの絶対パスとして、~
+ルールを / から始めなければ個々のディレクトリからの相対パスとして、解釈されるらしい。
+
+tmp ディレクトリの指定が結構面倒で、~
+ /tmp/*
+
+とすると、.gitignore も含めて無視されるため、空のディレクトリとなった /tmp 自身も無視されてしまう。
+ !.giignore
+
+とすることで、.gitignore は無視しないように指定できる。
+
+同様に、/tmp/cache/.gitignore なども無視しないように書いたのが以下のルール。
+ !/tmp/*.gitignore
+ !/tmp/cache
+ !/tmp/pids
+ !/tmp/sessions
+ !/tmp/sockets
+
+これでローカルに2つのリポジトリが作成できた。
+
+* プロジェクトを github に登録 [#s00cb9d3]
+
+https://github.com/
+
+にアカウントを作成し、プロジェクト RailsLanguageNegotiationPlugin と
+RailsLanguageNegotiationPluginTestApp を作成。
+
+表示されたガイダンスに沿う形で、
+
+ LANG:console
+ $ git config --global github.user osamutake
+ $ git config --global github.token c2b35c1bd09d64bf37a713702d7ae659
+ $ git remote add origin git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ $ git push origin master
+ Permission denied (publickey).
+ fatal: The remote end hung up unexpectedly
+ $ cd ~/.ssh
+ $ ls id_dsa
+ ls: cannot access id_dsa: そのようなファイルやディレクトリはありません
+ $ ssh-keygen # もし id_dsa がすでにあればスキップ
+ Generating public/private rsa key pair.
+ Enter file in which to save the key (/home/takeuchi/.ssh/id_rsa): (何もせずENTER)
+ Enter passphrase (empty for no passphrase): (何もせずENTER)
+ Enter same passphrase again: (何もせずENTER)
+ Your identification has been saved in github.
+ Your public key has been saved in github.pub.
+ The key fingerprint is:
+ ab:5e:07:df:a1:02:84:cf:a3:21:d1:7e:aa:38:b3:2f takeuchi@dora
+ The key's randomart image is:
+ +--[ RSA 2048]----+
+ | ................|
+ | |
+ +-----------------+
+ $ cat id_isa.pub
+ ssh-rsa AAAB3Nza...
+ $ # 表示された内容をコピーして https://github.com/account#keys の SSH Public Keys に追加
+ $ cd (rails)/negotiation/vendor/plugins/language_negotiation
+ $ git push origin master
+ Counting objects: 15, done.
+ Compressing objects: 100% (13/13), done.
+ Writing objects: 100% (15/15), 6.86 KiB, done.
+ Total 15 (delta 0), reused 0 (delta 0)
+ To git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ * [new branch] master -> master
+
+https://github.com/ を見るとちゃんと登録されている。とても簡単。
+
+** 取り出せる事をテスト [#a95bef8f]
+
+じゃ、取り出せるか試してみるか、と軽い気持ちで操作したら、えらい事になった。
+
+ LANGUAGE:console
+ $ cd ..
+ $ mkdir language_negotiation2
+ $ cd language_negotiation2
+ $ git pull git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ From git@github.com:osamutake/RailsLanguageNegotiationPlugin
+ * branch HEAD -> FETCH_HEAD
+ Auto-merging README
+ CONFLICT (add/add): Merge conflict in README
+ Auto-merging Rakefile
+ CONFLICT (add/add): Merge conflict in Rakefile
+ Auto-merging test/test_helper.rb
+ CONFLICT (add/add): Merge conflict in test/test_helper.rb
+ Automatic merge failed; fix conflicts and then commit the result.
+
+空のディレクトリでどうして conflict?~
+と思ったら、テストアプリケーションのリポジトリにマージされてました。
+
+ LANGUAGE:console
+ $ cd ../../..
+ $ git status
+ README: needs merge
+ Rakefile: needs merge
+ test/test_helper.rb: needs merge
+ # On branch master
+ # Changes to be committed:
+ # (use "git reset HEAD <file>..." to unstage)
+ #
+ # new file: MIT-LICENSE
+ # new file: init.rb
+ # new file: install.rb
+ # new file: lib/lanugage_negotiation.rb
+ # new file: tasks/lanugage_negotiation_tasks.rake
+ # new file: test/lanugage_negotiation_test.rb
+ # new file: uninstall.rb
+ #
+ # Changed but not updated:
+ # (use "git add <file>..." to update what will be committed)
+ # (use "git checkout -- <file>..." to discard changes in working directory)
+ #
+ # unmerged: README
+ # unmerged: Rakefile
+ # unmerged: test/test_helper.rb
+ #
+
+pull したのを取り消し、マージされたファイルを書き戻す。
+
+ LANGUAGE:console
+ $ git reset HEAD
+ README: locally modified
+ Rakefile: locally modified
+ test/test_helper.rb: locally modified
+ $ git status
+ # On branch master
+ # Changed but not updated:
+ # (use "git add <file>..." to update what will be committed)
+ # (use "git checkout -- <file>..." to discard changes in working directory)
+ #
+ # modified: README
+ # modified: Rakefile
+ # modified: test/test_helper.rb
+ #
+ # Untracked files:
+ # (use "git add <file>..." to include in what will be committed)
+ #
+ # MIT-LICENSE
+ # init.rb
+ # install.rb
+ # lib/lanugage_negotiation.rb
+ # tasks/
+ # test/lanugage_negotiation_test.rb
+ # uninstall.rb
+ no changes added to commit (use "git add" and/or "git commit -a")
+ $ rm MIT-LICENSE init.rb install.rb lib/lanugage_negotiation.rb
+ $ rm test/lanugage_negotiation_test.rb uninstall.rb
+ $ rm -r tasks
+ $ git status
+ # On branch master
+ # Changed but not updated:
+ # (use "git add <file>..." to update what will be committed)
+ # (use "git checkout -- <file>..." to discard changes in working directory)
+ #
+ # modified: README
+ # modified: Rakefile
+ # modified: test/test_helper.rb
+ #
+ no changes added to commit (use "git add" and/or "git commit -a")
+ $ git checkout README Rakefile test/test_helper.rb
+ $ git status
+ # On branch master
+ nothing to commit (working directory clean)
+
+ふう。~
+git は親ディレクトリをさかのぼってリポジトリデータを検索する仕様なのね。
+
+negotiation アプリケーションの外に出て、
+
+ LANGUAGE:console
+ $ cd
+ $ mkdir language_negotiation
+ $ cd language_negotiation
+ $ git pull git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ fatal: Not a git repository (or any of the parent directories): .git
+
+あら、そもそも pull と書いていたのが間違い、と。
+
+ LANG:console
+ $ git clone git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ Initialized empty Git repository in /home/takeuchi/language_negotiation/RailsLanguageNegotiationPlugin/.git/
+ remote: Counting objects: 15, done.
+ remote: Compressing objects: 100% (13/13), done.
+ remote: Total 15 (delta 0), reused 0 (delta 0)
+ Receiving objects: 100% (15/15), 6.86 KiB, done.
+ $ ls
+ RailsLanguageNegotiationPlugin
+ $ cd RailsLanguageNegotiationPlugin/
+ $ ls
+ MIT-LICENSE README Rakefile init.rb install.rb lib tasks test uninstall.rb
+ $ git status
+ # On branch master
+ nothing to commit (working directory clean)
+ $ cd ../..
+ $ rm -rf language_negotiation/
+
+正しく落とせている。
+
+もしかして・・・
+
+ LANG:console
+ $ cd (rails)/negotiation
+ $ cd vendor/plugins/
+ $ git clone git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ Initialized empty Git repository in (rails)/negotiation/vendor/plugins/RailsLanguageNegotiationPlugin/.git/
+ remote: Counting objects: 15, done.
+ remote: Compressing objects: 100% (13/13), done.
+ remote: Total 15 (delta 0), reused 0 (delta 0)
+ Receiving objects: 100% (15/15), 6.86 KiB, done.
+ $ ls
+ RailsLanguageNegotiationPlugin lanugage_negotiation
+ $ ls RailsLanguageNegotiationPlugin
+ MIT-LICENSE README Rakefile init.rb install.rb lib tasks test uninstall.rb
+ $ git status
+ # On branch master
+ # Untracked files:
+ # (use "git add <file>..." to include in what will be committed)
+ #
+ # RailsLanguageNegotiationPlugin/
+ nothing added to commit but untracked files present (use "git add" to track)
+ $ rm -rf RailsLanguageNegotiationPlugin
+
+これでもうまく行くわけね。
+
+やはりもう少し git に慣れないと危険だ。
+
+- Gitを使いこなすための20のコマンド http://sourceforge.jp/magazine/09/03/16/0831212/2
+- Lern.github http://learn.github.com/
+
+で、いきなり公開されてしまうのだから、もう少しドキュメントをしっかりしないとだめだと今更反省。~
+テストアプリケーションの方は特に、なので、しばらくローカルで更新して、整ったら push するのが良さそう。
+
+ローカルでも履歴が取れるのが subversion よりも良いところみたい。
+
+* git を使った編集作業 [#w301c061]
+
+http://github.com/osamutake/RailsLanguageNegotiationPlugin/issues#issue/1
+
+にも書いたけれど、言語の優先順位について考える必要がありそう。
+
+これを例にして、git を使った編集作業について書く。
+
+ LANG:console
+ $ jed vendor/plugins/language_negotiation/lib/language_negotiation.rb
+ (編集)
+ $ jed vendor/plugins/language_negotiation/test/language_negotiation_test.rb
+ (編集)
+ $ cd ../../..
+ $ rake test:plugins
+ Loaded suite /usr/lib/ruby/1.8/rake/rake_test_loader
+ Started
+ ...
+ Finished in 0.037807 seconds.
+
+ 3 tests, 30 assertions, 0 failures, 0 errors
+ $ rake test:functionals
+ Loaded suite /usr/lib/ruby/1.8/rake/rake_test_loader
+ Started
+ ..........................
+ Finished in 0.31159 seconds.
+
+ 30 tests, 112 assertions, 0 failures, 0 errors
+
+テストが無事通ったらコミット作業。
+
+ LANG:console
+ $ cd vendor/plugins/language_negotiation
+ $ git status
+ # On branch master
+ # Changed but not updated:
+ # (use "git add <file>..." to update what will be committed)
+ # (use "git checkout -- <file>..." to discard changes in working directory)
+ #
+ # modified: lib/lanugage_negotiation.rb
+ # modified: test/lanugage_negotiation_test.rb
+ #
+ no changes added to commit (use "git add" and/or "git commit -a")
+ $ git diff
+ (差分がカラー表示される)
+ $ git add lib/lanugage_negotiation.rb test/lanugage_negotiation_test.rb
+ $ git status
+ # On branch master
+ # Changes to be committed:
+ # (use "git reset HEAD <file>..." to unstage)
+ #
+ # modified: lib/lanugage_negotiation.rb
+ # modified: test/lanugage_negotiation_test.rb
+ #
+ $ git commit
+ Adjust priority of template file without language specification
+
+ When template files 'some.html.en' and 'some.html' exist,
+ request for 'some.ja' should be processed with 'some.html'
+ instead of 'some.html.en'.
+
+ See
+ http://github.com/osamutake/RailsLanguageNegotiationPlugin/issues#issue/1
+
+ Other improvements:
+
+ - removed not being used
+ ActionController::AbstractRequest::language
+
+ - calls for
+ ActionController::AbstractRequest.accepts_languages.first
+ were replaced by those for
+ ActionController::AbstractRequest.language_priority.first
+
+ - optimized template_handler_extension extraction from template_path
+ in ActionView::Base::_pick_template
+
+ - removed not being used
+ ActionController::Caching::Actions::ActionCachePath::language
+
+ [master e3a80c3] Adjust priority of template file without language specification
+ 2 files changed, 45 insertions(+), 44 deletions(-)
+ $ git push
+ Counting objects: 11, done.
+ Compressing objects: 100% (6/6), done.
+ Writing objects: 100% (6/6), 1.37 KiB, done.
+ Total 6 (delta 3), reused 0 (delta 0)
+ To git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ 3df21b6..e3a80c3 master -> master
+
+以上でリモートリポジトリも更新される。
+
+テストアプリケーションのコミットではちょっとミスした。
+
+ LANG:console
+ $ cd ../../..
+ $ git status
+ # On branch master
+ # Changed but not updated:
+ # (use "git add <file>..." to update what will be committed)
+ # (use "git checkout -- <file>..." to discard changes in working directory)
+ #
+ # modified: app/controllers/test_controller.rb
+ # modified: test/functional/test_controller_test.rb
+ #
+ # Untracked files:
+ # (use "git add <file>..." to include in what will be committed)
+ #
+ # app/views/test/nolang.html.erb
+ # app/views/test/nolang.html.ja.erb
+ $ git commit -a
+ Adjust priority of template file without language specification
+
+ When template files 'some.html.en' and 'some.html' exist,
+ request for 'some.ja' should be processed with 'some.html'
+ instead of 'some.html.en'.
+
+ See
+ http://github.com/osamutake/RailsLanguageNegotiationPlugin/issues#issue/1
+
+ [master 26c50dc] Adjust priority of template file without language specification
+ 2 files changed, 49 insertions(+), 0 deletions(-)
+ $ git status
+ # On branch master
+ # Untracked files:
+ # (use "git add <file>..." to include in what will be committed)
+ #
+ # app/views/test/nolang.html.erb
+ # app/views/test/nolang.html.ja.erb
+ nothing added to commit but untracked files present (use "git add" to track)
+ $ git add app/views/test/nolang.html.*
+ $ git status
+ # On branch master
+ # Changes to be committed:
+ # (use "git reset HEAD <file>..." to unstage)
+ #
+ # new file: app/views/test/nolang.html.erb
+ # new file: app/views/test/nolang.html.ja.erb
+ #
+ $ git commit --amend
+ [master 678da96] Adjust priority of template file without language specification
+ 4 files changed, 51 insertions(+), 0 deletions(-)
+ create mode 100644 app/views/test/nolang.html.erb
+ create mode 100644 app/views/test/nolang.html.ja.erb
+ $ git log
+ commit 678da9633211e515a7ec56034bb7b46e6724bd3e
+ Author: Osamu TAKEUCHI <osamu@big.jp>
+ Date: Wed Jun 10 17:45:01 2009 +0900
+
+ Adjust priority of template file without language specification
+
+ When template files 'some.html.en' and 'some.html' exist,
+ request for 'some.ja' should be processed with 'some.html'
+ instead of 'some.html.en'.
+
+ See
+ http://github.com/osamutake/RailsLanguageNegotiationPlugin/issues#issue/1
+
+ commit ad51715241e8f480da6de8d1d87035fea6c0d2ba
+ Author: Osamu TAKEUCHI <osamu@big.jp>
+ Date: Wed Jun 10 10:34:05 2009 +0900
+
+ Testing framework of LanguageNegotiation plugin for Ruby on Rails
+
+ Works with:
+ Ruby version 1.8.7 (i486-linux)
+ RubyGems version 1.3.2
+ Rails version 2.2.2
+ apache2 2.2.11-3
+
+git commit -a で変更点はすべてコミットされるかと思いきや、
+新規作成ファイルについては対象外だったようだ。
+
+手動で git add した後、git commit --amend したところ、
+コミットのやり直しができて、ミスの形跡も残らなかった。
+
+最後に github の該当する issue にコミット番号をコメントして、
+Closed に移して一件落着。
+
+* CHANGELOG について [#e36e8a78]
+
+ファイル中に変更履歴を残したい。
+
+こんな手順で良いんだろうか?
+
+ LANG:console
+ $ git log > CHANGELOG
+ $ git add CHANGELOG
+ $ git commit --amend
+ $ cd vendor/plugins/language_negotiation
+ $ git log > CHANGELOG
+ $ git add CHANGELOG
+ $ git commit --amend
+ $ git push origin master
+ To git@github.com:osamutake/RailsLanguageNegotiationPlugin.git
+ ! [rejected] master -> master (non-fast forward)
+ error: failed to push some refs to 'git@github.com:osamutake/RailsLanguageNegotiationPlugin.git'
+ $ git push origin +master:master
+
+最後の部分、--amend でコミットしたせいでリモートの情報とローカルの情報が
+食い違ってしまっていて、そのままだと push させてくれない。
+
+ローカルを優先して強制的に push するための呪文が git push origin +master:master らしい。
+
+同様の現象は、ローカルでの作業中にリモートリポジトリが書き換わった
+場合にも起きる。
+
+その場合には素直に git pull origin master して、ローカル側を更新する事になる。
+
+* tar ball の作り方 [#l75803c1]
+
+git ではなく tar ball で落としたい人向けに、
+ファイルパスに "*.git*" を含まないファイルのみを
+日付付きのファイル名に tar.gz してアップしておくと
+いいのかな、などと、
+
+ LANG:console
+ $ find . -not -path '*/.git*' -a -type f | \
+ tar fcz `date +"language_negotiation-%y%m%d%H%M%S"` -T-
+
+これを Downloads ページにアップロードすれば・・・
+
+む、よく見たら Dowloads ページに行かなくてもページの真ん中に
+download ボタンがあって、これをクリックすればソースツリーを
+zip か tar で落とせる仕組みになっているんだね。
+
+自分で tar ball を作る必要は無かったようでした。
+
+* スペルミスの修正 [#wd5c4d69]
+
+!!!
+
+今まで気づかなかったというのが信じられないほどに lanugage_negotiation 。
+
+ LANG:console
+ $ cd ..
+ $ mv lanugage_negotiation language_negotiation
+ $ cd language_negotiation
+ $ grep -i lanugage * */*
+ README:LanugageNegotiation
+ Rakefile:desc 'Test the lanugage_negotiation plugin.'
+ Rakefile:desc 'Generate documentation for the lanugage_negotiation plugin.'
+ Rakefile: rdoc.title = 'LanugageNegotiation'
+ init.rb:require "lanugage_negotiation"
+ lib/lanugage_negotiation.rb:# LanugageNegotiation
+ tasks/lanugage_negotiation_tasks.rake:# task :lanugage_negotiation do
+ test/lanugage_negotiation_test.rb:class LanugageNegotiationTest < ActiveSupport::TestCase
+ $ jed
+ (編集)
+ $ grep -i lanugage * */*
+ $ find . -iname lanu*
+ ./test/lanugage_negotiation_test.rb
+ ./tasks/lanugage_negotiation_tasks.rake
+ ./lib/lanugage_negotiation.rb
+ $ cd test
+ $ git mv lanugage_negotiation_test.rb language_negotiation_test.rb
+ $ ls
+ language_negotiation_test.rb test_helper.rb
+ $ cd ../tasks/
+ $ git mv lanugage_negotiation_tasks.rake language_negotiation_tasks.rake
+ $ cd ../lib
+ $ git mv lanugage_negotiation.rb language_negotiation.rb
+ $ cd ..
+ $ find . -iname lanu*
+ $ git status
+ # renamed: lib/lanugage_negotiation.rb -> lib/language_negotiation.rb
+ # renamed: tasks/lanugage_negotiation_tasks.rake -> tasks/language_negotiation_tasks.rake
+ # renamed: test/lanugage_negotiation_test.rb -> test/language_negotiation_test.rb
+ # modified: README
+ # modified: Rakefile
+ # modified: init.rb
+ # modified: lib/language_negotiation.rb
+ # modified: tasks/language_negotiation_tasks.rake
+ # modified: test/language_negotiation_test.rb
+ $ cd ../../..
+ $ rake test:plugins
+ 3 tests, 30 assertions, 0 failures, 0 errors
+ $ rake test:functionals
+ 30 tests, 112 assertions, 0 failures, 0 errors
+ $ cd vendor/plugins/language_negotiation/
+ $ git commit -a -m "Typo fixing lanugage -> language"
+ [master aef2a17] Typo fixing lanugage -> language
+ 6 files changed, 9 insertions(+), 9 deletions(-)
+ rename lib/{lanugage_negotiation.rb => language_negotiation.rb} (99%)
+ rename tasks/{lanugage_negotiation_tasks.rake => language_negotiation_tasks.rake} (67%)
+ rename test/{lanugage_negotiation_test.rb => language_negotiation_test.rb} (98%)
+ $ git status
+ nothing to commit (working directory clean)
+ $ git log > CHANGELOG
+ $ git commit -a --amend
+ [master 79ce1e9] Typo fixing lanugage -> language
+ 7 files changed, 16 insertions(+), 10 deletions(-)
+ rename lib/{lanugage_negotiation.rb => language_negotiation.rb} (99%)
+ rename tasks/{lanugage_negotiation_tasks.rake => language_negotiation_tasks.rake} (67%)
+ rename test/{lanugage_negotiation_test.rb => language_negotiation_test.rb} (98%)
+ $ git push origin master
+
+アプリケーション側のコードに全く触れずに済むあたり、Rails の設計の良さが光る。
+
+プラグイン内にしても、ドキュメント部分を除けば
+ init.rb:require "lanugage_negotiation"
+ test/lanugage_negotiation_test.rb:class LanugageNegotiationTest < ActiveSupport::TestCase
+
+この2つだけ。
+
+しかも後者は必ずしも直す必要のない部分。
+
+すばらしい。
+
+PS 今まで気づかなかったのが恐ろしい。
+
+* CHANGELOG 更新コマンド [#h3d7010e]
+
+メッセージの入力を求められるのがおっくうなとき、git commit の
+-C オプションが使える。
+
+ LANG:console
+ $ test -d .git && \
+ git log > CHANGELOG && \
+ git commit -a --amend -C `head -1 CHANGELOG | sed "s/.* //"`
+
++ .git ディレクトリが存在すれば~
+(リポジトリのトップディレクトリである事を確認してから)
++ git log を CHANGELOG に入れ
++ 前回と同じメッセージを付けて commit する
+
* コメント [#kfcbe2ab]
-気づいた点など、ぜひとも突っ込みを入れて下さい。
+気づいた点など、ぜひ突っ込みを入れて下さい。
#article_kcaptcha