diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f44ec3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Gemfile.lock +.DS_Store diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2c74d0e --- /dev/null +++ b/Gemfile @@ -0,0 +1 @@ +gem "coderay_bash" diff --git a/app/controllers/pastes_controller.rb b/app/controllers/pastes_controller.rb index adbc009..85181c1 100644 --- a/app/controllers/pastes_controller.rb +++ b/app/controllers/pastes_controller.rb @@ -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? } @@ -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 diff --git a/app/helpers/pastes_helper.rb b/app/helpers/pastes_helper.rb index adca49e..3ec7d3a 100644 --- a/app/helpers/pastes_helper.rb +++ b/app/helpers/pastes_helper.rb @@ -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 @@ -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 diff --git a/app/models/paste.rb b/app/models/paste.rb index e8bab7c..f174db1 100644 --- a/app/models/paste.rb +++ b/app/models/paste.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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? @@ -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 diff --git a/app/views/pastes/index.html.erb b/app/views/pastes/index.html.erb index b511890..151ea79 100644 --- a/app/views/pastes/index.html.erb +++ b/app/views/pastes/index.html.erb @@ -4,7 +4,13 @@ <% content_for :header_tags do %> <% end %> @@ -15,18 +21,24 @@
<%= l(:label_no_data) %>
<% else %> <% @pastes.each do |paste| %> + <% cache("paste-short-#{paste.id}-#{User.current.language}", skip_digest: true) do %><%=h paste.short_text %>
diff --git a/app/views/pastes/show.html.erb b/app/views/pastes/show.html.erb index 1bf302f..39475d6 100644 --- a/app/views/pastes/show.html.erb +++ b/app/views/pastes/show.html.erb @@ -13,25 +13,26 @@ table.CodeRay td.line_numbers { <% end %> -<% html_title @paste.title %> +<% cache("paste-#{@paste.id}-#{User.current.language}", skip_digest: true) do %> + <% html_title @paste.title %> -
-<%= l(:label_paste_link_here, :link => link_to(paste_url(@paste), @paste)).html_safe %> -
++ <%= l(:label_paste_link_here, :link => link_to(paste_url(@paste), @paste)).html_safe %> +
-
-<%= render :partial => "authorship", :locals => { :paste => @paste } %>
-<%=l :label_paste_syntax, :lang => pastebin_language_name(@paste.lang) %>
-
+ <%= render :partial => "authorship", :locals => { :paste => @paste } %>
+ <%=l :label_paste_syntax, :lang => pastebin_language_name(@paste.lang) %>
+
-<%= manage_paste_links(@paste) %> -
++ <%= manage_paste_links(@paste) %> +
+<% end %> <%= render "sidebar" if @project %>