Skip to content

Commit

Permalink
Merge branch 'refs/heads/snippets_expiration' into dev
Browse files Browse the repository at this point in the history
Conflicts:
	db/schema.rb
  • Loading branch information
NARKOZ committed Oct 27, 2011
2 parents f4e7ad7 + b6cdd1c commit 89fa800
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 31 deletions.
9 changes: 9 additions & 0 deletions app/helpers/snippets_helper.rb
@@ -1,2 +1,11 @@
module SnippetsHelper
def lifetime_select_options
options = [
['forever', nil],
['1 day', "#{Date.current + 1.day}"],
['1 week', "#{Date.current + 1.week}"],
['1 month', "#{Date.current + 1.month}"]
]
options_for_select(options)
end
end
8 changes: 8 additions & 0 deletions app/models/snippet.rb
Expand Up @@ -22,6 +22,9 @@ class Snippet < ActiveRecord::Base
:presence => true,
:length => { :within => 0..10000 }

scope :fresh, order("created_at DESC")
scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])

def self.content_types
[
".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
Expand All @@ -33,6 +36,10 @@ def self.content_types
def colorize
system_colorize(content, file_name)
end

def expired?
expires_at && expires_at < Time.current
end
end
# == Schema Information
#
Expand All @@ -46,5 +53,6 @@ def colorize
# created_at :datetime
# updated_at :datetime
# file_name :string(255)
# expires_at :datetime
#

2 changes: 1 addition & 1 deletion app/views/projects/_top_menu.html.haml
Expand Up @@ -23,7 +23,7 @@
= link_to project_snippets_path(@project), :class => (controller.controller_name == "snippets") ? "current" : nil do
Snippets
- if @project.snippets.count > 0
%span{ :class => "top_menu_count" }= @project.snippets.count
%span{ :class => "top_menu_count" }= @project.snippets.non_expired.count

- if @commit
%span= link_to truncate(commit_name(@project,@commit), :length => 15), project_commit_path(@project, :id => @commit.id), :class => current_page?(:controller => "commits", :action => "show", :project_id => @project, :id => @commit.id) ? "current" : nil
Expand Down
3 changes: 3 additions & 0 deletions app/views/snippets/_form.html.haml
Expand Up @@ -12,6 +12,9 @@
%tr
%td= f.label :file_name
%td= f.text_field :file_name, :placeholder => "example.rb"
%tr
%td= f.label "Lifetime"
%td= f.select :expires_at, lifetime_select_options
%tr
%td{:colspan => 2}
= f.label :content, "Code"
Expand Down
23 changes: 12 additions & 11 deletions app/views/snippets/_snippet.html.haml
@@ -1,11 +1,12 @@
%tr{ :id => dom_id(snippet), :class => "snippet", :url => project_snippet_path(@project, snippet) }
%td
= image_tag gravatar_icon(snippet.author.email), :class => "left", :width => 40, :style => "padding:0 5px;"
= truncate snippet.author.name, :lenght => 20
%td= html_escape snippet.title
%td= html_escape snippet.file_name
%td
- if can?(current_user, :admin_snippet, @project) || snippet.author == current_user
= link_to 'Edit', edit_project_snippet_path(@project, snippet), :class => "lbutton positive"
- if can?(current_user, :admin_snippet, @project) || snippet.author == current_user
= link_to 'Destroy', [@project, snippet], :confirm => 'Are you sure?', :method => :delete, :remote => true, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{snippet.id}"
- unless snippet.expired?
%tr{ :id => dom_id(snippet), :class => "snippet", :url => project_snippet_path(@project, snippet) }
%td
= image_tag gravatar_icon(snippet.author.email), :class => "left", :width => 40, :style => "padding:0 5px;"
= truncate snippet.author.name, :lenght => 20
%td= html_escape snippet.title
%td= html_escape snippet.file_name
%td
- if can?(current_user, :admin_snippet, @project) || snippet.author == current_user
= link_to 'Edit', edit_project_snippet_path(@project, snippet), :class => "lbutton positive"
- if can?(current_user, :admin_snippet, @project) || snippet.author == current_user
= link_to 'Destroy', [@project, snippet], :confirm => 'Are you sure?', :method => :delete, :remote => true, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{snippet.id}"
2 changes: 1 addition & 1 deletion app/views/snippets/index.html.haml
Expand Up @@ -8,7 +8,7 @@
%th Title
%th File name
%th
= render @snippets
= render @snippets.fresh
:javascript
$('.delete-snippet').live('ajax:success', function() {
$(this).closest('tr').fadeOut(); });
40 changes: 22 additions & 18 deletions app/views/snippets/show.html.haml
@@ -1,22 +1,26 @@
%h2
= "Snippet ##{@snippet.id} - #{@snippet.title}"
- if !@snippet.expired?
%h2
= "Snippet ##{@snippet.id} - #{@snippet.title}"

.view_file
.view_file_header
%strong
= @snippet.file_name
%br/
.view_file_content
:erb
<%= raw @snippet.colorize %>
.view_file
.view_file_header
%strong
= @snippet.file_name
%br/
.view_file_content
:erb
<%= raw @snippet.colorize %>

- if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user
= link_to 'Edit', edit_project_snippet_path(@project, @snippet), :class => "lbutton positive"
- if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user
= link_to 'Destroy', [@project, @snippet], :confirm => 'Are you sure?', :method => :delete, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{@snippet.id}"
.clear
%br
.snippet_notes= render "notes/notes"
- if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user
= link_to 'Edit', edit_project_snippet_path(@project, @snippet), :class => "lbutton positive"
- if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user
= link_to 'Destroy', [@project, @snippet], :confirm => 'Are you sure?', :method => :delete, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{@snippet.id}"
.clear
%br
.snippet_notes= render "notes/notes"

.clear
.clear

- else
%h2
Sorry, this snippet is no longer exists
5 changes: 5 additions & 0 deletions db/migrate/20111027051828_add_expires_at_to_snippets.rb
@@ -0,0 +1,5 @@
class AddExpiresAtToSnippets < ActiveRecord::Migration
def change
add_column :snippets, :expires_at, :datetime
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Expand Up @@ -65,6 +65,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "file_name"
t.datetime "expires_at"
end

create_table "users", :force => true do |t|
Expand Down
1 change: 1 addition & 0 deletions spec/models/snippet_spec.rb
Expand Up @@ -26,5 +26,6 @@
# created_at :datetime
# updated_at :datetime
# file_name :string(255)
# expires_at :datetime
#

8 changes: 8 additions & 0 deletions spec/requests/snippets_spec.rb
Expand Up @@ -23,6 +23,14 @@
it { should have_content(@snippet.project.name) }
it { should have_content(@snippet.author.name) }

it "doesn't show expired snippets" do
@snippet.update_attribute(:expires_at, 1.day.ago.to_time)
visit project_snippet_path(project, @snippet)
page.should have_content("Sorry, this snippet is no longer exists")
page.should_not have_content(@snippet.title)
page.should_not have_content(@snippet.content)
end

describe "Destroy" do
before do
# admin access to remove snippet
Expand Down

0 comments on commit 89fa800

Please sign in to comment.