commit
11d5334aa7
@ -1,20 +1,72 @@ |
||||
class Widget::Base < Widget |
||||
attr_reader :engine |
||||
attr_reader :engine, :output |
||||
|
||||
def self.dont_cache! |
||||
@dont_cache = true |
||||
end |
||||
|
||||
def self.dont_cache? |
||||
@dont_cache |
||||
end |
||||
|
||||
def initialize(query) |
||||
@query = query |
||||
@engine = query.class |
||||
end |
||||
|
||||
## |
||||
# Write a string to the canvas. The string is marked as html_safe. |
||||
# This will write twice, if @cache_output is set. |
||||
def write(str) |
||||
@output ||= "".html_safe |
||||
@output.write str.html_safe |
||||
@cache_output.write(str.html_safe) if @cache_output |
||||
end |
||||
|
||||
## |
||||
# Render this widget. Abstract method. Needs to call #write at least once |
||||
def render |
||||
raise NotImplementedError, "#render is missing in my subclass" |
||||
end |
||||
|
||||
## |
||||
# Render this widget, passing options. |
||||
# Available options: |
||||
# :to => canvas - The canvas (streaming or otherwise) to render to. Has to respond to #write |
||||
def render_with_options(options = {}, &block) |
||||
if canvas = options[:to] |
||||
canvas << "\n" << render(&block) |
||||
set_canvas(options[:to]) if options.has_key? :to |
||||
render_with_cache(options, &block) |
||||
@output |
||||
end |
||||
|
||||
def cache_key |
||||
"#{self.class.name}/#{subject.hash}" |
||||
end |
||||
|
||||
private |
||||
|
||||
def cache? |
||||
!self.class.dont_cache? |
||||
end |
||||
|
||||
## |
||||
# Render this widget or serve it from cache |
||||
def render_with_cache(options = {}, &block) |
||||
if Rails.cache.exist? cache_key and cache? |
||||
write Rails.cache.fetch(cache_key) |
||||
else |
||||
render(&block) |
||||
Rails.cache.write(cache_key, @cache_output || @output) |
||||
end |
||||
end |
||||
|
||||
## |
||||
# Set the canvas. If the canvas object isn't a string (e.g. cannot be cached easily), |
||||
# a @cache_output String is created, that will mirror what is being written to the canvas. |
||||
def set_canvas(canvas) |
||||
unless canvas.respond_to? :to_str |
||||
@cache_output = @output || "".html_safe |
||||
end |
||||
@output = canvas |
||||
end |
||||
end |
||||
|
@ -1,5 +1,7 @@ |
||||
|
||||
class Widget::Controls::Clear < Widget::Base |
||||
def render |
||||
link_to content_tag(:span, content_tag(:em, l(:"button_clear"), :class => "button-icon icon-clear")), '#', :id => 'query-link-clear', :class => 'button secondary' |
||||
write link_to(content_tag(:span, content_tag(:em, l(:"button_clear"), :class => "button-icon icon-clear")), |
||||
'#', :id => 'query-link-clear', :class => 'button secondary') |
||||
end |
||||
end |
||||
|
@ -1,11 +1,11 @@ |
||||
class Widget::Filters::RemoveButton < Widget::Filters::Base |
||||
def render |
||||
content_tag :td, :width => "25px" do |
||||
write( content_tag :td, :width => "25px" do |
||||
hidden_field = tag :input, :id => "rm_#{filter_class.underscore_name}", |
||||
:name => "fields[]", :type => "hidden", :value => "" |
||||
button = tag :input, :type => "button", :value => "", |
||||
:class => "icon filter_rem icon-filter-rem" |
||||
content_tag(:div, hidden_field + button, :id => "rm_box_#{filter_class.underscore_name}", :class => "remove-box") |
||||
end |
||||
end) |
||||
end |
||||
end |
||||
|
Loading…
Reference in new issue