From 5a5c54cd06432291962e9791cf00229e83fddcb0 Mon Sep 17 00:00:00 2001 From: Alex Shulgin Date: Sun, 1 Nov 2015 17:59:25 +0100 Subject: [PATCH] Redmine-3.0 compatibility --- app/controllers/pastes_controller.rb | 14 ++++----- app/models/paste.rb | 46 ++++++---------------------- init.rb | 18 +++-------- 3 files changed, 19 insertions(+), 59 deletions(-) diff --git a/app/controllers/pastes_controller.rb b/app/controllers/pastes_controller.rb index adbc009..2b26203 100644 --- a/app/controllers/pastes_controller.rb +++ b/app/controllers/pastes_controller.rb @@ -17,8 +17,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. class PastesController < ApplicationController - unloadable - include PastesHelper default_search_scope :pastes @@ -31,11 +29,11 @@ def index @limit = per_page_option @pastes_count = @pastes.count - @pastes_pages = Paginator.new(self, @pastes_count, @limit, params[:page]) - @offset ||= @pastes_pages.current.offset - @pastes = @pastes.all(:order => "#{Paste.table_name}.created_on DESC", - :offset => @offset, - :limit => @limit) + @pastes_pages = Paginator.new(@pastes_count, @limit, params[:page]) + @offset ||= @pastes_pages.offset + @pastes = @pastes.order("#{Paste.table_name}.created_on DESC"). + offset(@offset). + limit(@limit) respond_to do |format| format.html { render :layout => false if request.xhr? } @@ -95,7 +93,7 @@ def destroy def find_paste_and_project @project = Project.find(params[:project_id]) if params[:project_id].present? - @pastes = Paste.visible_to(User.current, :project => @project) + @pastes = Paste.visible(User.current, :project => @project) if params[:id].present? if Paste.secure_id?(params[:id]) diff --git a/app/models/paste.rb b/app/models/paste.rb index e8bab7c..2c69053 100644 --- a/app/models/paste.rb +++ b/app/models/paste.rb @@ -19,21 +19,19 @@ require "digest/sha1" class Paste < ActiveRecord::Base - unloadable # really? - attr_accessible :title, :lang, :text belongs_to :project belongs_to :author, :class_name => 'User' - scope :for_project, lambda { |project| + scope :for_project, lambda{ |project| where({ :project_id => project }) } - scope :secure, where("access_token IS NOT NULL") + scope :secure, lambda{ where("access_token IS NOT NULL") } - scope :expired, where("expires_at <= current_timestamp") - scope :unexpired, where("expires_at IS NULL OR expires_at > current_timestamp") + scope :expired, lambda{ where("expires_at <= current_timestamp") } + scope :unexpired, lambda{ where("expires_at IS NULL OR expires_at > current_timestamp") } # # * Restrict to projects where the user has a role allowing to view @@ -50,42 +48,20 @@ class Paste < ActiveRecord::Base scope :visible, lambda{ |user=nil, options={}| user ||= User.current - s = where(Project.allowed_to_condition(user, :view_pastes, options)).includes(:project) + s = where(Project.allowed_to_condition(user, :view_pastes, options)).joins(:project) unless user.admin? s = s.where(["access_token IS NULL OR author_id = ?", user.id]) end s.unexpired } - # - # The default scope limits what's exposed by event providers below. - # - # The use of block is important so that current user is evaluated - # every time inside the visible scope as opposed to being captured - # at the time of Paste class load. - # - default_scope { visible } - - # - # We need to use exclusive scope to be able to specify a user other - # than the current one. Otherwise the default scope will be in - # conflict by overriding the user. - # - def self.visible_to(user, options={}) - with_exclusive_scope do - Paste.visible(user, options) - end - end - - acts_as_searchable :columns => ["#{table_name}.title", "#{table_name}.text"], - :include => :project + acts_as_searchable :columns => ["#{table_name}.title", "#{table_name}.text"] acts_as_event :title => Proc.new{ |o| o.title }, :url => Proc.new{ |o| { :controller => 'pastes', :action => 'show', :id => o.to_param } } - acts_as_activity_provider :find_options => {:include => [:project, :author]}, - :author_key => :author_id + acts_as_activity_provider :author_key => :author_id def title t = super @@ -132,9 +108,7 @@ def self.secure_id?(id) end def self.find_by_secure_id(id) - with_exclusive_scope do - find_by_access_token(id) - end + find_by_access_token(id) end def expired? @@ -146,9 +120,7 @@ def expire_in(seconds) end def self.wipe_all_expired - with_exclusive_scope do - Paste.expired.delete_all - end + Paste.expired.delete_all end private diff --git a/init.rb b/init.rb index 593c6a6..2553e3b 100644 --- a/init.rb +++ b/init.rb @@ -16,17 +16,15 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -require 'redmine' - Redmine::Plugin.register :redmine_pastebin do name 'Redmine Pastebin plugin' - author 'Alex Shulgin ' + author 'Alex Shulgin ' description 'A real pastebin plugin for redmine' - version '0.2.0' + version '0.3.0' url 'https://github.com/commandprompt/redmine_pastebin/' # author_url 'http://example.com/about' - requires_redmine :version_or_higher => '2.0.x' + requires_redmine '3.0.0' project_module :pastes do permission :view_pastes, :pastes => [:index, :show, :download] @@ -59,15 +57,7 @@ end end -prepare_block = Proc.new do +Rails.application.config.after_initialize do Project.send(:include, RedminePastebin::ProjectPastesPatch) User.send(:include, RedminePastebin::UserPastesPatch) end - -if Rails.env.development? - ActionDispatch::Reloader.to_prepare { prepare_block.call } -else - prepare_block.call -end - -require_dependency 'redmine_pastebin/view_hooks'