Commit db9b5366c4a1007eb41f2d56f8c35d242de3fed8

Authored by Rafael Manzo
1 parent a1bd9d6f

Better organization for CompoundMetricConfiguration translations

Cretated helper t_hint for easy hint translation retrieval

Signed off by: Daniel Miranda <danielkza2@gmail.com>
app/helpers/application_helper.rb
1 module ApplicationHelper 1 module ApplicationHelper
  2 + def t_hint(attribute_key, class_key=nil)
  3 + class_key ||= controller_name.singularize
  4 + t("activemodel.hints.#{class_key}.#{attribute_key}")
  5 + end
2 end 6 end
app/helpers/compound_metric_configurations_helper.rb
@@ -2,4 +2,13 @@ module CompoundMetricConfigurationsHelper @@ -2,4 +2,13 @@ module CompoundMetricConfigurationsHelper
2 def scope_options 2 def scope_options
3 [[t("scopes.METHOD"),"METHOD"], [t("scopes.CLASS"), "CLASS"], [t("scopes.PACKAGE"), "PACKAGE"], [t("scopes.SOFTWARE"), "SOFTWARE"]] 3 [[t("scopes.METHOD"),"METHOD"], [t("scopes.CLASS"), "CLASS"], [t("scopes.PACKAGE"), "PACKAGE"], [t("scopes.SOFTWARE"), "SOFTWARE"]]
4 end 4 end
  5 +
  6 + def compound_metric_human_name(count=1)
  7 + key = count > 1 ? 'other': 'one'
  8 + t("activemodel.models.compound_metric_configuration.#{key}")
  9 + end
  10 +
  11 + def compound_metric_human_attribute_name(attribute_key)
  12 + t("activemodel.attributes.compound_metric_configuration.#{attribute_key}")
  13 + end
5 end 14 end
app/views/compound_metric_configurations/_form.html.erb
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 8
9 <div class="form-row"> 9 <div class="form-row">
10 <div class="field-container"> 10 <div class="field-container">
11 - <%= f.label :weight, t('weight'), class: 'control-label' %> 11 + <%= f.label :weight, compound_metric_human_attribute_name('weight'), class: 'control-label' %>
12 <%= f.text_field :weight, :required => true, class: 'text-field form-control' %> 12 <%= f.text_field :weight, :required => true, class: 'text-field form-control' %>
13 </div> 13 </div>
14 <div class="help-container"> 14 <div class="help-container">
@@ -25,7 +25,7 @@ @@ -25,7 +25,7 @@
25 <% else %> 25 <% else %>
26 <div class="form-row"> 26 <div class="form-row">
27 <div class="field-container"> 27 <div class="field-container">
28 - <%= f.label :reading_group_id, t('reading_group'), class: 'control-label' %> 28 + <%= f.label :reading_group_id, ReadingGroup.model_name.human, class: 'control-label' %>
29 <%= f.select( :reading_group_id, reading_group_options, {class: 'form-control'} ) %> 29 <%= f.select( :reading_group_id, reading_group_options, {class: 'form-control'} ) %>
30 </div> 30 </div>
31 <div class="help-container"> 31 <div class="help-container">
app/views/compound_metric_configurations/_metric_options.html.erb
1 <div class="form-row"> 1 <div class="form-row">
2 <div class="field-container"> 2 <div class="field-container">
3 - <%= f.label :name, t('name'), class: 'control-label' %> 3 + <%= f.label :name, compound_metric_human_attribute_name('name'), class: 'control-label' %>
4 <%= f.text_field :name, :required => true, class: 'text-field form-control', value: (metric.name unless metric.nil?) %> 4 <%= f.text_field :name, :required => true, class: 'text-field form-control', value: (metric.name unless metric.nil?) %>
5 </div> 5 </div>
6 <div class="help-container"> 6 <div class="help-container">
7 <p> 7 <p>
8 - <%= t('body1_compound_metric_options') %> 8 + <%= t_hint(:name) %>
9 </p> 9 </p>
10 </div> 10 </div>
11 </div> 11 </div>
12 12
13 <div class="form-row"> 13 <div class="form-row">
14 <div class="field-container"> 14 <div class="field-container">
15 - <%= f.label :description, t('description'), class: 'control-label' %> 15 + <%= f.label :description, compound_metric_human_attribute_name('description'), class: 'control-label' %>
16 <%= f.text_field :description, class: 'text-field form-control', value: (metric.description unless metric.nil?) %> 16 <%= f.text_field :description, class: 'text-field form-control', value: (metric.description unless metric.nil?) %>
17 </div> 17 </div>
18 <div class="help-container"> 18 <div class="help-container">
19 <p> 19 <p>
20 - <%= t('body2_compound_metric_options') %> 20 + <%= t_hint(:description) %>
21 </p> 21 </p>
22 </div> 22 </div>
23 </div> 23 </div>
24 24
25 <div class="form-row"> 25 <div class="form-row">
26 <div class="field-container"> 26 <div class="field-container">
27 - <%= f.label :script, t('script'), class: 'control-label' %> 27 + <%= f.label :script, compound_metric_human_attribute_name('script'), class: 'control-label' %>
28 <%= f.text_area :script, :required => true, class: 'text-area form-control', value: (metric.script unless metric.nil?) %> 28 <%= f.text_area :script, :required => true, class: 'text-area form-control', value: (metric.script unless metric.nil?) %>
29 </div> 29 </div>
30 <div class="help-container"> 30 <div class="help-container">
31 <p> 31 <p>
32 - <%= t('body3_compound_metric_options') %> 32 + <%= t_hint(:script) %>
33 </p> 33 </p>
34 </div> 34 </div>
35 </div> 35 </div>
36 36
37 <div class="form-row"> 37 <div class="form-row">
38 <div class="field-container"> 38 <div class="field-container">
39 - <%= f.label :scope, t('scope'), class: 'control-label' %> 39 + <%= f.label :scope, compound_metric_human_attribute_name('scope'), class: 'control-label' %>
40 <%= f.select( :scope, scope_options, {class: 'form-control', selected: (metric.scope unless metric.nil?)} ) %> 40 <%= f.select( :scope, scope_options, {class: 'form-control', selected: (metric.scope unless metric.nil?)} ) %>
41 </div> 41 </div>
42 <div class="help-container"> 42 <div class="help-container">
@@ -48,16 +48,16 @@ @@ -48,16 +48,16 @@
48 48
49 <div class="form-row"> 49 <div class="form-row">
50 <div class="field-container"> 50 <div class="field-container">
51 - <%= f.label :code, t('code'), class: 'control-label' %> 51 + <%= f.label :code, compound_metric_human_attribute_name('code'), class: 'control-label' %>
52 <%= f.text_field :code, :required => true, class: 'text-field form-control' %> 52 <%= f.text_field :code, :required => true, class: 'text-field form-control' %>
53 </div> 53 </div>
54 <div class="help-container"> 54 <div class="help-container">
55 <p> 55 <p>
56 - <%= t('body1_compound_form') %> 56 + <%= t_hint('code.description') %>
57 </p> 57 </p>
58 <br> 58 <br>
59 <p> 59 <p>
60 - <%= t('body1_compound_form_example') %> 60 + <%= t_hint('code.example') %>
61 </p> 61 </p>
62 </div> 62 </div>
63 </div> 63 </div>
app/views/compound_metric_configurations/edit.html.erb
1 <div class="page-header"> 1 <div class="page-header">
2 - <h1> <%= t('title_compound_edit') %> </h1> 2 + <h1> <%= t('edit_model', model: compound_metric_human_name) %> </h1>
3 </div 3 </div
4 4
5 <%= render partial: 'created_metrics', locals: {metric_configurations: @metric_configurations} %> 5 <%= render partial: 'created_metrics', locals: {metric_configurations: @metric_configurations} %>
app/views/compound_metric_configurations/new.html.erb
1 <div class="page-header"> 1 <div class="page-header">
2 - <h1> <%= t('title_compound_new') %> </h1> 2 + <h1> <%= "#{t('new.female')} #{compound_metric_human_name}" %> </h1>
3 </div> 3 </div>
4 4
5 <%= render partial: 'created_metrics', locals: {metric_configurations: @metric_configurations} %> 5 <%= render partial: 'created_metrics', locals: {metric_configurations: @metric_configurations} %>
config/locales/views/compound_metric_configuration/en.yml
1 en: 1 en:
2 - body1_compound_form: "A variable that holds the value of a metric calculation. It can be used in the script of another compound metric."  
3 - body2_compound_form: " It is used to calculate the weighted average of a"  
4 - body1_compound_form_example: "Example: Code foo := 10"  
5 - body1_compound_metric_options: "A custom name for your compound metric."  
6 - body2_compound_metric_options: "A short description of your compound metric."  
7 - body3_compound_metric_options: "A mathematical expression (in javascript) to calculate your compound metric. It needs a return statement. The codes of already created metrics can be used inside the script."  
8 - title_compound_edit: "Edit Compound Metric Configuration"  
9 - title_compound_new: "New Compound Metric Configuration"  
10 - compound_metric: "Compound Metric"  
11 - 2 + activemodel:
  3 + models:
  4 + compound_metric_configuration:
  5 + one: "Compound Metric Configuration"
  6 + other: "Compound Metric Configurations"
  7 + attributes:
  8 + compound_metric_configuration:
  9 + name: "Name"
  10 + description: "Description"
  11 + script: "Script"
  12 + scope: "Scope"
  13 + code: "Code"
  14 + weight: "Weight"
  15 + hints:
  16 + compound_metric_configuration:
  17 + name: "A custom name for your compound metric."
  18 + description: "A short description of your compound metric."
  19 + script: "A mathematical expression (in javascript) to calculate your compound metric. It needs a return statement. The codes of already created metrics can be used inside the script."
  20 + code:
  21 + description: "A variable that holds the value of a metric calculation. It can be used in the script of another compound metric."
  22 + example: "Example: Code foo := 10"
  23 + weight: "It is used to calculate the weighted average of a"
config/locales/views/compound_metric_configuration/pt.yml
1 pt: 1 pt:
2 - body1_compound_form: "Uma variável que contém o valor de um cálculo de métrica. Ele pode ser utilizado no script de outra métrica composta."  
3 - body1_compound_form_example: "Exemplo: Código foo := 10"  
4 - body1_compound_metric_options: "Um nome personalizado para a sua métrica composta."  
5 - body2_compound_metric_options: "Uma breve descrição de sua métrica composta."  
6 - body3_compound_metric_options: "A expressão matemática (em javascript) para calcular a sua métrica composta. Ela precisa de uma instrução de retorno (por exemplo, 'return resultado;'). Os códigos de métricas já criadas podem ser usados dentro do script."  
7 - title_compound_edit: "Editar Configuração de Métrica Composta"  
8 - title_compound_new: "Nova Configuração de Métrica Composta"  
9 - compound_metric: "Métrica Composta" 2 + activemodel:
  3 + models:
  4 + compound_metric_configuration:
  5 + one: "Configuração de Métrica Composta"
  6 + other: "Configurações de Métricas Compostas"
  7 + attributes:
  8 + compound_metric_configuration:
  9 + name: "Nome"
  10 + description: "Descrição"
  11 + script: "Script"
  12 + scope: "Escopo"
  13 + code: "Código"
  14 + weight: "Peso"
  15 + hints:
  16 + compound_metric_configuration:
  17 + name: "Um nome personalizado para sua métrica composta."
  18 + description: "Uma descrição curta de sua métrica composta."
  19 + script: "Uma expressão matemática (em JavaScript) para calcular sua métrica composta. Ela precisa de uma cláusula de retorno. O código de suas demais métricas pode ser utilizado dentro deste script."
  20 + code:
  21 + description: "Uma variável que contém o valor do cálculo da métrica. Ela pode ser usada no script de outra métrica composa."
  22 + example: "Examplo: Código foo := 10"
  23 + weight: "ele é usado para o cálculo de uma média ponderada de um"
features/compound_metric_configuration/create.feature
@@ -13,18 +13,18 @@ Feature: Compound Metric Configuration Creation @@ -13,18 +13,18 @@ Feature: Compound Metric Configuration Creation
13 And I am at the Sample Configuration page 13 And I am at the Sample Configuration page
14 And I click the Add Metric link 14 And I click the Add Metric link
15 And I click the Compound Metric link 15 And I click the Compound Metric link
16 - When I click the "Created Metrics" h3  
17 Then I see the sample metric configuration name 16 Then I see the sample metric configuration name
18 And I see the sample metric configuration code 17 And I see the sample metric configuration code
19 When I fill the Name field with "My Compound Metric" 18 When I fill the Name field with "My Compound Metric"
20 And I fill the Description field with "Some description" 19 And I fill the Description field with "Some description"
21 And I fill the Code field with "mcm" 20 And I fill the Code field with "mcm"
22 - And I fill the Script field with "8*8;" 21 + And I fill the Script field with "return 8*8;"
23 And I fill the Weight field with "8" 22 And I fill the Weight field with "8"
24 And I set the select field "Scope" as "Class" 23 And I set the select field "Scope" as "Class"
25 And I set the select field "Reading Group" as "Scholar" 24 And I set the select field "Reading Group" as "Scholar"
26 And I press the Save button 25 And I press the Save button
27 - When I click the show link of "My Compound Metric" 26 + Then I should see "Compound Metric Configuration was successfully created."
  27 + And I click the show link of "My Compound Metric"
28 Then I should see "My Compound Metric" 28 Then I should see "My Compound Metric"
29 And I should see "mcm" 29 And I should see "mcm"
30 And I should see "8" 30 And I should see "8"
features/step_definitions/user_steps.rb
@@ -7,11 +7,11 @@ When(/^I press the (.+) button$/) do |text| @@ -7,11 +7,11 @@ When(/^I press the (.+) button$/) do |text|
7 end 7 end
8 8
9 When(/^I fill the (.+) field with "(.+)"$/) do |field, text| 9 When(/^I fill the (.+) field with "(.+)"$/) do |field, text|
10 - fill_in I18n.t(field.gsub(" ", "_").downcase), :with => text 10 + fill_in field, :with => text
11 end 11 end
12 12
13 Then(/^the field "(.*?)" should be filled with "(.*?)"$/) do |field, value| 13 Then(/^the field "(.*?)" should be filled with "(.*?)"$/) do |field, value|
14 - expect(page.find_field(I18n.t(field.gsub(" ", "_").downcase)).value).to eq(value) 14 + expect(page.find_field(field).value).to eq(value)
15 end 15 end
16 16
17 Then(/^my name should have changed to (.+)$/) do |text| 17 Then(/^my name should have changed to (.+)$/) do |text|
spec/helpers/application_helper_spec.rb 0 → 100644
@@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
  1 +require 'rails_helper'
  2 +
  3 +describe ApplicationHelper, :type => :helper do
  4 + describe 't_hint' do
  5 + let(:class_key) { :compound_metric_configuration }
  6 + let(:attribute_key) { :script }
  7 +
  8 + context 'without class_key' do
  9 + let!(:translation) { "translated test" }
  10 +
  11 + before :each do
  12 + helper.expects(:t).with("activemodel.hints.#{helper.controller_name}.#{attribute_key}").returns(translation)
  13 + end
  14 +
  15 + it 'is expected to return the hint for the given attribute' do
  16 + expect(helper.t_hint(attribute_key)).to eq(translation)
  17 + end
  18 + end
  19 +
  20 + context 'with class_key' do
  21 + it 'is expected to return the hint for the given attribute' do
  22 + expect(helper.t_hint(attribute_key, class_key)).to eq(I18n.t("activemodel.hints.#{class_key}.#{attribute_key}"))
  23 + end
  24 + end
  25 + end
  26 +end
0 \ No newline at end of file 27 \ No newline at end of file
spec/helpers/compound_metric_configurations_helper_spec.rb
@@ -6,4 +6,27 @@ describe CompoundMetricConfigurationsHelper, :type =&gt; :helper do @@ -6,4 +6,27 @@ describe CompoundMetricConfigurationsHelper, :type =&gt; :helper do
6 expect(helper.scope_options).to eq [["Method","METHOD"], ["Class", "CLASS"], ["Package", "PACKAGE"], ["Software", "SOFTWARE"]] 6 expect(helper.scope_options).to eq [["Method","METHOD"], ["Class", "CLASS"], ["Package", "PACKAGE"], ["Software", "SOFTWARE"]]
7 end 7 end
8 end 8 end
  9 +
  10 + describe 'compound_metric_human_name' do
  11 + context 'with one' do
  12 + it 'is expected to return the class name internationalized' do
  13 + expect(helper.compound_metric_human_name).to eq(I18n.t('activemodel.models.compound_metric_configuration.one'))
  14 + end
  15 + end
  16 +
  17 + context 'with more than one' do
  18 + it 'is expected to return the class name internationalized and pluralized' do
  19 + expect(helper.compound_metric_human_name(2)).to eq(I18n.t('activemodel.models.compound_metric_configuration.other'))
  20 + expect(helper.compound_metric_human_name(50)).to eq(I18n.t('activemodel.models.compound_metric_configuration.other'))
  21 + end
  22 + end
  23 + end
  24 +
  25 + describe 'compound_metric_human_attribute_name' do
  26 + let(:attribute) { :name }
  27 +
  28 + it 'is expected to return the attribute name internationalized' do
  29 + expect(helper.compound_metric_human_attribute_name(attribute)).to eq(I18n.t("activemodel.attributes.compound_metric_configuration.#{attribute}"))
  30 + end
  31 + end
9 end 32 end