Commit 89fa800e28e4b740b9de2e526f0f4354d8310ecb
Exists in
master
and in
4 other branches
Merge branch 'refs/heads/snippets_expiration' into dev
Conflicts: db/schema.rb
Showing
11 changed files
with
71 additions
and
31 deletions
Show diff stats
app/helpers/snippets_helper.rb
1 | module SnippetsHelper | 1 | module SnippetsHelper |
2 | + def lifetime_select_options | ||
3 | + options = [ | ||
4 | + ['forever', nil], | ||
5 | + ['1 day', "#{Date.current + 1.day}"], | ||
6 | + ['1 week', "#{Date.current + 1.week}"], | ||
7 | + ['1 month', "#{Date.current + 1.month}"] | ||
8 | + ] | ||
9 | + options_for_select(options) | ||
10 | + end | ||
2 | end | 11 | end |
app/models/snippet.rb
@@ -22,6 +22,9 @@ class Snippet < ActiveRecord::Base | @@ -22,6 +22,9 @@ class Snippet < ActiveRecord::Base | ||
22 | :presence => true, | 22 | :presence => true, |
23 | :length => { :within => 0..10000 } | 23 | :length => { :within => 0..10000 } |
24 | 24 | ||
25 | + scope :fresh, order("created_at DESC") | ||
26 | + scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current]) | ||
27 | + | ||
25 | def self.content_types | 28 | def self.content_types |
26 | [ | 29 | [ |
27 | ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java", | 30 | ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java", |
@@ -33,6 +36,10 @@ class Snippet < ActiveRecord::Base | @@ -33,6 +36,10 @@ class Snippet < ActiveRecord::Base | ||
33 | def colorize | 36 | def colorize |
34 | system_colorize(content, file_name) | 37 | system_colorize(content, file_name) |
35 | end | 38 | end |
39 | + | ||
40 | + def expired? | ||
41 | + expires_at && expires_at < Time.current | ||
42 | + end | ||
36 | end | 43 | end |
37 | # == Schema Information | 44 | # == Schema Information |
38 | # | 45 | # |
@@ -46,5 +53,6 @@ end | @@ -46,5 +53,6 @@ end | ||
46 | # created_at :datetime | 53 | # created_at :datetime |
47 | # updated_at :datetime | 54 | # updated_at :datetime |
48 | # file_name :string(255) | 55 | # file_name :string(255) |
56 | +# expires_at :datetime | ||
49 | # | 57 | # |
50 | 58 |
app/views/projects/_top_menu.html.haml
@@ -23,7 +23,7 @@ | @@ -23,7 +23,7 @@ | ||
23 | = link_to project_snippets_path(@project), :class => (controller.controller_name == "snippets") ? "current" : nil do | 23 | = link_to project_snippets_path(@project), :class => (controller.controller_name == "snippets") ? "current" : nil do |
24 | Snippets | 24 | Snippets |
25 | - if @project.snippets.count > 0 | 25 | - if @project.snippets.count > 0 |
26 | - %span{ :class => "top_menu_count" }= @project.snippets.count | 26 | + %span{ :class => "top_menu_count" }= @project.snippets.non_expired.count |
27 | 27 | ||
28 | - if @commit | 28 | - if @commit |
29 | %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 | 29 | %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 |
app/views/snippets/_form.html.haml
@@ -13,6 +13,9 @@ | @@ -13,6 +13,9 @@ | ||
13 | %td= f.label :file_name | 13 | %td= f.label :file_name |
14 | %td= f.text_field :file_name, :placeholder => "example.rb" | 14 | %td= f.text_field :file_name, :placeholder => "example.rb" |
15 | %tr | 15 | %tr |
16 | + %td= f.label "Lifetime" | ||
17 | + %td= f.select :expires_at, lifetime_select_options | ||
18 | + %tr | ||
16 | %td{:colspan => 2} | 19 | %td{:colspan => 2} |
17 | = f.label :content, "Code" | 20 | = f.label :content, "Code" |
18 | %br | 21 | %br |
app/views/snippets/_snippet.html.haml
1 | -%tr{ :id => dom_id(snippet), :class => "snippet", :url => project_snippet_path(@project, snippet) } | ||
2 | - %td | ||
3 | - = image_tag gravatar_icon(snippet.author.email), :class => "left", :width => 40, :style => "padding:0 5px;" | ||
4 | - = truncate snippet.author.name, :lenght => 20 | ||
5 | - %td= html_escape snippet.title | ||
6 | - %td= html_escape snippet.file_name | ||
7 | - %td | ||
8 | - - if can?(current_user, :admin_snippet, @project) || snippet.author == current_user | ||
9 | - = link_to 'Edit', edit_project_snippet_path(@project, snippet), :class => "lbutton positive" | ||
10 | - - if can?(current_user, :admin_snippet, @project) || snippet.author == current_user | ||
11 | - = link_to 'Destroy', [@project, snippet], :confirm => 'Are you sure?', :method => :delete, :remote => true, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{snippet.id}" | 1 | +- unless snippet.expired? |
2 | + %tr{ :id => dom_id(snippet), :class => "snippet", :url => project_snippet_path(@project, snippet) } | ||
3 | + %td | ||
4 | + = image_tag gravatar_icon(snippet.author.email), :class => "left", :width => 40, :style => "padding:0 5px;" | ||
5 | + = truncate snippet.author.name, :lenght => 20 | ||
6 | + %td= html_escape snippet.title | ||
7 | + %td= html_escape snippet.file_name | ||
8 | + %td | ||
9 | + - if can?(current_user, :admin_snippet, @project) || snippet.author == current_user | ||
10 | + = link_to 'Edit', edit_project_snippet_path(@project, snippet), :class => "lbutton positive" | ||
11 | + - if can?(current_user, :admin_snippet, @project) || snippet.author == current_user | ||
12 | + = link_to 'Destroy', [@project, snippet], :confirm => 'Are you sure?', :method => :delete, :remote => true, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{snippet.id}" |
app/views/snippets/index.html.haml
@@ -8,7 +8,7 @@ | @@ -8,7 +8,7 @@ | ||
8 | %th Title | 8 | %th Title |
9 | %th File name | 9 | %th File name |
10 | %th | 10 | %th |
11 | - = render @snippets | 11 | + = render @snippets.fresh |
12 | :javascript | 12 | :javascript |
13 | $('.delete-snippet').live('ajax:success', function() { | 13 | $('.delete-snippet').live('ajax:success', function() { |
14 | $(this).closest('tr').fadeOut(); }); | 14 | $(this).closest('tr').fadeOut(); }); |
app/views/snippets/show.html.haml
1 | -%h2 | ||
2 | - = "Snippet ##{@snippet.id} - #{@snippet.title}" | 1 | +- if !@snippet.expired? |
2 | + %h2 | ||
3 | + = "Snippet ##{@snippet.id} - #{@snippet.title}" | ||
3 | 4 | ||
4 | -.view_file | ||
5 | - .view_file_header | ||
6 | - %strong | ||
7 | - = @snippet.file_name | ||
8 | - %br/ | ||
9 | - .view_file_content | ||
10 | - :erb | ||
11 | - <%= raw @snippet.colorize %> | 5 | + .view_file |
6 | + .view_file_header | ||
7 | + %strong | ||
8 | + = @snippet.file_name | ||
9 | + %br/ | ||
10 | + .view_file_content | ||
11 | + :erb | ||
12 | + <%= raw @snippet.colorize %> | ||
12 | 13 | ||
13 | -- if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user | ||
14 | - = link_to 'Edit', edit_project_snippet_path(@project, @snippet), :class => "lbutton positive" | ||
15 | -- if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user | ||
16 | - = link_to 'Destroy', [@project, @snippet], :confirm => 'Are you sure?', :method => :delete, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{@snippet.id}" | ||
17 | -.clear | ||
18 | -%br | ||
19 | -.snippet_notes= render "notes/notes" | 14 | + - if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user |
15 | + = link_to 'Edit', edit_project_snippet_path(@project, @snippet), :class => "lbutton positive" | ||
16 | + - if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user | ||
17 | + = link_to 'Destroy', [@project, @snippet], :confirm => 'Are you sure?', :method => :delete, :class => "lbutton delete-snippet negative", :id => "destroy_snippet_#{@snippet.id}" | ||
18 | + .clear | ||
19 | + %br | ||
20 | + .snippet_notes= render "notes/notes" | ||
20 | 21 | ||
21 | -.clear | 22 | + .clear |
22 | 23 | ||
24 | +- else | ||
25 | + %h2 | ||
26 | + Sorry, this snippet is no longer exists |
db/schema.rb
@@ -65,6 +65,7 @@ ActiveRecord::Schema.define(:version => 20111027142641) do | @@ -65,6 +65,7 @@ ActiveRecord::Schema.define(:version => 20111027142641) do | ||
65 | t.datetime "created_at" | 65 | t.datetime "created_at" |
66 | t.datetime "updated_at" | 66 | t.datetime "updated_at" |
67 | t.string "file_name" | 67 | t.string "file_name" |
68 | + t.datetime "expires_at" | ||
68 | end | 69 | end |
69 | 70 | ||
70 | create_table "users", :force => true do |t| | 71 | create_table "users", :force => true do |t| |
spec/models/snippet_spec.rb
spec/requests/snippets_spec.rb
@@ -23,6 +23,14 @@ describe "Snippets" do | @@ -23,6 +23,14 @@ describe "Snippets" do | ||
23 | it { should have_content(@snippet.project.name) } | 23 | it { should have_content(@snippet.project.name) } |
24 | it { should have_content(@snippet.author.name) } | 24 | it { should have_content(@snippet.author.name) } |
25 | 25 | ||
26 | + it "doesn't show expired snippets" do | ||
27 | + @snippet.update_attribute(:expires_at, 1.day.ago.to_time) | ||
28 | + visit project_snippet_path(project, @snippet) | ||
29 | + page.should have_content("Sorry, this snippet is no longer exists") | ||
30 | + page.should_not have_content(@snippet.title) | ||
31 | + page.should_not have_content(@snippet.content) | ||
32 | + end | ||
33 | + | ||
26 | describe "Destroy" do | 34 | describe "Destroy" do |
27 | before do | 35 | before do |
28 | # admin access to remove snippet | 36 | # admin access to remove snippet |