Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of https://github.com/radar/forem

  • Loading branch information...
commit 76330bcbec94ec411af9add7e2d9b4873afa933a 2 parents 0065cff + e482122
@jgadbois authored
View
20 app/models/forem/concerns/viewable.rb
@@ -15,17 +15,17 @@ def view_for(user)
# Track when users last viewed topics
def register_view_by(user)
- if user
- view = views.find_or_create_by_user_id(user.id)
- view.increment!("count")
- increment!(:views_count)
+ return unless user
- # update current viewed at if more than 15 minutes ago
- if view.current_viewed_at < 15.minutes.ago
- view.past_viewed_at = view.current_viewed_at
- view.current_viewed_at = Time.now
- view.save
- end
+ view = views.find_or_create_by_user_id(user.id)
+ view.increment!("count")
+ increment!(:views_count)
+
+ # update current viewed at if more than 15 minutes ago
+ if view.current_viewed_at < 15.minutes.ago
+ view.past_viewed_at = view.current_viewed_at
+ view.current_viewed_at = Time.now
+ view.save
end
end
end
View
10 app/models/forem/forum.rb
@@ -3,15 +3,13 @@ class Forum < ActiveRecord::Base
include Forem::Concerns::Viewable
belongs_to :category
- has_many :topics, :dependent => :destroy
- has_many :posts, :through => :topics, :dependent => :destroy
- has_many :moderator_groups
+ has_many :topics, :dependent => :destroy
+ has_many :posts, :through => :topics, :dependent => :destroy
has_many :moderators, :through => :moderator_groups, :source => :group
+ has_many :moderator_groups
- validates :category, :presence => true
- validates :title, :presence => true
- validates :description, :presence => true
+ validates :category, :title, :description, :presence => true
def last_post_for(forem_user)
forem_user && forem_user.forem_admin? || moderator?(forem_user) ? posts.last : last_visible_post
View
19 app/models/forem/post.rb
@@ -1,10 +1,11 @@
module Forem
class Post < ActiveRecord::Base
include Workflow
+
workflow_column :state
workflow do
state :pending_review do
- event :spam, :transitions_to => :spam
+ event :spam, :transitions_to => :spam
event :approve, :transitions_to => :approved
end
state :spam
@@ -15,26 +16,24 @@ class Post < ActiveRecord::Base
attr_accessor :moderation_option
belongs_to :topic
- belongs_to :user, :class_name => Forem.user_class.to_s
+ belongs_to :user, :class_name => Forem.user_class.to_s
belongs_to :reply_to, :class_name => "Post"
- has_many :replies, :class_name => "Post",
+ has_many :replies, :class_name => "Post",
:foreign_key => "reply_to_id",
- :dependent => :nullify
-
- delegate :forum, :to => :topic
+ :dependent => :nullify
validates :text, :presence => true
- after_save :email_topic_subscribers, :if => Proc.new { |p| p.approved? && !p.notified? }
+ delegate :forum, :to => :topic
after_create :set_topic_last_post_at
- after_create :subscribe_replier
+ after_create :subscribe_replier, :if => Proc.new { |p| p.user && p.user.forem_auto_subscribe? }
after_create :skip_pending_review_if_user_approved
- after_save :approve_user, :if => :approved?
+ after_save :approve_user, :if => :approved?
after_save :blacklist_user, :if => :spam?
-
+ after_save :email_topic_subscribers, :if => Proc.new { |p| p.approved? && !p.notified? }
class << self
def approved
View
3  app/models/forem/subscription.rb
@@ -2,7 +2,8 @@ module Forem
class Subscription < ActiveRecord::Base
belongs_to :topic
belongs_to :subscriber, :class_name => Forem.user_class.to_s
- validates_presence_of :subscriber_id
+
+ validates :subscriber_id, :presence => true
def send_notification(post_id)
SubscriptionMailer.topic_reply(post_id, self.subscriber.id).deliver
View
24 app/models/forem/topic.rb
@@ -1,12 +1,12 @@
module Forem
class Topic < ActiveRecord::Base
include Forem::Concerns::Viewable
-
include Workflow
+
workflow_column :state
workflow do
state :pending_review do
- event :spam, :transitions_to => :spam
+ event :spam, :transitions_to => :spam
event :approve, :transitions_to => :approved
end
state :spam
@@ -17,21 +17,19 @@ class Topic < ActiveRecord::Base
attr_accessible :subject, :posts_attributes
- attr_protected :pinned, :locked
-
belongs_to :forum
- has_many :subscriptions
belongs_to :user, :class_name => Forem.user_class.to_s
+ has_many :subscriptions
+ has_many :posts, :dependent => :destroy, :order => "forem_posts.created_at ASC"
- has_many :posts, :dependent => :destroy, :order => "forem_posts.created_at ASC"
accepts_nested_attributes_for :posts
validates :subject, :presence => true
- before_save :set_first_post_user
+ before_save :set_first_post_user
+ after_save :approve_user_and_posts, :if => :approved?
after_create :subscribe_poster
after_create :skip_pending_review_if_user_approved
- after_save :approve_user_and_posts, :if => :approved?
class << self
def visible
@@ -138,11 +136,11 @@ def skip_pending_review_if_user_approved
end
def approve_user_and_posts
- if state_changed?
- first_post = self.posts.by_created_at.first
- first_post.approve! unless first_post.approved?
- self.user.update_attribute(:forem_state, 'approved') if self.user.forem_state != 'approved'
- end
+ return unless state_changed?
+
+ first_post = self.posts.by_created_at.first
+ first_post.approve! unless first_post.approved?
+ self.user.update_attribute(:forem_state, 'approved') if self.user.forem_state != 'approved'
end
end
end
View
2  app/models/forem/view.rb
@@ -5,7 +5,7 @@ class View < ActiveRecord::Base
belongs_to :viewable, :polymorphic => true
belongs_to :user, :class_name => Forem.user_class.to_s
- validates :viewable_id, :presence => true
+ validates :viewable_id, :presence => true
validates :viewable_type, :presence => true
def viewed_at
View
5 db/migrate/20120414183053_add_forem_auto_subscribe_to_user.rb
@@ -0,0 +1,5 @@
+class AddForemAutoSubscribeToUser < ActiveRecord::Migration
+ def change
+ add_column :users, :forem_auto_subscribe, :boolean, :default => false
+ end
+end
View
16 spec/models/post_spec.rb
@@ -16,11 +16,21 @@
end
context "after creation" do
- it "subscribes the current poster" do
+ it "subscribes the current poster if forem_auto_subscribe is set to true" do
@topic = FactoryGirl.create(:topic)
@post = FactoryGirl.create(:post, :topic => @topic)
@topic.subscriptions.last.subscriber.should == @post.user
end
+
+ it "doesn't subscribe the current poster if forem_auto_subscribe is set to false" do
+ @topic = FactoryGirl.create(:topic)
+ @post = FactoryGirl.create(:post, :topic => @topic)
+
+ @user_not_autosubscribed = FactoryGirl.create(:not_autosubscribed)
+ @post = FactoryGirl.build(:approved_post, :topic => @topic, :user => @user_not_autosubscribed)
+
+ @topic.subscriptions.last.subscriber.should_not == @post.user
+ end
it "does not email subscribers after post creation if not approved" do
@post = FactoryGirl.build(:post, :topic => topic)
@@ -55,11 +65,11 @@
topic = FactoryGirl.create(:topic)
post1 = FactoryGirl.create(:post, :topic => topic)
topic.reload
- topic.last_post_at.should == post1.created_at
+ topic.last_post_at.to_s.should == post1.created_at.to_s
post2 = FactoryGirl.create(:post, :topic => topic)
topic.reload
- topic.last_post_at.should == post2.created_at
+ topic.last_post_at.to_s.should == post2.created_at.to_s
end
end
View
5 spec/support/factories/users.rb
@@ -4,10 +4,15 @@
f.email { "bob#{rand(100000)}@boblaw.com" }
f.password "password"
f.password_confirmation "password"
+ f.forem_auto_subscribe true
factory :admin do |f|
f.forem_admin true
end
+
+ factory :not_autosubscribed do |f|
+ f.forem_auto_subscribe false
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.