diff --git a/app/controllers/pastes_controller.rb b/app/controllers/pastes_controller.rb index c2220a7..43157d4 100644 --- a/app/controllers/pastes_controller.rb +++ b/app/controllers/pastes_controller.rb @@ -25,7 +25,8 @@ class PastesController < ApplicationController before_filter :find_project, :authorize - accept_key_auth :index + accept_rss_auth :index + accept_api_auth :index, :show, :download, :create, :update, :destroy def index @limit = per_page_option @@ -40,6 +41,7 @@ def index respond_to do |format| format.html { render :layout => false if request.xhr? } format.atom { render_feed(@pastes, :title => (@project ? @project.name : Setting.app_title) + ": " + l(:label_paste_plural)) } + format.api end end @@ -63,10 +65,18 @@ def create @paste = @project.pastes.build(params[:paste]) @paste.author = User.current if @paste.save - flash[:notice] = l(:notice_paste_created) - redirect_to @paste + respond_to do |format| + format.html { + flash[:notice] = l(:notice_paste_created) + redirect_to @paste + } + format.api { render :action => 'show', :status => :created, :location => paste_url(@paste) } + end else - render(params[:fork].blank? ? :new : :edit) + respond_to do |format| + format.html { render(params[:fork].blank? ? :new : :edit) } + format.api { render_validation_errors(@paste) } + end end end @@ -76,9 +86,16 @@ def update else if @paste.update_attributes(params[:paste]) flash[:notice] = l(:notice_paste_updated) - redirect_to @paste + + respond_to do |format| + format.html { redirect_to @paste } + format.api { head :ok } + end else - render :edit + respond_to do |format| + format.html { render :edit } + format.api { render_validation_errors(@paste) } + end end end end @@ -86,7 +103,11 @@ def update def destroy @paste.destroy flash[:notice] = l(:notice_paste_destroyed) - redirect_to pastes_path(:project_id => @project.id) + + respond_to do |format| + format.html { redirect_to pastes_path(:project_id => @project.id) } + format.api { head :ok } + end end private diff --git a/app/helpers/pastes_helper.rb b/app/helpers/pastes_helper.rb index 81bb2fc..09af1ab 100644 --- a/app/helpers/pastes_helper.rb +++ b/app/helpers/pastes_helper.rb @@ -75,7 +75,7 @@ def highlighted_content_for_paste(paste) # TODO: hard-coding code-ray for :table option content_tag :div, :class => "syntaxhl box" do - ::CodeRay.scan(paste.text, paste.lang).html(:line_numbers => :table) + ::CodeRay.scan(paste.text, paste.lang).html(:line_numbers => :table).html_safe end end diff --git a/app/models/paste.rb b/app/models/paste.rb index 7a3ccfc..4d328e4 100644 --- a/app/models/paste.rb +++ b/app/models/paste.rb @@ -24,7 +24,7 @@ class Paste < ActiveRecord::Base belongs_to :project belongs_to :author, :class_name => 'User' - named_scope :for_project, lambda { |project| + scope :for_project, lambda { |project| { :conditions => { :project_id => project } } } @@ -50,6 +50,7 @@ def description SHORT_TEXT_LIMIT = 100 def short_text + return "" if text.nil? if text.length < SHORT_TEXT_LIMIT text else diff --git a/app/views/pastes/_authorship.html.erb b/app/views/pastes/_authorship.html.erb index 5facc2a..ced3f0d 100644 --- a/app/views/pastes/_authorship.html.erb +++ b/app/views/pastes/_authorship.html.erb @@ -1,5 +1,5 @@ -<%= authoring paste.created_on, paste.author %>. +<%= authoring(paste.created_on, paste.author).html_safe %>. <% if paste.updated_on != paste.created_on %> - <%= l(:label_updated_time, time_tag(paste.updated_on)) %>. + <%= l(:label_updated_time, time_tag(paste.updated_on)).html_safe %>. <% end %> diff --git a/app/views/pastes/edit.html.erb b/app/views/pastes/edit.html.erb index e94c91a..ec9e5b1 100644 --- a/app/views/pastes/edit.html.erb +++ b/app/views/pastes/edit.html.erb @@ -6,10 +6,11 @@ <%= render :partial => "authorship", :locals => { :paste => @paste } %>

-<% labelled_tabular_form_for :paste, @paste, +<%= labelled_form_for @paste, :url => { :action => "update", :id => @paste }, :html => { :method => :put } do |f| %>
+
<%= hidden_field_tag "fork" %>

<%= f.text_field :title, :label => :field_paste_title %>

@@ -17,10 +18,11 @@ :label => :field_paste_lang %>

<%= f.text_area :text, :label => :field_paste_text, :rows => 25, :cols => 80 %>

+
-<%=l :text_paste_update_or_make_new, +<%=l(:text_paste_update_or_make_new, :update => f.submit(l(:button_paste_update)), :make_new => submit_tag(l(:button_paste_make_new), - :onclick => "$('fork').value = 'yes'") %> + :onclick => "$('fork').value = 'yes'")).html_safe %> <% end %> diff --git a/app/views/pastes/index.api.rsb b/app/views/pastes/index.api.rsb new file mode 100644 index 0000000..1d0cd04 --- /dev/null +++ b/app/views/pastes/index.api.rsb @@ -0,0 +1,17 @@ +api.array :pastes, api_meta(:total_count => @pastes_count, :offset => @offset, :limit => @limit) do + @pastes.each do |paste| + api.paste do + api.id paste.id + api.author_id paste.author_id + api.project_id paste.project_id + + api.lang paste.lang + api.title paste.title + api.text paste.text + + api.created_on paste.created_on + api.updated_on paste.updated_on + end + end +end + diff --git a/app/views/pastes/index.html.erb b/app/views/pastes/index.html.erb index 0fba42e..16fe7ad 100644 --- a/app/views/pastes/index.html.erb +++ b/app/views/pastes/index.html.erb @@ -4,8 +4,21 @@ <% content_for :header_tags do %> <% end %> @@ -16,19 +29,30 @@

<%= l(:label_no_data) %>

<% else %> <% @pastes.each do |paste| %> -

- <%= paste_timestamp(paste) %> – - <%= link_to_paste paste %> – +

+ +

+ <%= pastebin_language_name(paste.lang) %> +
+ + <%= manage_paste_links(paste).html_safe %> + +

+ + <%= link_to_paste paste %> <% unless @project %> - <%= link_to_project paste.project %> – + <%= link_to_project(paste.project).html_safe %> <% end %> - <%= pastebin_language_name(paste.lang) %> – - <%=h paste.short_text %>
- - <%= render :partial => "authorship", :locals => { :paste => paste } %> +
+ <%= paste.short_text %> +
+
+ + <%= render :partial => "authorship", :locals => { :paste => paste } %> + - <%= manage_paste_links(paste) %> -

+
+

<% end %>

diff --git a/app/views/pastes/new.html.erb b/app/views/pastes/new.html.erb index bcdb98e..1558f66 100644 --- a/app/views/pastes/new.html.erb +++ b/app/views/pastes/new.html.erb @@ -2,14 +2,16 @@

<%=l :label_paste_new %>

-<% labelled_tabular_form_for :paste, @paste, :url => { :action => "create", +<%= labelled_form_for @paste, :url => { :action => "create", :project_id => @paste.project.id } do |f| %>
+

<%= f.text_field :title, :label => :field_paste_title %>

<%= f.select :lang, pastebin_language_choices, :label => :field_paste_lang %>

<%= f.text_area :text, :label => :field_paste_text, :rows => 25, :cols => 80 %>

+
<%= f.submit l(:button_paste_submit) %> <% end %> diff --git a/app/views/pastes/show.api.rsb b/app/views/pastes/show.api.rsb new file mode 100644 index 0000000..0ed93b1 --- /dev/null +++ b/app/views/pastes/show.api.rsb @@ -0,0 +1,12 @@ +api.paste do + api.id @paste.id + api.author_id @paste.author_id + api.project_id @paste.project_id + + api.lang @paste.lang + api.title @paste.title + api.text @paste.text + + api.created_on @paste.created_on + api.updated_on @paste.updated_on +end diff --git a/app/views/pastes/show.html.erb b/app/views/pastes/show.html.erb index 0f8b65a..4a7a84d 100644 --- a/app/views/pastes/show.html.erb +++ b/app/views/pastes/show.html.erb @@ -18,7 +18,7 @@ table.CodeRay td.line_numbers {

<%=h @paste.title %>

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

@@ -27,11 +27,11 @@ table.CodeRay td.line_numbers {

-<%= highlighted_content_for_paste(@paste) %> +<%= highlighted_content_for_paste(@paste).html_safe %>

-<%= manage_paste_links(@paste) %> +<%= manage_paste_links(@paste).html_safe %>

<%= render "sidebar" %> diff --git a/config/routes.rb b/config/routes.rb index d3c0d10..694ca06 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,23 @@ -ActionController::Routing::Routes.draw do |map| - map.resources :pastes, :only => [:index, :show], - :member => { :download => :get } - map.resources :pastes, :path_prefix => '/projects/:project_id', - :member => { :download => :get } +RedmineApp::Application.routes.draw do + + resources :pastes do + member do + get 'download' + end + end + + resources :projects do + resources :pastes do + member do + get 'download' + end + end + end + + + + # map.resources :pastes, :only => [:index, :show], + # :member => { :download => :get } + # map.resources :pastes, :path_prefix => '/projects/:project_id', + # :member => { :download => :get } end diff --git a/init.rb b/init.rb index f7beb2c..4a7caf1 100644 --- a/init.rb +++ b/init.rb @@ -16,22 +16,20 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +YAML::ENGINE.yamler = 'syck' require 'redmine' -require 'dispatcher' require_dependency 'redmine_pastebin/view_hooks' -Dispatcher.to_prepare :redmine_pastebin do - require_dependency 'project' - require_dependency 'user' +require_dependency 'project' +require_dependency 'user' - unless Project.included_modules.include? RedminePastebin::ProjectPastesPatch - Project.send(:include, RedminePastebin::ProjectPastesPatch) - end +unless Project.included_modules.include? RedminePastebin::ProjectPastesPatch + Project.send(:include, RedminePastebin::ProjectPastesPatch) +end - unless User.included_modules.include? RedminePastebin::UserPastesPatch - User.send(:include, RedminePastebin::UserPastesPatch) - end +unless User.included_modules.include? RedminePastebin::UserPastesPatch + User.send(:include, RedminePastebin::UserPastesPatch) end Redmine::Plugin.register :redmine_pastebin do