detail_from_path ヘルパー : 小林研 Rails Tips (36)

はじめに

Rails Tips の 36 回目です。今日から授業が再開されるので、今日はサボり気味で detail_from_path にします。

detail_from_cap, detail_from_path

detail_from_capdetail_from_path は昨日までの title_from_path などと仕組みは同じです。key を title ではなく detail にしているだけです。テストは一括で紹介してしまいましょう。

  describe "common test" do
    it_behaves_like "単一メソッド呼び出し" do
      let(:test_set) do
        cap_nsr = {controller: "nyusen/suisens", action: "reception_list"}
        cap_tgi = {controller: "teacher/gakus", action: "index"}
        {
          detail_from_cap: [
            [cap_nsr], "推薦選抜の受付簿を作成します。",
            [cap_tgi], "学生室のトップページを表示します。",
          ],
          detail_from_path: [
            reception_list_nyusen_suisens_path, "推薦選抜の受付簿を作成します。",
            teacher_gakus_path, "学生室のトップページを表示します。",
          ],
        }
      end
    end
  end

昨日の views/ja.yml に detail も設定します。これを取得するだけです。

ja:
  nyusen:
    suisens:
      reception_list:
        title: 受付簿(推薦選抜)
        link_title: 推薦選抜受付簿作成
        detail: 推薦選抜の受付簿を作成します。
  teacher:
    gakus:
      index:
        title: 学生室トップページ
        detail: 学生室のトップページを表示します。

中身は昨日までと同様に key_from_cap で detail を取得しているだけです。

  # @param [Hash] cap コントローラ名・アクション名・パラメータのハッシュ
  # @return [String] 詳細
  def detail_from_cap(cap)
    key_from_cap cap, :detail
  end

  # @param [String] path path 名
  # @param [Symbol,nil] method メソッド名
  # @return [String] タイトル
  def detail_from_path(path, method = :get)
    detail_from_cap(controller_action_and_params(path, method))
  end

おわりに

detail はリンクメニューの説明文章を取得するものでした。これらのメニュー表示のためのヘルパーも後日紹介します。