Skip to content

Commit

Permalink
Add comments_expire_on Date property to Article.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo van den Boom committed Nov 30, 2008
1 parent a97ad85 commit fc86ac3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG
Expand Up @@ -6,8 +6,8 @@

- Allow author to choose whether comments may be approved automatically
- Allow author to approve/disapprove comments
- Allow author to specify the period in which comments on an article are allowed
- Add comments_expire Date attribute to Article
+ Allow author to specify the period in which comments on an article are allowed
+ Add comments_expire_on Date attribute to Article
- Make /admin/login view use admin layout
- Filter passwords from log file
+ Display user.name instead of user.email in back-end
10 changes: 9 additions & 1 deletion app/models/article.rb
@@ -1,6 +1,6 @@
class Article < Content
# === Properties
# TODO property :comments_expire_on, Date
property :comments_expire_on, Date
property :published_at, DateTime

# === Associations
Expand Down Expand Up @@ -33,6 +33,14 @@ def comments_allowed?
published_at > Date.today - 60 ? true : false
end

def comments_expire_after
comments_expire_on - published_at if published_at
end

def comments_expire_after=(value)
attribute_set(:comments_expire_on, published_at + value.to_i) if published_at
end

# Extract an excerpt from the body
#
# ==== Parameters
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/articles/_form.html.haml
Expand Up @@ -16,4 +16,6 @@
%dt
= label "Publish at", :id => "article_published_at"
.hint Enter a date and time formatted as 'dd-mm-yyyy HH:MM'. Leave this empty to save the page as a draft.
%dd= text_field :published_at, :value => @article.published_at.kind_of?(DateTime) ? @article.published_at.strftime("%d-%m-%Y %H:%M") : nil, :maxlength => 16, :size => 16
%dd= text_field :published_at, :value => @article.published_at.kind_of?(DateTime) ? @article.published_at.strftime("%d-%m-%Y %H:%M") : nil, :maxlength => 16, :size => 16
%dt= label "Set comment expiration", :id => "article_comments_expire_after"
%dd= select :comments_expire_after, :collection => [[30, "After one month"], [0, "Immediately"]]
32 changes: 28 additions & 4 deletions spec/models/article_spec.rb
Expand Up @@ -122,30 +122,54 @@
describe "with comments" do
describe "enabled" do
before(:each) do
@article = Article.new(@valid_attributes.merge(:published_at => Date.today))
@article = Article.new(@valid_attributes.merge(:published_at => Date.today, :comments_expire_on => Date.today + 30))
end

it "should allow comments" do
@article.comments_allowed?.should == true
end

it "should be published no longer than 30 days ago" do
it "should be published no longer than 1 month ago" do
@article.published_at.should > Date.today - 30
end
end

describe "disabled" do
before(:each) do
@article = Article.new(@valid_attributes.merge(:published_at => Date.today - 40.days))
@article = Article.new(@valid_attributes.merge(:published_at => Date.today - 60, :comments_expire_on => Date.today - 30))
end

it "should not allow comments" do
@article.comments_allowed?.should == false
end

it "should have been published more than 30 days ago" do
it "should have been published more than 1 month ago" do
@article.published_at.should < Date.today - 30
end
end

describe "expiration" do
before(:each) do
@article = Article.new(@valid_attributes.merge(:published_at => Date.today))
end

it "should be published" do
@article.published_at.should_not == nil
end

it "should expire comments after one month" do
@article.comments_expire_after = 30
@article.comments_expire_on.should == @article.published_at + 30
end

it "should not be set when article is not published" do
@article.published_at = nil

@article.comments_expire_on.should == nil
@article.comments_expire_after = 30
@article.comments_expire_on.should == nil
@article.comments_expire_after.should == nil
end
end
end
end

0 comments on commit fc86ac3

Please sign in to comment.