Commit 7649725c4838acf236d137dd945e862b4b4d7cf5
1 parent
2f2ddebb
Exists in
colab
and in
4 other branches
MetricConfigurations inherits from KalibroClient class
Showing
10 changed files
with
5 additions
and
152 deletions
Show diff stats
app/controllers/base_metric_configurations_controller.rb
| ... | ... | @@ -16,7 +16,7 @@ class BaseMetricConfigurationsController < ApplicationController |
| 16 | 16 | def show |
| 17 | 17 | if metric_configuration |
| 18 | 18 | @reading_group = ReadingGroup.find(metric_configuration.reading_group_id) |
| 19 | - @mezuro_ranges = metric_configuration.mezuro_ranges | |
| 19 | + @mezuro_ranges = metric_configuration.kalibro_ranges | |
| 20 | 20 | metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i |
| 21 | 21 | else |
| 22 | 22 | raise NotImplementedError | ... | ... |
app/models/metric_configuration.rb
| 1 | -require "validators/code_uniqueness_validator.rb" | |
| 2 | -require "validators/name_script_presence_validator.rb" | |
| 3 | - | |
| 4 | -class MetricConfiguration < KalibroGatekeeperClient::Entities::MetricConfiguration | |
| 5 | - include KalibroRecord | |
| 6 | - | |
| 7 | - attr_accessor :code, :weight, :aggregation_form | |
| 8 | - | |
| 9 | - validates :code, presence: true, code_uniqueness: true | |
| 10 | - validates :weight, presence: true, numericality: { greater_than: 0 } | |
| 11 | - validates :aggregation_form, presence: true, unless: "metric.compound == true" | |
| 12 | - validates_with NameScriptPresenceValidator, unless: "metric.compound == false" | |
| 13 | - | |
| 14 | - def mezuro_ranges | |
| 15 | - MezuroRange.ranges_of self.id | |
| 16 | - end | |
| 17 | -end | |
| 1 | +class MetricConfiguration < KalibroClient::Configurations::MetricConfiguration; end | ... | ... |
app/models/validators/code_uniqueness_validator.rb
| ... | ... | @@ -1,10 +0,0 @@ |
| 1 | -class CodeUniquenessValidator < ActiveModel::EachValidator | |
| 2 | - def validate_each(record, attribute, value) | |
| 3 | - record.class.metric_configurations_of(record.configuration_id).each do |metric_configuration| | |
| 4 | - if metric_configuration.code == value && metric_configuration.id != record.id | |
| 5 | - record.errors[attribute] << "There is already a #{record.class} with #{attribute} #{value}! Please, choose another one." | |
| 6 | - break | |
| 7 | - end | |
| 8 | - end | |
| 9 | - end | |
| 10 | -end | |
| 11 | 0 | \ No newline at end of file |
app/models/validators/name_script_presence_validator.rb
| ... | ... | @@ -1,10 +0,0 @@ |
| 1 | -class NameScriptPresenceValidator < ActiveModel::Validator | |
| 2 | - def validate(record) | |
| 3 | - if record.metric.name.strip.empty? | |
| 4 | - record.errors[:name] << "can't be blank" | |
| 5 | - end | |
| 6 | - if record.metric.script.strip.empty? | |
| 7 | - record.errors[:script] << "can't be blank" | |
| 8 | - end | |
| 9 | - end | |
| 10 | -end |
spec/controllers/base_metric_configurations_controller_spec.rb
| ... | ... | @@ -119,7 +119,7 @@ describe InheritsFromBaseMetricConfigurationsController, :type => :controller do |
| 119 | 119 | before :each do |
| 120 | 120 | ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) |
| 121 | 121 | subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration) |
| 122 | - MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) | |
| 122 | + metric_configuration.expects(:kalibro_ranges).returns([mezuro_range]) | |
| 123 | 123 | |
| 124 | 124 | get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id |
| 125 | 125 | end | ... | ... |
spec/controllers/compound_metric_configurations_controller_spec.rb
| ... | ... | @@ -75,7 +75,7 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 75 | 75 | before :each do |
| 76 | 76 | ReadingGroup.expects(:find).with(compound_metric_configuration.reading_group_id).returns(reading_group) |
| 77 | 77 | subject.expects(:find_resource).with(MetricConfiguration, compound_metric_configuration.id).returns(compound_metric_configuration) |
| 78 | - MezuroRange.expects(:ranges_of).with(compound_metric_configuration.id).returns([mezuro_range]) | |
| 78 | + compound_metric_configuration.expects(:kalibro_ranges).returns([mezuro_range]) | |
| 79 | 79 | |
| 80 | 80 | get :show, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s, id: compound_metric_configuration.id |
| 81 | 81 | end | ... | ... |
spec/controllers/metric_configurations_controller_spec.rb
| ... | ... | @@ -96,7 +96,7 @@ describe MetricConfigurationsController, :type => :controller do |
| 96 | 96 | before :each do |
| 97 | 97 | ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) |
| 98 | 98 | subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration) |
| 99 | - MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) | |
| 99 | + metric_configuration.expects(:kalibro_ranges).returns([mezuro_range]) | |
| 100 | 100 | |
| 101 | 101 | get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id |
| 102 | 102 | end | ... | ... |
spec/models/metric_configuration_spec.rb
| ... | ... | @@ -1,43 +0,0 @@ |
| 1 | -require 'rails_helper' | |
| 2 | - | |
| 3 | -describe MetricConfiguration, :type => :model do | |
| 4 | - subject {FactoryGirl.build(:metric_configuration)} | |
| 5 | - | |
| 6 | - describe 'methods' do | |
| 7 | - describe 'mezuro_ranges' do | |
| 8 | - let(:mezuro_range) { FactoryGirl.build(:mezuro_range) } | |
| 9 | - before :each do | |
| 10 | - MezuroRange.expects(:ranges_of).with(subject.id).returns([mezuro_range]) | |
| 11 | - end | |
| 12 | - | |
| 13 | - it 'should returns a list with its ranges' do | |
| 14 | - expect(subject.mezuro_ranges).to eq([mezuro_range]) | |
| 15 | - end | |
| 16 | - end | |
| 17 | - end | |
| 18 | - | |
| 19 | - describe 'validations' do | |
| 20 | - | |
| 21 | - context 'active model validations' do | |
| 22 | - before :each do | |
| 23 | - MetricConfiguration.expects(:metric_configurations_of).at_least_once.returns([]) | |
| 24 | - end | |
| 25 | - | |
| 26 | - it { is_expected.to validate_presence_of(:code) } | |
| 27 | - it { is_expected.to validate_presence_of(:weight) } | |
| 28 | - it { is_expected.to validate_numericality_of(:weight) } | |
| 29 | - it { is_expected.to validate_presence_of(:aggregation_form) } | |
| 30 | - end | |
| 31 | - | |
| 32 | - context 'code validations' do | |
| 33 | - before :each do | |
| 34 | - MetricConfiguration.expects(:request).returns(42) | |
| 35 | - end | |
| 36 | - | |
| 37 | - it 'should validate uniqueness' do | |
| 38 | - CodeUniquenessValidator.any_instance.expects(:validate_each).with(subject, :code, subject.code) | |
| 39 | - subject.save | |
| 40 | - end | |
| 41 | - end | |
| 42 | - end | |
| 43 | -end |
spec/models/validators/code_uniqueness_validator_spec.rb
| ... | ... | @@ -1,32 +0,0 @@ |
| 1 | -require 'rails_helper' | |
| 2 | - | |
| 3 | -describe CodeUniquenessValidator, :type => :model do | |
| 4 | - describe 'methods' do | |
| 5 | - describe 'validate_each' do | |
| 6 | - context 'without saved metric_configurations' do | |
| 7 | - before :each do | |
| 8 | - MetricConfiguration.expects(:metric_configurations_of).returns([]) | |
| 9 | - MetricConfiguration.expects(:request).returns(42) | |
| 10 | - end | |
| 11 | - | |
| 12 | - subject { FactoryGirl.build(:metric_configuration) } | |
| 13 | - it 'should contain no errors' do | |
| 14 | - subject.save | |
| 15 | - expect(subject.errors).to be_empty | |
| 16 | - end | |
| 17 | - end | |
| 18 | - | |
| 19 | - context 'with code already taken by another metric_configuration' do | |
| 20 | - before :each do | |
| 21 | - @subject = FactoryGirl.build(:metric_configuration) | |
| 22 | - MetricConfiguration.expects(:metric_configurations_of).with(@subject.configuration_id).returns([FactoryGirl.build(:metric_configuration, id: @subject.id + 1)]) | |
| 23 | - end | |
| 24 | - | |
| 25 | - it 'should contain errors' do | |
| 26 | - @subject.save | |
| 27 | - expect(@subject.errors[:code]).to eq(["There is already a MetricConfiguration with code #{@subject.code}! Please, choose another one."]) | |
| 28 | - end | |
| 29 | - end | |
| 30 | - end | |
| 31 | - end | |
| 32 | -end |
spec/models/validators/name_script_validator_spec.rb
| ... | ... | @@ -1,36 +0,0 @@ |
| 1 | -require 'rails_helper' | |
| 2 | - | |
| 3 | -describe NameScriptPresenceValidator, :type => :model do | |
| 4 | - describe 'methods' do | |
| 5 | - describe 'validate' do | |
| 6 | - let!(:compound_metric_configuration){ FactoryGirl.build(:compound_metric_configuration) } | |
| 7 | - | |
| 8 | - before :each do | |
| 9 | - CodeUniquenessValidator.any_instance.stubs(:validate_each) | |
| 10 | - end | |
| 11 | - | |
| 12 | - context 'with blank name' do | |
| 13 | - before :each do | |
| 14 | - compound_metric_configuration.metric.name = "" | |
| 15 | - end | |
| 16 | - | |
| 17 | - it 'is expected to return a error for the name field' do | |
| 18 | - compound_metric_configuration.save | |
| 19 | - expect(compound_metric_configuration.errors[:name]).to include("can't be blank") | |
| 20 | - end | |
| 21 | - | |
| 22 | - context 'with blank script' do | |
| 23 | - before :each do | |
| 24 | - compound_metric_configuration.metric.script = "" | |
| 25 | - end | |
| 26 | - | |
| 27 | - it 'is expected to return a error for the name and script fields' do | |
| 28 | - compound_metric_configuration.save | |
| 29 | - expect(compound_metric_configuration.errors[:name]).to include("can't be blank") | |
| 30 | - expect(compound_metric_configuration.errors[:script]).to include("can't be blank") | |
| 31 | - end | |
| 32 | - end | |
| 33 | - end | |
| 34 | - end | |
| 35 | - end | |
| 36 | -end | |
| 37 | 0 | \ No newline at end of file |