From 117d1e90fdef3aaf64901ba18a5ce5bab4c668d8 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 10:20:45 +0500 Subject: [PATCH 1/8] add expires_at column to snippets --- db/migrate/20111027051828_add_expires_at_to_snippets.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20111027051828_add_expires_at_to_snippets.rb diff --git a/db/migrate/20111027051828_add_expires_at_to_snippets.rb b/db/migrate/20111027051828_add_expires_at_to_snippets.rb new file mode 100644 index 00000000..0d94b333 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 21c224d4..44b9375a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111025134235) do +ActiveRecord::Schema.define(:version => 20111027051828) do create_table "issues", :force => true do |t| t.string "title" @@ -65,6 +65,7 @@ ActiveRecord::Schema.define(:version => 20111025134235) do t.datetime "created_at" t.datetime "updated_at" t.string "file_name" + t.datetime "expires_at" end create_table "users", :force => true do |t| From 8e1e17763f169f63dc99c7a0f109fe98c87b4064 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 11:46:21 +0500 Subject: [PATCH 2/8] implement snippets lifetime --- app/helpers/snippets_helper.rb | 9 ++++++ app/models/snippet.rb | 4 +++ app/views/snippets/_form.html.haml | 3 ++ app/views/snippets/_snippet.html.haml | 23 +++++++-------- app/views/snippets/show.html.haml | 40 +++++++++++++++------------ 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/app/helpers/snippets_helper.rb b/app/helpers/snippets_helper.rb index 236b6c8c..cd7b0359 100644 --- a/app/helpers/snippets_helper.rb +++ b/app/helpers/snippets_helper.rb @@ -1,2 +1,11 @@ module SnippetsHelper + def snippet_lifetime_select_options + options = [ + ['forever', nil], + ['1 day', Date.strptime("#{Date.current.day}.#{Date.current.month}.#{Date.current.year}", "%d.%m.%Y") + 1.day], + ['1 week', Date.strptime("#{Date.current.day}.#{Date.current.month}.#{Date.current.year}", "%d.%m.%Y") + 1.week], + ['1 month', Date.strptime("#{Date.current.day}.#{Date.current.month}.#{Date.current.year}", "%d.%m.%Y") + 1.month] + ] + options_for_select(options) + end end diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 0f488a8c..acffc76c 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -33,6 +33,10 @@ class Snippet < ActiveRecord::Base def colorize system_colorize(content, file_name) end + + def expired? + expires_at && expires_at < Time.current + end end # == Schema Information # diff --git a/app/views/snippets/_form.html.haml b/app/views/snippets/_form.html.haml index 7a34ae8e..2c9680c0 100644 --- a/app/views/snippets/_form.html.haml +++ b/app/views/snippets/_form.html.haml @@ -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, snippet_lifetime_select_options %tr %td{:colspan => 2} = f.label :content, "Code" diff --git a/app/views/snippets/_snippet.html.haml b/app/views/snippets/_snippet.html.haml index 483ff42c..ddfba6bf 100644 --- a/app/views/snippets/_snippet.html.haml +++ b/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}" diff --git a/app/views/snippets/show.html.haml b/app/views/snippets/show.html.haml index 899950b7..bfa1bf45 100644 --- a/app/views/snippets/show.html.haml +++ b/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 snipped is no longer exists From de4e859928d22a44549ebb719bbfd669f630f1b1 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 11:50:14 +0500 Subject: [PATCH 3/8] install timecop for testing --- Gemfile | 3 ++- Gemfile.lock | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 28168095..2f3e870c 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ group :assets do gem 'uglifier' end -group :development do +group :development do gem 'rails-footnotes', '>= 3.7.5.rc4' gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git' end @@ -42,6 +42,7 @@ group :development, :test do gem 'awesome_print' gem 'database_cleaner' gem 'launchy' + gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index 01ec37e1..1f40f712 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -225,6 +225,7 @@ GEM rack (>= 1.0.0) thor (0.14.6) tilt (1.3.3) + timecop (0.3.5) treetop (1.4.10) polyglot polyglot (>= 0.3.1) @@ -276,6 +277,7 @@ DEPENDENCIES stamp therubyracer thin + timecop turn uglifier will_paginate (~> 3.0) From 5e584ee6264962cc60d9074c33141a28b4100607 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 12:11:42 +0500 Subject: [PATCH 4/8] refactor lifetime_select helper --- app/helpers/snippets_helper.rb | 8 ++++---- app/views/snippets/_form.html.haml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/helpers/snippets_helper.rb b/app/helpers/snippets_helper.rb index cd7b0359..6a91d616 100644 --- a/app/helpers/snippets_helper.rb +++ b/app/helpers/snippets_helper.rb @@ -1,10 +1,10 @@ module SnippetsHelper - def snippet_lifetime_select_options + def lifetime_select_options options = [ ['forever', nil], - ['1 day', Date.strptime("#{Date.current.day}.#{Date.current.month}.#{Date.current.year}", "%d.%m.%Y") + 1.day], - ['1 week', Date.strptime("#{Date.current.day}.#{Date.current.month}.#{Date.current.year}", "%d.%m.%Y") + 1.week], - ['1 month', Date.strptime("#{Date.current.day}.#{Date.current.month}.#{Date.current.year}", "%d.%m.%Y") + 1.month] + ['1 day', "#{Date.current + 1.day}"], + ['1 week', "#{Date.current + 1.week}"], + ['1 month', "#{Date.current + 1.month}"] ] options_for_select(options) end diff --git a/app/views/snippets/_form.html.haml b/app/views/snippets/_form.html.haml index 2c9680c0..5cd0f74a 100644 --- a/app/views/snippets/_form.html.haml +++ b/app/views/snippets/_form.html.haml @@ -14,7 +14,7 @@ %td= f.text_field :file_name, :placeholder => "example.rb" %tr %td= f.label "Lifetime" - %td= f.select :expires_at, snippet_lifetime_select_options + %td= f.select :expires_at, lifetime_select_options %tr %td{:colspan => 2} = f.label :content, "Code" From 000c032482dbe2fe882f3b2583fe1a481edf460a Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 12:14:50 +0500 Subject: [PATCH 5/8] display recent snippets at top --- app/models/snippet.rb | 2 ++ app/views/snippets/index.html.haml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/snippet.rb b/app/models/snippet.rb index acffc76c..44cb8887 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -22,6 +22,8 @@ class Snippet < ActiveRecord::Base :presence => true, :length => { :within => 0..10000 } + scope :fresh, order("created_at DESC") + def self.content_types [ ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java", diff --git a/app/views/snippets/index.html.haml b/app/views/snippets/index.html.haml index fe5e6170..3f261000 100644 --- a/app/views/snippets/index.html.haml +++ b/app/views/snippets/index.html.haml @@ -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(); }); From 325b84545ca09911df0bf80a7233b339aba755ed Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 13:12:12 +0500 Subject: [PATCH 6/8] don't count expired snippets --- app/models/snippet.rb | 1 + app/views/projects/_top_menu.html.haml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 44cb8887..b67db60b 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -23,6 +23,7 @@ class Snippet < ActiveRecord::Base :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 [ diff --git a/app/views/projects/_top_menu.html.haml b/app/views/projects/_top_menu.html.haml index 59f2533e..0b8751c9 100644 --- a/app/views/projects/_top_menu.html.haml +++ b/app/views/projects/_top_menu.html.haml @@ -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 From 01f72bfa83ef05817c2e9898a62ae09919a6d505 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 27 Oct 2011 23:35:16 +0500 Subject: [PATCH 7/8] Revert "install timecop for testing" This reverts commit 0aa5682f8e3d2f50443fa8ac457daf5c9784dfd8. --- Gemfile | 3 +-- Gemfile.lock | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 2f3e870c..28168095 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ group :assets do gem 'uglifier' end -group :development do +group :development do gem 'rails-footnotes', '>= 3.7.5.rc4' gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git' end @@ -42,7 +42,6 @@ group :development, :test do gem 'awesome_print' gem 'database_cleaner' gem 'launchy' - gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index 1f40f712..01ec37e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -225,7 +225,6 @@ GEM rack (>= 1.0.0) thor (0.14.6) tilt (1.3.3) - timecop (0.3.5) treetop (1.4.10) polyglot polyglot (>= 0.3.1) @@ -277,7 +276,6 @@ DEPENDENCIES stamp therubyracer thin - timecop turn uglifier will_paginate (~> 3.0) From b6cdd1c819b4a43cd00dfdc9dbffc1490593a8b5 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Fri, 28 Oct 2011 00:25:50 +0500 Subject: [PATCH 8/8] test expired snippets --- app/models/snippet.rb | 1 + app/views/snippets/show.html.haml | 2 +- spec/models/snippet_spec.rb | 1 + spec/requests/snippets_spec.rb | 8 ++++++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/snippet.rb b/app/models/snippet.rb index b67db60b..5c61cf1c 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -53,5 +53,6 @@ end # created_at :datetime # updated_at :datetime # file_name :string(255) +# expires_at :datetime # diff --git a/app/views/snippets/show.html.haml b/app/views/snippets/show.html.haml index bfa1bf45..757cdb11 100644 --- a/app/views/snippets/show.html.haml +++ b/app/views/snippets/show.html.haml @@ -23,4 +23,4 @@ - else %h2 - Sorry, this snipped is no longer exists + Sorry, this snippet is no longer exists diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index 9dab72ca..037287a9 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -26,5 +26,6 @@ end # created_at :datetime # updated_at :datetime # file_name :string(255) +# expires_at :datetime # diff --git a/spec/requests/snippets_spec.rb b/spec/requests/snippets_spec.rb index d4811958..ee4f90e6 100644 --- a/spec/requests/snippets_spec.rb +++ b/spec/requests/snippets_spec.rb @@ -23,6 +23,14 @@ describe "Snippets" do 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