class NewDrgFormGenerator < Rails::Generators::NamedBase source_root File.expand_path('../templates', __FILE__) #desc "This generator creates form for model at app/forms" #argument :form_name, :type => :string, :default => '' class_option :tabs, :type => :boolean, :default => false, :description => "Create form with tabulators" ########################################################################### # Will create output and save it to form filename. ########################################################################### def create_form_file #:TODO: find out how to prevent error when model class is not defined @file_name = file_name form_name = file_name #if formname.size == 0 @model = file_name.classify.constantize rescue nil return (p "Model #{file_name.classify} not found! Aborting.") if @model.nil? # yml = top_level_options + index_options + result_set_options + form_top_options + form_fields_options + localize_options create_file "app/forms/#{form_name}.yml", yml end private ########################################################################### # ########################################################################### def top_level_options < 0 yml << ' '*offset + "size: 50\n" if type == 'text_field' if type == 'select' yml << ' '*offset + "html:\n" offset += 2 yml << ' '*offset + "include_blank: true\n" end yml end ########################################################################### # ########################################################################### def embedded_form_field(offset) yml = '' field_index = 10 @model.embedded_relations.keys.each do |embedded_name| yml << ' '*offset + "#{field_index}:\n" yml << ' '*(offset+2) + "name: #{embedded_name}\n" yml << ' '*(offset+2) + "type: embedded\n" yml << ' '*(offset+2) + "form_name: #{embedded_name[0,embedded_name.size - 1]}\n" yml << '#' + ' '*(offset+2) + "html:\n" yml << '#' + ' '*(offset+4) + "height: 500\n" field_index += 10 end yml end ########################################################################### # ########################################################################### def form_fields_options forbidden = ['_id','created_by','updated_by','created_at','updated_at'] tab_index = 1 field_index = 0 if options.tabs? yml = " tabs:\n" @model.attribute_names.each do |attr_name| next if forbidden.include?(attr_name) if field_index%100 == 0 yml << " tab#{tab_index}:\n" field_index = 0 tab_index += 1 end field_index += 10 yml << form_field(attr_name, field_index, 6) end yml << " tab#{tab_index}:\n" yml << embedded_form_field(6) else yml = " fields:\n" @model.attribute_names.each do |attr_name| next if forbidden.include?(attr_name) field_index += 10 yml << form_field(attr_name, field_index, 4) end yml << embedded_form_field(4) end yml end ########################################################################### # ########################################################################### def localize_options forbidden = ['_id','created_by','updated_by','created_at','updated_at'] yml =<