Skip to content

Redmine-3.0 compatibility #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions app/controllers/pastes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

class PastesController < ApplicationController
unloadable

include PastesHelper

default_search_scope :pastes
Expand All @@ -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? }
Expand Down Expand Up @@ -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])
Expand Down
46 changes: 9 additions & 37 deletions app/models/paste.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down
18 changes: 4 additions & 14 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ash@commandprompt.com>'
author 'Alex Shulgin <alex.shulgin@gmail.com>'
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]
Expand Down Expand Up @@ -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'