Module: Spider::ControllerMixins::Visual
- Defined in:
- lib/spiderfw/controller/mixins/visual.rb
Overview
Mixin for objects using templates
Defined Under Namespace
Modules: ClassMethods, OutputFormatMethods
Instance Attribute Summary collapse
-
#dispatcher_layout ⇒ Object
Returns the value of attribute dispatcher_layout.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
Instance Method Summary collapse
-
#after_widget(id) ⇒ Object
Callback executed after child widgets are run.
- #before(action = '', *params) ⇒ Object
- #build_backtrace(exc) ⇒ Object
- #dispatched_object(route) ⇒ Object
- #execute(action = '', *params) ⇒ Object
- #find_widget(name) ⇒ Object
- #get_template(path = nil, scene = nil, options = {}) ⇒ Object
- #init_layout(layout) ⇒ Object
- #init_template(template = nil, options = {}) ⇒ Object
- #init_widgets(template, layout = nil) ⇒ Object
- #load_template(path, cur_path = nil, owner = nil, search_paths = nil) ⇒ Object
- #output_format_headers(format) ⇒ Object
- #prepare_template(template) ⇒ Object
- #render(path = nil, options = {}) ⇒ Object
- #send_error_email(exc) ⇒ Object
- #target_mode? ⇒ Boolean
- #template_exists?(name) ⇒ Boolean
- #try_rescue(exc) ⇒ Object
- #visual_params ⇒ Object
Instance Attribute Details
#dispatcher_layout ⇒ Object
Returns the value of attribute dispatcher_layout.
9 10 11 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 9 def dispatcher_layout @dispatcher_layout end |
#layout ⇒ Object
Returns the value of attribute layout.
9 10 11 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 9 def layout @layout end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
8 9 10 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 8 def template @template end |
Class Method Details
.define_format_annotations(klass) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 17 def self.define_format_annotations(klass) klass.define_annotation(:html) { |k, m, params| k.output_format(m, :html, params) } klass.define_annotation(:xml) { |k, m, params| k.output_format(m, :xml, params) } klass.define_annotation(:json) { |k, m, params| k.output_format(m, :json, params) } klass.define_annotation(:text) { |k, m, params| k.output_format(m, :text, params) } end |
.included(klass) ⇒ Object
11 12 13 14 15 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 11 def self.included(klass) klass.extend(ClassMethods) klass.extend(OutputFormatMethods) define_format_annotations(klass) end |
Instance Method Details
#after_widget(id) ⇒ Object
Callback executed after child widgets are run. Useful to postprocess widgets data. id is the child widget id, as a symbol.
224 225 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 224 def (id) end |
#before(action = '', *params) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 24 def before(action='', *params) @layout ||= self.class.get_layout(action) @layout ||= @dispatcher_layout format = nil req_format = self.is_a?(Widget) && @is_target && @request.params['_wf'] ? @request.params['_wf'].to_sym : @request.format if (req_format && self.class.output_formats[@executed_method]) format = req_format if self.class.output_format?(@executed_method, req_format) if (format) format_params = self.class.output_format_params(@executed_method, format) end if @executed_method && !format || (format_params && format_params[:widgets] && !target_mode?) #raise Spider::Controller::NotFound.new("#{action}.#{@request.format}") end end format ||= self.class.output_format(@executed_method) format_params ||= self.class.output_format_params(@executed_method, format) output_format_headers(format) @executed_format = format @executed_format_params = format_params super end |
#build_backtrace(exc) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 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 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 318 def build_backtrace(exc) bt = [] if Object.const_defined?(:Debugger) && Debugger.started? && Debugger.post_mortem? use_debugger = true context = exc.__debug_context ct_first_file = context.frame_file(0) ct_first_line = context.frame_line(0) e_file, e_line, e_method = exc.backtrace[0].split(':') if (e_file != ct_first_file || e_line.to_i != ct_first_line) use_debugger = false end end if (!use_debugger) exc.backtrace.each do |trace_line| str = trace_line file_path, line, method = trace_line.split(':') bt << {:text => str, :path => file_path, :line => line, :method => method} end return bt end context = exc.__debug_context 0.upto(Debugger.current_context.stack_size - 2) do |i| begin file = context.frame_file(i) line = context.frame_line(i) klass = context.frame_class(i) method = context.frame_method(i) args = context.frame_args(i) locals = context.frame_locals(i) frame_self = context.frame_self(i) dest = context.frame_self(i-1) unless i == 0 ex_method = context.frame_method(i-1) unless i == 0 in_method = context.frame_method(i) #s ex_args = context.frame_args(i+1) str = "#{file}:#{line}: in #{in_method}" #str = exc.backtrace[i] self_str = frame_self # self_str = "#<#{frame_self.class}:#{frame_self.object_id}>" if (dest) dest_str = dest.is_a?(Class) ? dest.inspect : "#<#{dest.class}:#{dest.object_id}>" else dest_str = "" end self_str = frame_self.is_a?(Class) ? frame_self.inspect : "#<#{frame_self.class}:#{frame_self.object_id}>" if (i == -1) info = "" else # if (frame_self == dest) # info = "#{dest_str}" # else # info = "#{self_str}: #{dest_str}" # end info = "#{self_str}: #{dest_str}" info += ".#{ex_method}(" info += args.map{ |arg| val = locals[arg] arg_str = "#{arg}##{val.class}" val_str = nil if (val.is_a?(String)) if (val.length > 20) val_str = (val[0..20]+'...').inspect else val_str = val.inspect end elsif (val.is_a?(Symbol) || val.is_a?(Fixnum) || val.is_a?(Float) || val.is_a?(BigDecimal) || val.is_a?(Date) || val.is_a?(Time)) val_str = val.inspect end arg_str += "=#{val_str}" if val_str arg_str }.join(', ') info += ")" end if (Spider.conf.get('devel.trace.show_instance_variables')) iv = {} frame_self.instance_variables.each{ |var| iv[var] = frame_self.instance_variable_get(var) } iv.reject{ |k, v| v.nil? } end locals = nil unless Spider.conf.get('devel.trace.show_locals') bt << { :text => str, :info => info, :path => File.(file), :line => line, :method => method, :klass => klass, :locals => locals, :instance_variables => iv } rescue => exc2 end end return bt end |
#dispatched_object(route) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 229 def dispatched_object(route) obj = super if (obj.is_a?(Visual)) set_layout = @layout || @dispatcher_layout if set_layout set_layout = [set_layout] unless set_layout.is_a?(Array) set_layout.map{ |l| self.class.load_layout(l) } obj.dispatcher_layout = set_layout end end return obj end |
#execute(action = '', *params) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 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 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 81 def execute(action='', *params) @visual_params = @executed_format_params @is_target = false if target_mode? && !self.is_a?(Spider::Widget) if (self.is_a?(Widget) && @is_target && @request.params['_wp']) params = @request.params['_wp'] elsif (@visual_params.is_a?(Hash) && @visual_params[:params]) p_first, p_rest = action.split('/') params = format_params[:params].call(p_rest) if p_rest end super(action, *params) return unless @visual_params.is_a?(Hash) @template = get_template if !@template && @visual_params[:template] (@template) if @template if @visual_params[:call] meth = self.method(@visual_params[:call]) args = params + @executed_method_arguments arity = meth.arity arity = (-arity + 1) if arity < 0 args = arity == 0 ? [] : args[0..(arity-1)] args = [nil] if meth.arity == 1 && args.empty? send(@visual_params[:call], *args) end if @visual_params[:template] && !@_widget && !done? if (@template) render(@template, @visual_params) # has been init'ed in before method else render(@visual_params[:template], @visual_params) end end if @visual_params[:redirect] redirect(@visual_params[:redirect]) end if @executed_format == :json && (@visual_params[:scene] || @visual_params[:return]) # FIXME: move in JSON mixin? if (@visual_params[:return]) $out << @visual_params[:return].to_json elsif (@visual_params[:scene].is_a?(Array)) h = @scene.to_hash res = {} @visual_params[:scene].each{ |k| res[k] = h[k] } $out << res.to_json else $out << @scene.to_json end end end |
#find_widget(name) ⇒ Object
218 219 220 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 218 def (name) @template.(name) end |
#get_template(path = nil, scene = nil, options = {}) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 145 def get_template(path=nil, scene=nil, ={}) return @template if @template && @loaded_template_path == path scene ||= @scene scene ||= get_scene if (!path) format_params = @visual_params || self.class.output_format_params(@executed_method, @executed_format) return unless format_params && format_params[:template] path = format_params[:template] = format_params.merge() end template = load_template(path) init_template(template, ) @template = template @loaded_template_path = path return template end |
#init_layout(layout) ⇒ Object
176 177 178 179 180 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 176 def init_layout(layout) l = layout.is_a?(Layout) ? layout : self.class.load_layout(layout) prepare_template(l) return l end |
#init_template(template = nil, options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 163 def init_template(template=nil, ={}) return get_template(template, nil, ) unless template.is_a?(Spider::Template) prepare_template(template) unless template.owner # if called directly template.init(scene) if (@request.params['_action']) template. = @request.params['_action'] else template._action_to = [:action_to] template._action = @controller_action end return template end |
#init_widgets(template, layout = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 59 def (template, layout=nil) = @request.params['_wt'] = @request.params['_we'] if ( && !@rendering_error) first, rest = .split('/', 2) @_widget = template.(first) @_widget = layout.(first) if !@_widget && layout raise Spider::Controller::NotFound.new("Widget #{}") unless @_widget @_widget.is_target = true unless rest @_widget.set_action() if @_widget.is_target_ancestor = true @_widget. = rest end template. layout. if layout if @_widget @_widget.before() if @_widget.is_target @_widget.execute done end end |
#load_template(path, cur_path = nil, owner = nil, search_paths = nil) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 127 def load_template(path, cur_path=nil, owner=nil, search_paths=nil) template = self.class.load_template(path, cur_path, owner, search_paths) prepare_template(template) @template = template return template end |
#output_format_headers(format) ⇒ Object
46 47 48 49 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 46 def output_format_headers(format) return content_type('application/json') if format == :json && Spider.runmode == 'devel' && @request.params['_text'] content_type(format) end |
#prepare_template(template) ⇒ Object
134 135 136 137 138 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 134 def prepare_template(template) template.owner = self template.request = request template.response = response end |
#render(path = nil, options = {}) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 182 def render(path=nil, ={}) scene = [:scene] || @scene scene ||= get_scene scene = prepare_scene(scene) request = [:request] || @request response = [:response] || @response if (path.is_a?(Spider::Template)) template = path else template = get_template(path, scene, ) end layout = nil unless .key?(:layout) && ![:layout] chosen_layouts = [:layout] || @layout chosen_layouts = [chosen_layouts] if chosen_layouts && !chosen_layouts.is_a?(Array) if (chosen_layouts) t = template layout = nil (chosen_layouts.length-1).downto(0) do |i| layout = init_layout(chosen_layouts[i]) layout.template = t t = layout end end layout.init(scene) if layout end (template, layout) return template if done? if layout layout.render(scene) else template.render(scene) end return template end |
#send_error_email(exc) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 301 def send_error_email(exc) scene = Spider::Scene.new scene.request = @request.env scene.exception = exc subject = _("Site error") if Spider.conf.get('orgs.default.name') subject = "#{Spider.conf.get('orgs.default.name')} - #{subject}" elsif @request.env.key?('HTTP_HOST') subject = "#{@request.env['HTTP_HOST']} - #{subject}" end headers = {'Subject' => subject} from = Spider.conf.get('orgs.default.auto_from_email') || Spider.conf.get('site.tech_admin.email') Spider::Messenger::MessengerHelper.send_email( self.class, 'error', scene, from, Spider.conf.get('site.tech_admin.email'), headers ) end |
#target_mode? ⇒ Boolean
55 56 57 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 55 def target_mode? !@request.params['_wt'].nil? && !@request.params['_wt'].empty? end |
#template_exists?(name) ⇒ Boolean
140 141 142 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 140 def template_exists?(name) self.class.template_exists?(name) end |
#try_rescue(exc) ⇒ Object
242 243 244 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 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 242 def try_rescue(exc) exc.uuid = UUIDTools::UUID.random_create.to_s if exc.respond_to?(:uuid=) format = self.class.output_format(:error) || :html return super unless @executed_format == :html return super unless action_target? return super if target_mode? output_format_headers(format) if (exc.is_a?(Spider::Controller::NotFound)) error_page = '404' @scene.error_msg = _("Page not found") @scene.email_subject = @scene.error_msg else error_page = 'error_generic' if (exc.is_a?(HTTPMixin::HTTPStatus)) @scene.error_msg = exc. end @scene.error_msg = _("An error occurred") @scene.email_subject = @scene.error_msg end if (exc.respond_to?(:uuid)) exc.extend(UUIDExceptionMessage) @scene.exception_uuid = exc.uuid @scene.email_subject += " (#{exc.uuid})" end @scene.admin_email = Spider.conf.get('site.tech_admin.email') if (Spider.runmode == 'devel') begin @scene.devel = true @scene.backtrace = build_backtrace(exc) client_editor = Spider.conf.get('client.text_editor') prefix = 'txmt://open?url=' if client_editor == 'textmate' @scene.exception = "#{exc.class.name}: #{CGI.escapeHTML(exc.)}" cnt = 0 @scene.backtrace.each do |tr| tr[:index] = cnt cnt += 1 suffix = '' suffix = "&line=#{tr[:line]}" if (client_editor == 'textmate') tr[:link] = "#{prefix}file://#{tr[:path]}#{suffix}" end @scene.request_params = @request.params.inspect @scene.session = @request.session.inspect rescue => exc2 super(exc) end end @rendering_error = true @scene.__is_error_page = true if Spider.const_defined?(:Messenger) && Spider.conf.get('errors.send_email') && Spider.conf.get('site.tech_admin.email') begin send_error_email(exc) rescue => exc2 end end render "errors/#{error_page}", :layout => "errors/error" super end |
#visual_params ⇒ Object
51 52 53 |
# File 'lib/spiderfw/controller/mixins/visual.rb', line 51 def visual_params @visual_params ||= {} end |