Skip to content

Minor improvements #17

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 10 commits 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Gemfile.lock
.DS_Store
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem "coderay_bash"
9 changes: 6 additions & 3 deletions app/controllers/pastes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ class PastesController < ApplicationController

accept_rss_auth :index

accept_api_auth :create

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 = @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 @@ -77,6 +78,8 @@ def update
create
else
if @paste.update_attributes(params[:paste])
expire_fragment("paste-short-#{@paste.id}-#{User.current.language}")
expire_fragment("paste-#{@paste.id}-#{User.current.language}")
flash[:notice] = l(:notice_paste_updated)
redirect_to @paste
else
Expand Down
6 changes: 4 additions & 2 deletions app/helpers/pastes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

module PastesHelper
PASTEBIN_LANGS = ["Plain Text", "C", "C++", "Java", "JavaScript",
"Python", "Ruby", "PHP", "SQL", "XML", "Diff"]
"Python", "Ruby", "PHP", "SQL", "XML", "Diff",
"Clojure", "CSS", "HAML", "HTML", "JSON", "YAML",
"Bash"]

# This maps pretty language/syntax name to identifier that CodeRay
# can understand. If a language is not mapped, unchanged name is
Expand Down Expand Up @@ -57,7 +59,7 @@ def pastebin_language_name(lang)
end

def pastebin_language_choices
PASTEBIN_LANGS.map { |v| [v, pastebin_lang_to_scanner(v)] }
PASTEBIN_LANGS.sort.map { |v| [v, pastebin_lang_to_scanner(v)] }
end

def pastebin_expiration_choices
Expand Down
35 changes: 18 additions & 17 deletions app/models/paste.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Paste < ActiveRecord::Base
where({ :project_id => project })
}

scope :secure, where("access_token IS NOT NULL")
scope :secure, -> { 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, -> { where("expires_at <= current_timestamp") }
scope :unexpired, -> { 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,7 +50,7 @@ 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 = Paste.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
Expand All @@ -72,24 +72,28 @@ class Paste < ActiveRecord::Base
# conflict by overriding the user.
#
def self.visible_to(user, options={})
with_exclusive_scope do
Paste.visible(user, options)
end
Paste.visible(user, options).unscope(:where)
end

acts_as_searchable :columns => ["#{table_name}.title", "#{table_name}.text"],
:include => :project
:preload => :project

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 :scope => preload(:project, :author),
:author_key => :author_id

def title
t = super
t.present? ? t : "Paste ##{id}"
if t.present?
t
elsif not id.nil?
"Paste ##{id}"
else
""
end
end

def description
Expand All @@ -99,6 +103,7 @@ def description
SHORT_TEXT_LIMIT = 100

def short_text
return "" if not text
if text.length < SHORT_TEXT_LIMIT
text
else
Expand Down Expand Up @@ -132,9 +137,7 @@ def self.secure_id?(id)
end

def self.find_by_secure_id(id)
with_exclusive_scope do
find_by_access_token(id)
end
where(:acces_token => id).unscope(:where)
end

def expired?
Expand All @@ -146,9 +149,7 @@ def expire_in(seconds)
end

def self.wipe_all_expired
with_exclusive_scope do
Paste.expired.delete_all
end
Paste.expired.unscope(:where).delete_all
end

private
Expand Down
20 changes: 16 additions & 4 deletions app/views/pastes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
<% content_for :header_tags do %>
<style type="text/css">
.paste {
margin-bottom: 1.5em;
margin-bottom: 2.5em;
}
.paste .author, .paste .actions {
float: right;
}
.paste .box {
margin-bottom: 2px;
}
</style>
<% end %>
Expand All @@ -15,18 +21,24 @@
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<% @pastes.each do |paste| %>
<% cache("paste-short-#{paste.id}-#{User.current.language}", skip_digest: true) do %>
<div class="paste">
<%= link_to_paste paste %>&nbsp;&ndash;
<% unless @project %>
<%= link_to_project paste.project %>&nbsp;&ndash;
<% end %>
<%= pastebin_language_name(paste.lang) %><br>
<div class="author">
<%= render :partial => "authorship", :locals => { :paste => paste } %>
</div>
<%= pastebin_language_name(paste.lang) %>
<div class="box">
<pre><%=h paste.short_text %></pre>
</div>
<%= render :partial => "authorship", :locals => { :paste => paste } %><br>
<%= manage_paste_links(paste) %>
<div class="actions">
<%= manage_paste_links(paste) %>
</div>
</div>
<% end %>
<% end %>

<p class="pagination">
Expand Down
33 changes: 17 additions & 16 deletions app/views/pastes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ table.CodeRay td.line_numbers {
</style>
<% end %>

<% html_title @paste.title %>
<% cache("paste-#{@paste.id}-#{User.current.language}", skip_digest: true) do %>
<% html_title @paste.title %>

<h2><%=h @paste.title %></h2>
<h2><%=h @paste.title %></h2>

<p>
<%= l(:label_paste_link_here, :link => link_to(paste_url(@paste), @paste)).html_safe %>
</p>
<p>
<%= l(:label_paste_link_here, :link => link_to(paste_url(@paste), @paste)).html_safe %>
</p>

<p>
<%= render :partial => "authorship", :locals => { :paste => @paste } %><br>
<%=l :label_paste_syntax, :lang => pastebin_language_name(@paste.lang) %>
</p>
<p>
<%= render :partial => "authorship", :locals => { :paste => @paste } %><br>
<%=l :label_paste_syntax, :lang => pastebin_language_name(@paste.lang) %>
</p>

<div class="paste">
<%= highlighted_content_for_paste(@paste) %>
</div>

<p>
<%= manage_paste_links(@paste) %>
</p>
<div class="paste">
<%= highlighted_content_for_paste(@paste) %>
</div>

<p>
<%= manage_paste_links(@paste) %>
</p>
<% end %>
<%= render "sidebar" if @project %>