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
app/models/snippet.rb
| ... | ... | @@ -22,6 +22,9 @@ class Snippet < ActiveRecord::Base |
| 22 | 22 | :presence => true, |
| 23 | 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 | 28 | def self.content_types |
| 26 | 29 | [ |
| 27 | 30 | ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java", |
| ... | ... | @@ -33,6 +36,10 @@ class Snippet < ActiveRecord::Base |
| 33 | 36 | def colorize |
| 34 | 37 | system_colorize(content, file_name) |
| 35 | 38 | end |
| 39 | + | |
| 40 | + def expired? | |
| 41 | + expires_at && expires_at < Time.current | |
| 42 | + end | |
| 36 | 43 | end |
| 37 | 44 | # == Schema Information |
| 38 | 45 | # |
| ... | ... | @@ -46,5 +53,6 @@ end |
| 46 | 53 | # created_at :datetime |
| 47 | 54 | # updated_at :datetime |
| 48 | 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 | 23 | = link_to project_snippets_path(@project), :class => (controller.controller_name == "snippets") ? "current" : nil do |
| 24 | 24 | Snippets |
| 25 | 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 | 28 | - if @commit |
| 29 | 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 | 13 | %td= f.label :file_name |
| 14 | 14 | %td= f.text_field :file_name, :placeholder => "example.rb" |
| 15 | 15 | %tr |
| 16 | + %td= f.label "Lifetime" | |
| 17 | + %td= f.select :expires_at, lifetime_select_options | |
| 18 | + %tr | |
| 16 | 19 | %td{:colspan => 2} |
| 17 | 20 | = f.label :content, "Code" |
| 18 | 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
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
spec/models/snippet_spec.rb
spec/requests/snippets_spec.rb
| ... | ... | @@ -23,6 +23,14 @@ describe "Snippets" do |
| 23 | 23 | it { should have_content(@snippet.project.name) } |
| 24 | 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 | 34 | describe "Destroy" do |
| 27 | 35 | before do |
| 28 | 36 | # admin access to remove snippet | ... | ... |