Module: RbbtHTMLHelpers
- Included in:
- Sinatra::SinatraRbbt
- Defined in:
- lib/rbbt/workflow/rest/render.rb,
lib/rbbt/workflow/rest/helpers.rb
Constant Summary collapse
- PAGESIZE =
25
Class Method Summary collapse
Instance Method Summary collapse
- #entity_action_render(type, action, locals, &block) ⇒ Object
- #entity_list_action_render(type, action, locals, &block) ⇒ Object
- #entity_list_render(type, locals, &block) ⇒ Object
- #entity_render(type, locals, &block) ⇒ Object
- #input(type, name, options = {}) ⇒ Object
- #locate_file(workflow, task, file) ⇒ Object
- #paginate(object, page = nil, just_keys = false) ⇒ Object
- #parse_page(page) ⇒ Object
- #process_result(result, options) ⇒ Object
- #workflow_partial(file, *args) ⇒ Object
-
#workflow_render(file, *args, &block) ⇒ Object
def workflow_render(file, workflow = nil, task = nil, locals = {}, &block).
- #workflow_render_result(result, *args) ⇒ Object
Class Method Details
.locate_file(workflow, task, file) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rbbt/workflow/rest/render.rb', line 6 def self.locate_file(workflow, task, file) file = case when file =~ /\.[a-z]{2,4}$/ file when file =~ /(.*)\/$/ $1 else file + '.haml' end webapp_dir_views = Rbbt.www.views.find(:lib, Sinatra::Application.root) workflow_dir_views = (workflow.nil? or workflow.libdir.nil?) ? nil : workflow.libdir.www.views global_views = Rbbt.share.views # workflow = workflow.to_s unless workflow.nil? or String === workflow path = case when webapp_dir_views[workflow][task][file].exists? webapp_dir_views[workflow][task][file] when webapp_dir_views[task][file].exists? webapp_dir_views[task][file] when webapp_dir_views[workflow][file].exists? webapp_dir_views[workflow][file] when webapp_dir_views[file].exists? webapp_dir_views[file] when (not workflow_dir_views.nil? and workflow_dir_views[workflow][task][file].exists?) workflow_dir_views[workflow][task][file] when (not workflow_dir_views.nil? and workflow_dir_views[task][file].exists?) workflow_dir_views[task][file] when (not workflow_dir_views.nil? and workflow_dir_views[workflow][file].exists?) workflow_dir_views[workflow][file] when (not workflow_dir_views.nil? and workflow_dir_views[file].exists?) workflow_dir_views[file] when global_views[workflow][task][file].exists? global_views[workflow][task][file] when global_views[task][file].exists? global_views[task][file] when global_views[workflow][file].exists? global_views[workflow][file] when global_views[file].exists? global_views[file] else raise "File not found: #{[workflow, task, file].compact * ", "}" end Log.debug "File #{[workflow, task, file].collect{|v| "'#{ v }'"} * ", "} located at #{ path }" path end |
Instance Method Details
#entity_action_render(type, action, locals, &block) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/rbbt/workflow/rest/render.rb', line 329 def entity_action_render(type, action, locals, &block) type, format = type.split ":" size = locals.delete(:_size) || locals.delete("_size") || :normal workflow = WorkflowREST.workflows.last entity = locals[:entity] entity_class = case when Entity.formats.include?(format) Entity.formats[format] when Entity.formats.include?(type) Entity.formats[type] else nil end path = case when (entity.respond_to? :action_template and Hash === entity.action_template and not entity.action_template[action].nil?) entity.action_template[action] when (entity_class.respond_to? :action_template and Hash === entity_class.action_template and not entity_class.action_template[action].nil?) entity_class.action_template[action] else path = nil [format, type].compact.each do |file| break unless path.nil? file = File.join(file, action) begin case size.to_sym when :normal file = File.join('entity', file + '.haml') path = locate_file(workflow, nil, file) when :small begin file_small = File.join('entity', file + '.small.haml') path = locate_file(workflow, nil, file_small) rescue Log.debug $!. file = File.join('entity', file + '.haml') path = locate_file(workflow, nil, file) end end rescue next end end raise "Action Template not found for type, format, action: #{[type, format, action] * ", "}" if path.nil? path end if entity_class entity_class.setup(entity, *locals.values_at(*entity_class.annotations).collect{|v| case v when "false" false when "true" true else v end }) entity.format = format if entity.respond_to? :format end layout = locals[:_layout] layout = false if locals[:_xhr] and layout.nil? layout = true if layout.nil? renderer = Haml::Engine.new(path.read) case when (not layout) renderer.render(self, locals, &block) when (not block_given?) workflow_render("layout", nil, nil, locals) do renderer.render(self, locals) end else renderer.render(self, locals, &block) end end |
#entity_list_action_render(type, action, locals, &block) ⇒ Object
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/rbbt/workflow/rest/render.rb', line 498 def entity_list_action_render(type, action, locals, &block) type, format = type.split ":" size = locals.delete(:_size) || locals.delete("_size") || :normal workflow = WorkflowREST.workflows.last list = locals[:list] = Entity::REST.load_list(locals[:id]) entity_class = case when Entity.formats.include?(format) Entity.formats[format] when Entity.formats.include?(type) Entity.formats[type] else nil end path = case when (list.respond_to? :list_action_template and Hash === list.list_action_template and not list.list_action_template[action].nil?) list.list_action_template[action] when (entity_class.respond_to? :list_action_template and Hash === entity_class.list_action_template and not entity_class.list_action_template[action].nil?) entity_class.list_action_template[action] else path = nil [format, type].compact.each do |file| break unless path.nil? file = File.join(file, action) begin case size.to_sym when :normal file = File.join('entity_list', file + '.haml') path = locate_file(workflow, nil, file) when :small begin file_small = File.join('entity_list', file + '.small.haml') path = locate_file(workflow, nil, file_small) rescue Log.debug $!. file = File.join('entity_list', file + '.haml') path = locate_file(workflow, nil, file) end end rescue next end end raise "List Template not found for type, format: #{[type, format] * ", "}" if path.nil? path end if entity_class entity_class.setup(list, *locals.values_at(*entity_class.annotations).collect{|v| case v when "false" false when "true" true else v end }) list.format = format if list.respond_to? :format end layout = locals[:_layout] layout = false if locals[:_xhr] and layout.nil? layout = true if layout.nil? renderer = Haml::Engine.new(path.read) case when (not layout) renderer.render(self, locals, &block) when (not block_given?) workflow_render("layout", nil, nil, locals) do renderer.render(self, locals) end else renderer.render(self, locals, &block) end end |
#entity_list_render(type, locals, &block) ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/rbbt/workflow/rest/render.rb', line 413 def entity_list_render(type, locals, &block) type, format = type.split ":" size = locals.delete(:_size) || locals.delete("_size") || :normal workflow = WorkflowREST.workflows.last list = locals[:list] = Entity::REST.load_list(locals[:id]) entity_class = case when Entity.formats.include?(format) Entity.formats[format] when Entity.formats.include?(type) Entity.formats[type] else nil end path = case when (list.respond_to? :list_template and not list.list_template.nil?) list.list_template when (entity_class.respond_to? :list_template and not entity_class.list_template.nil?) entity_class.list_template else path = nil [format, type].compact.each do |file| break unless path.nil? begin case size.to_sym when :normal file = File.join('entity_list', file + '.haml') path = locate_file(workflow, nil, file) when :small begin file_small = File.join('entity_list', file + '.small.haml') path = locate_file(workflow, nil, file_small) rescue Log.debug $!. file = File.join('entity_list', file + '.haml') path = locate_file(workflow, nil, file) end end rescue next end end raise "List Template not found for type, format: #{[type, format] * ", "}" if path.nil? path end if entity_class entity_class.setup(list, *locals.values_at(*entity_class.annotations).collect{|v| case v when "false" false when "true" true else v end }) list.format = format if list.respond_to? :format end layout = locals[:_layout] layout = false if locals[:_xhr] and layout.nil? layout = true if layout.nil? renderer = Haml::Engine.new(path.read) case when (not layout) renderer.render(self, locals, &block) when (not block_given?) workflow_render("layout", nil, nil, locals) do renderer.render(self, locals) end else renderer.render(self, locals, &block) end end |
#entity_render(type, locals, &block) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/rbbt/workflow/rest/render.rb', line 245 def entity_render(type, locals, &block) type, format = type.split ":" size = locals.delete(:_size) || locals.delete("_size") || :normal workflow = WorkflowREST.workflows.last entity = locals[:entity] entity_class = case when Entity.formats.include?(format) Entity.formats[format] when Entity.formats.include?(type) Entity.formats[type] else nil end path = case when (entity.respond_to? :template and not entity.template.nil?) entity.template when (entity_class.respond_to? :template and not entity_class.template.nil?) entity_class.template else path = nil [format, type].compact.each do |file| break unless path.nil? begin case size.to_sym when :normal file = File.join('entity', file + '.haml') path = locate_file(workflow, nil, file) when :small begin file_small = File.join('entity', file + '.small.haml') path = locate_file(workflow, nil, file_small) rescue Log.debug $!. file = File.join('entity', file + '.haml') path = locate_file(workflow, nil, file) end end rescue next end end raise "Template not found for type, format: #{[type, format] * ", "}" if path.nil? path end if entity_class entity_class.setup(entity, *locals.values_at(*entity_class.annotations).collect{|v| case v when "false" false when "true" true else v end }) entity.format = format if entity.respond_to? :format end layout = locals[:_layout] layout = false if locals[:_xhr] and layout.nil? layout = true if layout.nil? renderer = Haml::Engine.new(path.read) case when (not layout) renderer.render(self, locals, &block) when (not block_given?) workflow_render("layout", nil, nil, locals) do renderer.render(self, locals) end else renderer.render(self, locals, &block) end end |
#input(type, name, options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rbbt/workflow/rest/helpers.rb', line 3 def input(type, name, = {}) id, hide, value, klass, default, description = Misc. , :id, :hide, :value, :class, :default, :description str = "<div class='form_input' id='#{ id }'>" if hide str << "<input #{"id='#{ id }'" if id} type='hidden' name='#{ name }' #{"class='#{ klass }'" if klass} #{"value='#{ value }'" if value}/>" << '</div>' << "\n" return str end case type when :boolean not_value = ! value str << "<label #{"for='#{ id }" if id}'><span class='input_name'>#{ name }</span>#{": <span class='input_description'>#{ description }</span>" if description}</label>" << "\n" str << "<input type='radio' id='input_#{id}_true' name='#{ name }' value='true' #{"checked='true'" if value}>" << "\n" str << "<label type='radio_option radio_true' for='input_#{id}_true' name='#{ name }'>true</label>" << "\n" str << "<input type='radio' id='input_#{id}_false' name='#{ name }' value='false' #{"checked='false'" if not value}>" << "\n" str << "<label type='radio_option radio_false' for='input_#{id}_false' name='#{ name }'>false</label>" << "\n" str << "</div>" str when :string, :float, :integer str << "<label #{"for='#{ id }" if id}'><span class='input_name'>#{ name }</span>#{": <span class='input_description'>#{ description }</span>" if description}#{": <span class='input_default'>(Default: #{ default })</span>" if default}</label>" << "\n" str << "<input #{"class='#{klass ? klass : "#{ name } #{ type }" }'"} #{"id='#{ id }'" if id} name='#{ name }' #{"value='#{ value }'" if value}/>" << "\n" str << "</div>" str when :tsv, :array, :text str << "<label #{"for='#{ id }" if id}'><span class='input_name'>#{ name }</span>#{": <span class='input_description'>#{ description }</span>" if description}#{": <span class='input_default'>(Default: #{ default })</span>" if default}</label>" << "\n" str << "<p>" str << "<input type='file' #{"class='#{klass ? klass : "#{ name } #{ type }" }'"} #{"id='input_#{ id }_tsv_file'" if id} name='#{ name }_param_file' #{"value='#{ value }'" if value}/>" << "\n" str << "<span class='tsv_textarea_note note'>(or use text area bellow)</span>" str << "</p>" str << "<textarea #{"class='#{klass ? klass : "#{ name } #{ type }" }'"} name='#{ name }' #{"id='input_#{ id }'" if id}></textarea>" str << "</div>" str end end |
#locate_file(workflow, task, file) ⇒ Object
57 58 59 |
# File 'lib/rbbt/workflow/rest/render.rb', line 57 def locate_file(workflow, task, file) RbbtHTMLHelpers.locate_file(workflow, task, file) end |
#paginate(object, page = nil, just_keys = false) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rbbt/workflow/rest/render.rb', line 72 def paginate(object, page = nil, just_keys = false) return object unless TSV === object and not page.nil? return object if page == "all" or page.nil? field, num, size = parse_page(page) entity = Entity.formats[field] num = num.to_i size = size.to_i max = object.size / size + 1 num = max if num > max num = - max if num < - max if entity and entity.respond_to? :tsv_sort object.page(num, size, field, just_keys, &entity.method(:tsv_sort)) else object.page(num, size, field, just_keys) end end |
#parse_page(page) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/rbbt/workflow/rest/render.rb', line 62 def parse_page(page) field, num, size = page.split("~").values_at 0, 1, 2 if num.nil? num = field field = "key" end size ||= PAGESIZE [field, num, size] end |
#process_result(result, options) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rbbt/workflow/rest/render.rb', line 94 def process_result(result, ) rows = result if .include? :_filter and not [:_filter].nil? field, query = [:_filter].split("~") entity = Entity.formats[field] if entity and entity.respond_to?(:filter) filter_proc = entity.method :filter if filter_proc.respond_to? :curry curried_proc = filter_proc.curry(query, field, .merge(:tsv => result, :field => field)) else curried_proc = Proc.new{|entity| filter_proc.call(query, field, .merge(:tsv => result, :field => field), entity) } end rows = rows.select field => curried_proc else rows = rows.select field => Regexp.new(query) end end total = rows.size [:_page] = "1" if rows.size > 100 and [:_page].nil? if .include?(:_page) and not [:_page].nil? keys = paginate(rows, [:_page], true) if keys rows = keys.zip(rows.values_at *keys) end end [rows, total] end |
#workflow_partial(file, *args) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rbbt/workflow/rest/render.rb', line 182 def workflow_partial(file, *args) locals = Hash === args.last ? args.pop : {} case args.length when 0 workflow = locals[:workflow] task = locals[:task] when 1 workflow = args.shift task = locals[:task] when 2 workflow = args.shift task = args.shift end workflow_render(file, workflow, task, locals.merge(:_layout => false)) end |
#workflow_render(file, *args, &block) ⇒ Object
def workflow_render(file, workflow = nil, task = nil, locals = {}, &block)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rbbt/workflow/rest/render.rb', line 133 def workflow_render(file, *args, &block) locals = Hash === args.last ? args.pop : {} case args.length when 0 workflow = locals[:workflow] task = locals[:task] when 1 workflow = args.shift task = locals[:task] when 2 workflow = args.shift task = args.shift end size = locals.delete(:_size) || locals.delete("_size") || :normal case size.to_sym when :normal path = locate_file(workflow, task, file) when :small begin path = locate_file(workflow, task, file + '.small') rescue Log.debug $!. path = locate_file(workflow, task, file) end end layout = locals[:_layout] layout = false if locals[:_xhr] and layout.nil? layout = true if layout.nil? locals = Misc.add_defaults locals, :workflow => workflow, :task => task renderer = Haml::Engine.new(path.read) case when (not layout) renderer.render(self, locals, &block) when (not block_given?) workflow_render("layout", workflow, task, locals) do renderer.render(self, locals) end else renderer.render(self, locals, &block) end end |
#workflow_render_result(result, *args) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rbbt/workflow/rest/render.rb', line 200 def workflow_render_result(result, *args) = Hash === args.last ? args.pop : {} case args.length when 0 workflow = [:workflow] task = [:task] when 1 workflow = args.shift task = [:task] when 2 workflow = args.shift task = args.shift end format = [:_format] page = [:_page] case when format.to_sym == :raw content_type "text/plain" if Array === result result * "\n" else result.to_s end when format.to_sym == :json content_type "application/json" if TSV === result result = paginate(result, page) result.to_json else result.to_json end when format.to_sym == :html content_type "text/html" = Misc.add_defaults , workflow.task_info(task) = Misc.add_defaults , :result => result, :workflow => workflow, :task => task workflow_render("result", workflow, task, ) else raise "Unkonwn format: #{format}" end end |