if(@instance) menu_item = MenuItem.find(@instance.menu_item_id) # Format for option json opt_format = [] # Format for attributes json menu_item.item_options.each do|opt| menu_opt = MenuItemOption.find(opt) if opt_format.count == 0 opt_format.push({ type: menu_opt.option_type, values: [menu_opt.name] }) next end opt_format.each do |of| if menu_opt.option_type.in? opt_format.map {|k| k[:type]} if menu_opt.option_type == of[:type] of[:values].push(menu_opt.name) end else new_opt = {type: menu_opt.option_type, values: [ menu_opt.name ] } opt_format.push(new_opt) break end end end json.success true json.id @instance.id json.name @instance.item_instance_name json.code @instance.item_instance_code json.item_id @instance.menu_item_id json.attributes @instance.item_attributes json.price @instance.price json.is_default @instance.is_default json.options opt_format else json.success false end