Skip to content

Commit

Permalink
Fixes #24980 - enable structured formatters for refernces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Strachota authored and rabajaj0509 committed Sep 25, 2018
1 parent 5096f1e commit adcc94f
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 55 deletions.
55 changes: 45 additions & 10 deletions lib/hammer_cli_foreman/output/fields.rb
Expand Up @@ -2,25 +2,60 @@

module Fields

class SingleReference < Field
class Reference < Field
def initialize(options={})
super
initialize_options
end

def display?(value)
id_key = "#{parameters[:key]}_id"
display_field = parameters[:display_field] || 'name'
display_key = "#{parameters[:key]}_#{display_field}"
def initialize_options
@options[:details] ||= []
@options[:details] = [@options[:details]] unless @options[:details].is_a?(Array)
@options[:details] = [id_detail] + @options[:details]
@options[:display_field_key] ||= @options[:name_key] || :name
end

(value[display_key.to_sym] || value[display_key]) && (value[id_key.to_sym] || value[id_key])
protected

def id_detail
{
:label => _('id'),
:structured_label => _('Id'),
:key => @options[:id_key] || :id,
:id => true
}
end
end

class Reference < Field
class SingleReference < Reference
def initialize_options
key = @options[:key]
display_field = @options[:display_field] || 'name'

@options[:id_key] ||= "#{key}_id"
@options[:display_field_key] ||= "#{key}_#{display_field}"
super
end

def display?(value)
id_key = @options[:id_key]
display_key = @options[:display_field_key]

(value[display_key.to_sym] || value[display_key]) && (value[id_key.to_sym] || value[id_key])
end
end

class Template < Reference
def initialize_options
super
@options[:details] << template_kind_detail
end

def initialize(options={})
options[:details] ||= [:template_kind_name]
super(options)
def template_kind_detail
{
:structured_label => _('Kind'),
:key => :template_kind_name,
}
end
end

Expand Down
127 changes: 82 additions & 45 deletions lib/hammer_cli_foreman/output/formatters.rb
@@ -1,69 +1,106 @@
module HammerCLIForeman::Output
module Formatters

class SingleReferenceFormatter < HammerCLI::Output::Formatters::FieldFormatter

class ReferenceFormatter < HammerCLI::Output::Formatters::FieldFormatter
def tags
[:flat]
end

def format(resource, field_params={})
return "" if resource.nil?

key = field_params[:key]
display_field = field_params[:display_field] || 'name'

id_key = "#{key}_id"
display_key = "#{key}_#{display_field}"

name = resource[display_key.to_sym] || resource[display_key]
id = resource[id_key.to_sym] || resource[id_key]

# Parameters:
# :display_field_key - key where the formmatter will look for the main field to display, default is :name
# :details - detail fields to be displayed
# example format:
# :details => [
# { :label => _('Type'), :key => :provider_friendly_name, :structured_label => _('Type') },
# { :label => _('Id'), :key => :id }
# ]
def format(data, field_params={})
return "" if data.nil?

name = get_value(data, field_params[:display_field_key] || :name)
context = field_params[:context] || {}

out = "#{name}"
out += " (id: #{id})" if context[:show_ids] && id
out
details = format_details(data, field_params[:details] || [], context[:show_ids])
if details.empty?
"#{name}" if name
else
"#{name} (#{details.join(', ')})" if name
end
end

end
protected
def format_details(data, details, show_ids)
details = [details] unless details.is_a?(Array)

details.map do |detail|
if detail.is_a?(Hash)
next if detail[:id] && !show_ids
if detail[:label]
"#{detail[:label]}: #{get_value(data, detail[:key])}"
else
get_value(data, detail[:key])
end
else
get_value(data, detail)
end
end.compact
end

class ReferenceFormatter < HammerCLI::Output::Formatters::FieldFormatter
def get_value(data, key)
data[key] || data[key.to_s]
end
end

class StructuredReferenceFormatter < HammerCLI::Output::Formatters::FieldFormatter
def tags
[:flat]
[:data]
end

def format(reference, field_params={})
return "" if reference.nil? || reference == ""

id_key = field_params[:id_key] || :id
name_key = field_params[:name_key] || :name

name = reference[name_key] || reference[name_key.to_s]
id = reference[id_key] || reference[id_key.to_s]

context = field_params[:context] || {}

details = field_params[:details] || []
details = [details] unless details.is_a? Array
values = details.collect do |key|
reference[key] || reference[key.to_s]
end
values << "id: #{id}" if context[:show_ids]

if values.empty?
"#{name}" if name
else
"#{name} (#{values.join(', ')})" if name && !values.empty?
# Parameters:
# :display_field_key - key where the formmatter will look for the main field to display, default is :name
# :details - detail fields to be displayed
# example format:
# :details => [
# { :label => _('Type'), :key => :provider_friendly_name, :structured_label => _('Type') },
# { :label => _('Id'), :key => :id }
# ]
def format(data, field_params={})
return {} if data.nil? || data == ""

display_field_key = field_params[:display_field_key] || :name

# TODO: hardcoded name
formatted = {
_('Name') => get_value(data, display_field_key)
}

details = field_params[:details]
details = [details] unless details.is_a?(Array)

details.map do |detail|
if detail.is_a?(Hash)
label = detail[:structured_label]
label = detail[:label].capitalize if !label && detail[:label]
if label
formatted[label] = get_value(data, detail[:key])
end
end
end
formatted
end

protected
def get_value(data, key)
data[key] || data[key.to_s]
end
end

HammerCLI::Output::Output.register_formatter(SingleReferenceFormatter.new, :SingleReference)
HammerCLI::Output::Output.register_formatter(ReferenceFormatter.new, :SingleReference)
HammerCLI::Output::Output.register_formatter(StructuredReferenceFormatter.new, :SingleReference)

HammerCLI::Output::Output.register_formatter(ReferenceFormatter.new, :Reference)
HammerCLI::Output::Output.register_formatter(StructuredReferenceFormatter.new, :Reference)
HammerCLI::Output::Output.register_formatter(ReferenceFormatter.new, :Template)

HammerCLI::Output::Output.register_formatter(StructuredReferenceFormatter.new, :Template)
end
end

0 comments on commit adcc94f

Please sign in to comment.