Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 211 lines (174 sloc) 6.972 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
# encoding: utf-8
require 'spec_helper'

describe 'Formtastic::I18n' do

  FORMTASTIC_KEYS = [:required, :yes, :no, :create, :update].freeze

  it "should be defined" do
    lambda { Formtastic::I18n }.should_not raise_error(::NameError)
  end

  describe "default translations" do
    it "should be defined" do
      lambda { Formtastic::I18n::DEFAULT_VALUES }.should_not raise_error(::NameError)
      Formtastic::I18n::DEFAULT_VALUES.is_a?(::Hash).should == true
    end

    it "should exists for the core I18n lookup keys" do
      (Formtastic::I18n::DEFAULT_VALUES.keys & FORMTASTIC_KEYS).size.should == FORMTASTIC_KEYS.size
    end
  end

  describe "when I18n locales are available" do

    before do
      @formtastic_strings = {
          :yes => 'Default Yes',
          :no => 'Default No',
          :create => 'Default Create %{model}',
          :update => 'Default Update %{model}',
          :custom_scope => {
              :duck => 'Duck',
              :duck_pond => '%{ducks} ducks in a pond'
            }
        }
      ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
    end

    after do
      ::I18n.backend.reload!
    end

    it "should translate core strings correctly" do
      ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
      Formtastic::I18n.t(:required).should == "Default Required"
      Formtastic::I18n.t(:yes).should == "Default Yes"
      Formtastic::I18n.t(:no).should == "Default No"
      Formtastic::I18n.t(:create, :model => 'Post').should == "Default Create Post"
      Formtastic::I18n.t(:update, :model => 'Post').should == "Default Update Post"
    end

    it "should all belong to scope 'formtastic'" do
      Formtastic::I18n.t(:duck, :scope => [:custom_scope]).should == 'Duck'
    end

    it "should override default I18n lookup args if these are specified" do
      Formtastic::I18n.t(:duck_pond, :scope => [:custom_scope], :ducks => 15).should == '15 ducks in a pond'
    end

    it "should be possible to override default values" do
      Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!'
    end

  end

  describe "when no I18n locales are available" do

    before do
      ::I18n.backend.reload!
    end

    it "should use default strings" do
      (Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
        Formtastic::I18n.t(key, :model => '%{model}').should == Formtastic::I18n::DEFAULT_VALUES[key]
      end
    end

  end

  describe "I18n string lookups" do

    include FormtasticSpecHelper

    before do
      @output_buffer = ''
      mock_everything

      ::I18n.backend.store_translations :en, {:formtastic => {
          :labels => {
              :author => { :name => "Top author name transation" },
              :post => {:title => "Hello post!", :author => {:name => "Hello author name!"}},
              :project => {:title => "Hello project!"},
            }
        }, :helpers => {
          :label => {
            :post => {:body => "Elaborate..." },
            :author => { :login => "Hello login" }
          }
        }}

      @new_post.stub!(:title)
      @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255))
    end

    after do
      ::I18n.backend.reload!
    end

    it "lookup scopes should be defined" do
      with_config :i18n_lookups_by_default, true do
        lambda { Formtastic::I18n::SCOPES }.should_not raise_error(::NameError)
      end
    end

    it "should be able to translate with namespaced object" do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.input(:title))
        end)
        output_buffer.should have_tag("form label", /Hello post!/)
      end
    end

    it "should be able to translate without form-object" do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
          concat(builder.input(:title))
        end)
        output_buffer.should have_tag("form label", /Hello project!/)
      end
    end

    it "should be able to translate when method name is same as model" do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
          concat(builder.input(:author))
        end)
        output_buffer.should have_tag("form label", /Author/)
      end
    end

    it 'should be able to translate nested objects with nested translations' do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.semantic_fields_for(:author) do |f|
            concat(f.input(:name))
          end)
        end)
        output_buffer.should have_tag("form label", /Hello author name!/)
      end
    end

    it 'should be able to translate nested objects with top level translations' do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(@new_post) do |builder|
          builder.semantic_fields_for(:author) do |f|
            concat(f.input(:name))
          end
        end)
        output_buffer.should have_tag("form label", /Hello author name!/)
      end
    end

    it 'should be able to translate nested objects with nested object translations' do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(@new_post) do |builder|
          builder.semantic_fields_for(:project) do |f|
            concat(f.input(:title))
          end
        end)
        output_buffer.should have_tag("form label", /Hello project!/)
      end
    end
    
    it 'should be able to translate nested forms with top level translations' do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(:post) do |builder|
          builder.semantic_fields_for(:author) do |f|
            concat(f.input(:name))
          end
        end)
        output_buffer.should have_tag("form label", /Hello author name!/)
      end
    end

    it 'should be able to translate helper label as Rails does' do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.input(:body))
        end)
        output_buffer.should have_tag("form label", /Elaborate/)
      end
    end
    
    it 'should be able to translate nested helper label as Rails does' do
      with_config :i18n_lookups_by_default, true do
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.inputs(:for => :author) do |f|
            concat(f.input(:login))
          end)
        end)
        output_buffer.should have_tag("form label", /Hello login/)
      end
    end

    # TODO: Add spec for namespaced models?

  end

end
Something went wrong with that request. Please try again.