# Copyright (c) 2006 KBMJ, Inc. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class ::ActionController::Base before_filter :sjis_request after_filter :sjis_response protected alias_method :rewrite_options_without_session_id, :rewrite_options # url_for などで URL を生成する際に _session_id を含める def rewrite_options(options) if is_mobile? session_key = ::ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:session_key] || '_session_id' if request && request.session.session_id options = options.dup options.update session_key => request.session.session_id end end rewrite_options_without_session_id(options) end # レスポンスを Shift_JIS に変換 def sjis_response if is_mobile? && (!headers["Content-Type"] || headers["Content-Type"] =~ %r{^text/html}) response.body = NKF.nkf('-Ws -m0', response.body) if is_mobile? headers["Content-Type"] = "text/html; charset=Shift_JIS" if is_mobile? end end # リクエストのパラメータを Shift_JIS から UTF-8 に変換 def sjis_request hash_sjis_to_utf8!(params) if is_mobile? end # ハッシュ内の value を Shift_JIS から UTF-8 に変換 def hash_sjis_to_utf8!(hash) hash.each do |key,value| case value when String hash[key] = NKF.nkf('-Sw -m0', value) when Hash hash_sjis_to_utf8!(value) end end end # モバイルかどうかを判定 def is_mobile? begin ua = request.user_agent ua =~ %r{^(DoCoMo|J-PHONE|Vodafone)/} || ua =~ %r{UP\.Browser/} rescue NoMethodError # テストのときは user_agent メソッドが存在しないので false を返しておく false end end end # View のテンプレート切り替え。 # モバイルの場合は app/views/mobile 以下からテンプレートを読み込みます。 class ::ActionView::Base private alias_method :full_template_path_without_mobile, :full_template_path def full_template_path(template_path, extension) if is_mobile? full_template_path_without_mobile("mobile/#{template_path}", extension) else full_template_path_without_mobile(template_path, extension) end end def is_mobile? if controller.is_a? ::ActionController::Base controller.send(:is_mobile?) else false end end end module ::ActionView::Helpers::FormTagHelper alias_method :form_tag_without_session_id, :form_tag def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) tag = form_tag_without_session_id(url_for_options, options, *parameters_for_url, &proc) if controller.send(:is_mobile?) session_key = ::ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:session_key] || '_session_id' tag + hidden_field_tag(session_key, request.session.session_id) else tag end end alias_method :start_form_tag, :form_tag end