require 'rails_helper'
class ModelNameTest
  extend ActiveModel::Naming
  def self.i18n_scope
    'activerecord'
  end
end
module ModelNameTestRootModel
  class NestedModelNameTest
    extend ActiveModel::Naming
    def self.i18n_scope
      'activerecord'
    end
  end
end

describe '.human_plural' do

  def with_translation_modifications(&block)
    original = I18n.backend.instance_variable_get('@translations').deep_dup
    block.call
    I18n.backend.instance_variable_set('@translations', original)
  end
  let(:translations){ I18n.backend.instance_variable_get('@translations') }
  it "works when translation is specified in locales" do
    User.model_name.human_plural.should eq 'Gebruikers'
  end

  it "Falls back to reasonable human.pluralize when no translation can be found" do
    ModelNameTest.model_name.human_plural.should eq 'Model name tests'
  end

  describe 'nested' do
    subject { ModelNameTestRootModel::NestedModelNameTest.model_name }

    it "falls back to unnested path if nested path is not given" do
      with_translation_modifications do
        translations[:nl][:activerecord][:models][:plural][:model_name_test_root_model] = "Non sti version human plural"
        subject.human_plural.should eq "Non sti version human plural"
      end
    end

    it "chooses the most specific translation if available" do
      with_translation_modifications do
        translations[:nl][:activerecord][:models][:plural][:model_name_test_root_model] = "Non sti version human plural"
        translations[:nl][:activerecord][:models][:plural][:"model_name_test_root_model/nested_model_name_test"] = "Sti version human plural"
        subject.human_plural.should eq "Sti version human plural"
      end
    end

    it "returns a resonable default when not given" do
      subject.human_plural.should eq 'Nested model name tests'
    end
  end
end
