Skip to content

Commit

Permalink
Move apply_dialog_settings logic into DialogProperties class
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcculloug committed Jul 30, 2018
1 parent 12b109c commit e00b17b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 17 deletions.
20 changes: 3 additions & 17 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Service < ApplicationRecord
virtual_has_one :reconfigure_dialog

before_validation :set_tenant_from_group
before_create :apply_dialog_settings
before_create :update_attributes_from_dialog

delegate :provision_dialog, :to => :miq_request, :allow_nil => true
delegate :user, :to => :miq_request, :allow_nil => true
Expand Down Expand Up @@ -463,21 +463,7 @@ def remove_from_service(parent_service)
def configuration_script
end

private

def apply_dialog_settings
dialog_options = options[:dialog] || {}

%w(dialog_service_name dialog_service_description).each do |field_name|
send(field_name, dialog_options[field_name]) if dialog_options.key?(field_name)
end
end

def dialog_service_name(value)
self.name = value if value.present?
end

def dialog_service_description(value)
self.description = value if value.present?
private def update_attributes_from_dialog
Service::DialogProperties.parse(options[:dialog]).each { |key, value| self[key] = value }
end
end
40 changes: 40 additions & 0 deletions app/models/service/dialog_properties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Service
class DialogProperties
def initialize(options)
@attributes = {}
@options = options || {}
end

def self.parse(options)
new(options).parse
end

def parse
@attributes.tap { parse_options }
end

private

def parse_options
%w(name description).each do |field_name|
dialog_field_name = "dialog_service_#{field_name}"
dialog_field_value = @options[dialog_field_name]
parse_value(field_name, dialog_field_name, dialog_field_value) if @options.key?(dialog_field_name)
end
end

def parse_value(field_name, dialog_field_name, dialog_field_value)
send(field_name, dialog_field_value) if @options.key?(dialog_field_name)
rescue StandardError => err
$log.error("Error setting key [#{dialog_field_name}] with value [#{dialog_field_value.inspect}] error: [#{err}]")
end

def name(value)
@attributes[:name] = value if value.present?
end

def description(value)
@attributes[:description] = value if value.present?
end
end
end
35 changes: 35 additions & 0 deletions spec/models/service/dialog_properties_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
describe Service::DialogProperties do
it 'with a nil parameter' do
options = nil
expect(described_class.parse(options)).to eq({})
end

it 'with an empty hash' do
options = {}
expect(described_class.parse(options)).to eq({})
end

context 'name' do
it 'with an empty name' do
options = {'dialog_service_name' => ' '}
expect(described_class.parse(options)).to eq({})
end

it 'with option name' do
options = {'dialog_service_name' => 'name from dialog'}
expect(described_class.parse(options)).to eq(:name => 'name from dialog')
end
end

context 'description' do
it 'with an empty description' do
options = {'dialog_service_description' => ' '}
expect(described_class.parse(options)).to eq({})
end

it 'with option description' do
options = {'dialog_service_description' => 'test description from dialog'}
expect(described_class.parse(options)).to eq(:description => 'test description from dialog')
end
end
end

0 comments on commit e00b17b

Please sign in to comment.