kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.7 KiB
124 lines
3.7 KiB
11 years ago
|
#-- copyright
|
||
5 years ago
|
# OpenProject is an open source project management software.
|
||
|
# Copyright (C) 2012-2020 the OpenProject GmbH
|
||
11 years ago
|
#
|
||
5 years ago
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License version 3.
|
||
|
#
|
||
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
11 years ago
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
5 years ago
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
11 years ago
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
5 years ago
|
#
|
||
|
# See docs/COPYRIGHT.rdoc for more details.
|
||
11 years ago
|
#++
|
||
|
|
||
13 years ago
|
require 'digest/sha1'
|
||
|
|
||
8 years ago
|
module ::Widget
|
||
4 years ago
|
class Base < Widget::ReportingWidget
|
||
8 years ago
|
attr_reader :engine, :output
|
||
7 years ago
|
attr_accessor :request
|
||
13 years ago
|
|
||
8 years ago
|
##
|
||
|
# Deactivate caching for certain widgets. If called on Widget::Base,
|
||
|
# caching is deactivated globally
|
||
|
def self.dont_cache!
|
||
|
@dont_cache = true
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
##
|
||
|
# Query whether this widget class should be cached.
|
||
|
def self.dont_cache?
|
||
|
@dont_cache or self != Widget::Base && Widget::Base.dont_cache?
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
def initialize(query)
|
||
|
@subject = query
|
||
|
@engine = query.class
|
||
|
@options = {}
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
##
|
||
|
# 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)
|
||
|
str ||= ''
|
||
|
@output ||= ''.html_safe
|
||
|
@output = @output + '' if @output.frozen? # Rails 2 freezes tag strings
|
||
|
@output.concat str.html_safe
|
||
|
@cache_output.concat(str.html_safe) if @cache_output
|
||
|
str.html_safe
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
##
|
||
|
# Render this widget. Abstract method. Needs to call #write at least once
|
||
|
def render
|
||
5 years ago
|
raise NotImplementedError, "#render is missing in my subclass #{self.class}"
|
||
8 years ago
|
end
|
||
13 years ago
|
|
||
8 years ago
|
##
|
||
|
# 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)
|
||
|
set_canvas(options.delete(:to)) if options.has_key? :to
|
||
|
@options = options
|
||
|
render_with_cache(options, &block)
|
||
|
@output
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
def cache_key
|
||
|
@cache_key ||= Digest::SHA1::hexdigest begin
|
||
|
if subject.respond_to? :cache_key
|
||
|
"#{I18n.locale}/#{self.class.name.demodulize}/#{subject.cache_key}/#{@options.sort_by(&:to_s)}"
|
||
|
else
|
||
|
subject.inspect
|
||
|
end
|
||
13 years ago
|
end
|
||
|
end
|
||
|
|
||
8 years ago
|
def cached?
|
||
|
cache? && Rails.cache.exist?(cache_key)
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
private
|
||
13 years ago
|
|
||
8 years ago
|
def cache?
|
||
|
!self.class.dont_cache?
|
||
|
end
|
||
13 years ago
|
|
||
8 years ago
|
##
|
||
|
# Render this widget or serve it from cache
|
||
|
def render_with_cache(_options = {}, &block)
|
||
|
if cached?
|
||
|
write Rails.cache.fetch(cache_key)
|
||
|
else
|
||
|
render(&block)
|
||
|
Rails.cache.write(cache_key, @cache_output || @output) if cache?
|
||
|
end
|
||
13 years ago
|
end
|
||
|
|
||
8 years ago
|
##
|
||
|
# 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)
|
||
|
@cache_output = ''.html_safe
|
||
|
@output = canvas
|
||
|
end
|
||
13 years ago
|
end
|
||
|
end
|