Monday, October 24, 2011

Autumn short vacation 2011

I take a autumn vacation, so I suspend posts and comments to this blog for a moment. Resume will be November 4, 2011.

Friday, October 21, 2011

make unobtrusive CoffeeScript on AWDwR 4th beta chapter15

On Agile Web Development with Rails 4th edition beta chapter15 (Internationalization), there is one more stuck point not-related with internationalization. In this chapter, JavaScript sample is not unobtrusive one. So I try to rewrite the sample with unobtrusive CoffeeScript.

application.js and application.html.erb

In this tutorial, we should use CoffeeScript only store.js.coffee. So we can omit to use another *.js.coffee files.
  1. In application.js file, delete the line //= require_tree ..
  2. In application.html.erb file, add the line <%= javascript_include_tag "store" %>.

make unobtrusive

In store.js.coffee, write the code for erase submit button and execute the action when drop down list is changed.
$ ->
  $('.locale input').hide()
........
$ ->
  $('#set_locale').change ->
    $(this).parent('form.locale').submit()
so that we just write the drop down list in layouts/application.html.erb like below:
    <%= form_tag store_path, class: 'locale' do %>
      <%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s) %>
      <%= submit_tag 'submit' %>
    <% end %>

Tuesday, October 18, 2011

Addenda for AWDwR 4th beta chapter15

Agile Web Development with Rails 4th edition beta has a quality almost like final version. But, chapter15 (Internationalization) just has beta one. There are some stuck points. So I try to explain these points.

Configure default locale and available locales

In this book, author creates the file config/initializers/i18n.rb and configure default locale and locales for drop down list in this file. But, there are two better ways:
  1. This sample does not explain the code for available locales.
  2. There are some definitions for locale in config/application.rb file.
So that we should write below in config/application.rb file, not config/initializers/i18n.rb file:
# -*- coding: UTF-8 -*-
........
LANGUAGES = [
  ['English', 'en'],
  ['日本語'.html_safe, 'ja']
]


module Depot
  class Application < Rails::Application
........
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :en
    config.i18n.available_locales = LANGUAGES.map(&:last)

........
  end
end

default_url_options in controller

In this book, method default_url_options is inplemented in ApplicationController class. But, this brings two troublesome points.
  1. default_url_options would take an optional argument for hash.
  2. This works when server running or functional test running, but does not when integration test running.
So that we re-write these:
application_controller.rb
  def default_url_options(options={})
    options.merge({locale: I18n.locale})
  end
In each integration tests
  def setup
    app.default_url_options = { locale: I18n.locale }
  end

Saturday, October 15, 2011

Rails 3.1.1 never depends on bcrypt-ruby, but must be required.

One week ago, Ruby on Rails 3.1.1 has been released. Since this release, rails never depends on bcrypt-ruby. But, when you use some functions related with encryption, you need to choice from some encryption gems.

For example, if you want to use has_secure_password, you should write onto Gemfile like below:
gem 'bcrypt-ruby', '~> 3.0.0'

Wednesday, October 12, 2011

MySQL Connector/Net 6.4.4 has been released

In two weeks ago, MySQL Connector/Net 6.4.4 has been released(Check change log).

You may think this release is small bug-fix one. But, for ASP.NET MVC3 / EntityFramework4 developer, this contains big change. Since this release, "Code first" function runs correctly with PluralizingTableNameConvention. So you don't have to remove this convention like this post anymore!