Commit a4ea9a93 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add ChroniDurationAttribute concern

parent 3c23cefa
...@@ -3,6 +3,7 @@ module Ci ...@@ -3,6 +3,7 @@ module Ci
extend Gitlab::Ci::Model extend Gitlab::Ci::Model
include Gitlab::SQL::Pattern include Gitlab::SQL::Pattern
include RedisCacheable include RedisCacheable
include ChronicDurationAttribute
RUNNER_QUEUE_EXPIRY_TIME = 60.minutes RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
ONLINE_CONTACT_TIMEOUT = 1.hour ONLINE_CONTACT_TIMEOUT = 1.hour
...@@ -51,6 +52,8 @@ module Ci ...@@ -51,6 +52,8 @@ module Ci
cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at, :ip_address cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at, :ip_address
chronic_duration_attribute :job_upper_timeout_user_readable, :job_upper_timeout
# Searches for runners matching the given query. # Searches for runners matching the given query.
# #
# This method uses ILIKE on PostgreSQL and LIKE on MySQL. # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
......
module ChronicDurationAttribute
extend ActiveSupport::Concern
class_methods do
def chronic_duration_attribute(virtual_attribute, source_attribute)
chronic_duration_attribute_reader(virtual_attribute, source_attribute)
chronic_duration_attribute_writer(virtual_attribute, source_attribute)
end
def chronic_duration_attribute_reader(virtual_attribute, source_attribute)
define_method(virtual_attribute) do
value = self.send(source_attribute) # rubocop:disable GitlabSecurity/PublicSend
ChronicDuration.output(value, format: :short) unless value.nil?
end
end
def chronic_duration_attribute_writer(virtual_attribute, source_attribute)
define_method("#{virtual_attribute}=") do |value|
new_value = ChronicDuration.parse(value).to_i
self.send("#{source_attribute}=", new_value) # rubocop:disable GitlabSecurity/PublicSend
new_value
end
end
end
end
require 'spec_helper'
shared_examples 'ChronicDurationAttribute' do
describe 'dynamically defined methods' do
it { expect(subject.class).to be_public_method_defined(virtual_field) }
it { expect(subject.class).to be_public_method_defined("#{virtual_field}=") }
it 'parses chronic duration input' do
subject.send("#{virtual_field}=", "10m")
expect(subject.send(source_field)).to eq(600)
end
it 'outputs chronic duration formated value' do
subject.send("#{source_field}=", 120)
expect(subject.send(virtual_field)).to eq('2m')
end
end
end
describe 'ChronicDurationAttribute' do
let(:source_field) { :maximum_job_timeout }
let(:virtual_field) { :maximum_job_timeout_user_readable }
subject { Ci::Runner.new }
it_behaves_like 'ChronicDurationAttribute'
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment