Commit a4a40c576fbfa3fce52ea00cf9e766db7bcc87b6
1 parent
c324d615
Exists in
colab
and in
4 other branches
Add helper for translating actions for models
Showing
2 changed files
with
25 additions
and
0 deletions
Show diff stats
app/helpers/application_helper.rb
| ... | ... | @@ -3,4 +3,12 @@ module ApplicationHelper |
| 3 | 3 | class_key ||= controller_name.singularize |
| 4 | 4 | t("activemodel.hints.#{class_key}.#{attribute_key}", options) |
| 5 | 5 | end |
| 6 | + | |
| 7 | + def t_action(action, model, options={}) | |
| 8 | + options[:default] = "helpers.submit.#{action}".to_sym | |
| 9 | + count = options.delete(:count) || 1 | |
| 10 | + options[:model] = model.model_name.human(count: count) | |
| 11 | + | |
| 12 | + t("helpers.submit.#{model.model_name.i18n_key}.#{action}", options) | |
| 13 | + end | |
| 6 | 14 | end | ... | ... |
spec/helpers/application_helper_spec.rb
| ... | ... | @@ -23,4 +23,21 @@ describe ApplicationHelper, :type => :helper do |
| 23 | 23 | end |
| 24 | 24 | end |
| 25 | 25 | end |
| 26 | + | |
| 27 | + describe 't_action' do | |
| 28 | + let!(:model) { mock "Model" } | |
| 29 | + let!(:model_name) { mock "Model Name" } | |
| 30 | + let(:action) { :edit } | |
| 31 | + | |
| 32 | + before :each do | |
| 33 | + model.expects(:model_name).twice.returns(model_name) | |
| 34 | + model_name.expects(:i18n_key).returns(:model) | |
| 35 | + model_name.expects(:human).with(count: 1).returns("Model") | |
| 36 | + helper.expects(:t).with("helpers.submit.model.edit", {default: "helpers.submit.edit".to_sym, model: "Model"}).returns("Edit Model") | |
| 37 | + end | |
| 38 | + | |
| 39 | + it "is expected to return the Model's edit action translation" do | |
| 40 | + expect(helper.t_action(action, model)).to eq("Edit Model") | |
| 41 | + end | |
| 42 | + end | |
| 26 | 43 | end | ... | ... |