Skip to content

Instantly share code, notes, and snippets.

@martinpovolny
Created February 16, 2016 13:34
Show Gist options
  • Save martinpovolny/7a07c719593da94022a1 to your computer and use it in GitHub Desktop.
Save martinpovolny/7a07c719593da94022a1 to your computer and use it in GitHub Desktop.
To convert toolbar to a class, run the 2 commands in the comment on the top of the file below. Don't read the source, it's ugly. I will have to take your eyes out if you read it!
# bundle exec rails r convert_toolbar_3.rb toobar_name.yml
# rubocop -a -c .rubocop.yml app/helpers/application_helper/toolbar/toolbar_new_class.rb
require 'active_support'
require 'active_support/inflector'
require 'active_support/inflector/methods'
#in_file = 'product/toolbars/vm_clouds_center_tb.yaml'
#
def value_of(key, value)
if %i(title text confirm).index(key)
"N_(#{value.inspect})"
else
value.inspect
end
end
def class_name(hash)
return 'twostate' if hash.key?(:buttonTwoState)
return 'select' if hash.key?(:buttonSelect)
return 'button' if hash.key?(:button)
end
def mandatory_args(hash, n)
indent = ' ' * n
if hash[:title] == hash[:text] && hash[:title].present?
indent + ':' + (hash[:buttonTwoState] || hash[:buttonSelect] || hash[:button]) + ",\n" <<
indent + (hash[:icon] ? "'#{hash[:icon]}',\n" : "nil,\n") <<
indent + "t = N_('#{hash[:title]}'),\n" <<
indent + "t"
else
indent + ':' + (hash[:buttonTwoState] || hash[:buttonSelect] || hash[:button]) + ",\n" <<
indent + (hash[:icon] ? "'#{hash[:icon]}',\n" : "nil,\n") <<
indent + (hash[:title] ? "N_('#{hash[:title]}'),\n" : "nil,\n") <<
indent + (hash[:text] ? "N_('#{hash[:text]}')" : "nil")
end
end
def convert_toolbar(in_file)
except_keys = %i(buttonTwoState buttonSelect button icon title text)
toolbar_hash = YAML.load_file(in_file)
base = File.basename(in_file)
base.sub!(/_tb.yaml$/, '')
class_name = ActiveSupport::Inflector.camelize(base)
out_file = "app/helpers/application_helper/toolbar/#{base}.rb"
out = File.open(out_file, 'w')
out << "class ApplicationHelper::Toolbar::#{class_name} < ApplicationHelper::Toolbar::Basic\n"
toolbar_hash[:button_groups].each do |bg|
next unless bg[:items]
out << " button_group('#{bg[:name]}', [\n"#{bg[:items].inspect})\n"
bg[:items].each do |button|
out << " #{class_name(button)}(\n#{mandatory_args(button, 6)}";
button.except(*except_keys).each_pair do |key, value|
v = value_of(key, value)
out << ",\n :#{key.to_s} => #{v}" unless key == :items
end
if button[:items]
out << ",\n :items => [\n"
button[:items].each do |sub_b|
if sub_b.key?(:separator)
out << " separator,\n"
else
out << " #{class_name(sub_b)}(\n#{mandatory_args(sub_b, 10)}";
sub_b.except(*except_keys).each_pair do |key,value|
v = value_of(key, value)
out << ",\n :#{key.to_s} => #{v}"
end
out << "),\n"
end
end
out << " ]\n"
end
out << " ),\n";
end
out << " ])\n"
end
out << <<EOD
end
EOD
out.close
end
#Dir['product/toolbars/**'].each do |file|
# puts "converting #{file}..."
# convert_toolbar(file)
#end
convert_toolbar(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment