ack-mongoid-forums 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +35 -0
  4. data/app/assets/javascripts/mongoid_forums/admin/base.js +2 -0
  5. data/app/assets/javascripts/mongoid_forums/admin/categories.js +2 -0
  6. data/app/assets/javascripts/mongoid_forums/admin/forums.js +2 -0
  7. data/app/assets/javascripts/mongoid_forums/admin/groups.js +2 -0
  8. data/app/assets/javascripts/mongoid_forums/admin/users.js +2 -0
  9. data/app/assets/javascripts/mongoid_forums/application.js +15 -0
  10. data/app/assets/javascripts/mongoid_forums/forums.js +2 -0
  11. data/app/assets/javascripts/mongoid_forums/posts.js +2 -0
  12. data/app/assets/javascripts/mongoid_forums/topics.js +2 -0
  13. data/app/assets/stylesheets/mongoid_forums/admin/base.css +4 -0
  14. data/app/assets/stylesheets/mongoid_forums/admin/categories.css +4 -0
  15. data/app/assets/stylesheets/mongoid_forums/admin/forums.css +4 -0
  16. data/app/assets/stylesheets/mongoid_forums/admin/groups.css +4 -0
  17. data/app/assets/stylesheets/mongoid_forums/admin/users.css +4 -0
  18. data/app/assets/stylesheets/mongoid_forums/application.css +15 -0
  19. data/app/assets/stylesheets/mongoid_forums/forums.css +4 -0
  20. data/app/assets/stylesheets/mongoid_forums/posts.css +4 -0
  21. data/app/assets/stylesheets/mongoid_forums/topics.css +4 -0
  22. data/app/assets/stylesheets/scaffold.css +56 -0
  23. data/app/controllers/mongoid_forums/admin/base_controller.rb +18 -0
  24. data/app/controllers/mongoid_forums/admin/categories_controller.rb +83 -0
  25. data/app/controllers/mongoid_forums/admin/forums_controller.rb +83 -0
  26. data/app/controllers/mongoid_forums/admin/groups_controller.rb +83 -0
  27. data/app/controllers/mongoid_forums/admin/topics_controller.rb +48 -0
  28. data/app/controllers/mongoid_forums/admin/users_controller.rb +34 -0
  29. data/app/controllers/mongoid_forums/application_controller.rb +57 -0
  30. data/app/controllers/mongoid_forums/forums_controller.rb +66 -0
  31. data/app/controllers/mongoid_forums/posts_controller.rb +133 -0
  32. data/app/controllers/mongoid_forums/redirect_controller.rb +33 -0
  33. data/app/controllers/mongoid_forums/topics_controller.rb +83 -0
  34. data/app/decorators/lib/mongoid_forums/user_class_decorator.rb +1 -0
  35. data/app/helpers/mongoid_forums/admin/base_helper.rb +4 -0
  36. data/app/helpers/mongoid_forums/admin/categories_helper.rb +4 -0
  37. data/app/helpers/mongoid_forums/admin/forums_helper.rb +4 -0
  38. data/app/helpers/mongoid_forums/admin/groups_helper.rb +4 -0
  39. data/app/helpers/mongoid_forums/admin/users_helper.rb +4 -0
  40. data/app/helpers/mongoid_forums/application_helper.rb +20 -0
  41. data/app/helpers/mongoid_forums/formatting_helper.rb +28 -0
  42. data/app/helpers/mongoid_forums/forums_helper.rb +15 -0
  43. data/app/helpers/mongoid_forums/posts_helper.rb +4 -0
  44. data/app/helpers/mongoid_forums/topics_helper.rb +4 -0
  45. data/app/models/mongoid_forums/ability.rb +65 -0
  46. data/app/models/mongoid_forums/alert.rb +80 -0
  47. data/app/models/mongoid_forums/category.rb +31 -0
  48. data/app/models/mongoid_forums/concerns/subscribable.rb +76 -0
  49. data/app/models/mongoid_forums/concerns/viewable.rb +60 -0
  50. data/app/models/mongoid_forums/forum.rb +67 -0
  51. data/app/models/mongoid_forums/group.rb +15 -0
  52. data/app/models/mongoid_forums/post.rb +40 -0
  53. data/app/models/mongoid_forums/subscription.rb +65 -0
  54. data/app/models/mongoid_forums/topic.rb +55 -0
  55. data/app/models/mongoid_forums/view.rb +30 -0
  56. data/app/views/layouts/mongoid_forums/application.haml +21 -0
  57. data/app/views/mongoid_forums/admin/base/index.haml +6 -0
  58. data/app/views/mongoid_forums/admin/categories/edit.haml +4 -0
  59. data/app/views/mongoid_forums/admin/categories/index.haml +21 -0
  60. data/app/views/mongoid_forums/admin/categories/new.haml +4 -0
  61. data/app/views/mongoid_forums/admin/categories/show.haml +21 -0
  62. data/app/views/mongoid_forums/admin/forums/edit.haml +5 -0
  63. data/app/views/mongoid_forums/admin/forums/index.haml +25 -0
  64. data/app/views/mongoid_forums/admin/forums/new.haml +5 -0
  65. data/app/views/mongoid_forums/admin/forums/show.haml +21 -0
  66. data/app/views/mongoid_forums/admin/groups/edit.haml +4 -0
  67. data/app/views/mongoid_forums/admin/groups/index.haml +23 -0
  68. data/app/views/mongoid_forums/admin/groups/new.haml +5 -0
  69. data/app/views/mongoid_forums/admin/groups/show.haml +26 -0
  70. data/app/views/mongoid_forums/admin/users/index.haml +24 -0
  71. data/app/views/mongoid_forums/forums/index.haml +46 -0
  72. data/app/views/mongoid_forums/forums/new.haml +15 -0
  73. data/app/views/mongoid_forums/forums/show.haml +47 -0
  74. data/app/views/mongoid_forums/posts/_form.haml +4 -0
  75. data/app/views/mongoid_forums/posts/_post.haml +24 -0
  76. data/app/views/mongoid_forums/posts/_quoted.haml +9 -0
  77. data/app/views/mongoid_forums/posts/edit.haml +17 -0
  78. data/app/views/mongoid_forums/posts/new.haml +17 -0
  79. data/app/views/mongoid_forums/topics/_topic.haml +2 -0
  80. data/app/views/mongoid_forums/topics/my_posts.haml +6 -0
  81. data/app/views/mongoid_forums/topics/my_subscriptions.haml +5 -0
  82. data/app/views/mongoid_forums/topics/my_topics.haml +6 -0
  83. data/app/views/mongoid_forums/topics/show.haml +16 -0
  84. data/config/locales/en.yml +196 -0
  85. data/config/routes.rb +62 -0
  86. data/lib/ack_mongoid_forums.rb +1 -0
  87. data/lib/generators/mongoid_forums/install/templates/initializer.rb +5 -0
  88. data/lib/generators/mongoid_forums/install_generator.rb +90 -0
  89. data/lib/generators/mongoid_forums/views_generator.rb +27 -0
  90. data/lib/mongoid_forums.rb +53 -0
  91. data/lib/mongoid_forums/default_permissions.rb +63 -0
  92. data/lib/mongoid_forums/engine.rb +20 -0
  93. data/lib/mongoid_forums/sanitizer.rb +10 -0
  94. data/lib/mongoid_forums/version.rb +3 -0
  95. data/lib/tasks/mongoid_forums_tasks.rake +4 -0
  96. data/test/controllers/mongoid_forums/admin/base_controller_test.rb +11 -0
  97. data/test/controllers/mongoid_forums/admin/categories_controller_test.rb +31 -0
  98. data/test/controllers/mongoid_forums/admin/forums_controller_test.rb +31 -0
  99. data/test/controllers/mongoid_forums/admin/groups_controller_test.rb +41 -0
  100. data/test/controllers/mongoid_forums/admin/users_controller_test.rb +11 -0
  101. data/test/controllers/mongoid_forums/forums_controller_test.rb +16 -0
  102. data/test/controllers/mongoid_forums/posts_controller_test.rb +36 -0
  103. data/test/controllers/mongoid_forums/topics_controller_test.rb +36 -0
  104. data/test/dummy/README.rdoc +28 -0
  105. data/test/dummy/Rakefile +6 -0
  106. data/test/dummy/app/assets/javascripts/application.js +15 -0
  107. data/test/dummy/app/assets/javascripts/welcome.js +2 -0
  108. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  109. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  110. data/test/dummy/app/assets/stylesheets/welcome.css +4 -0
  111. data/test/dummy/app/controllers/application_controller.rb +11 -0
  112. data/test/dummy/app/controllers/welcome_controller.rb +4 -0
  113. data/test/dummy/app/helpers/application_helper.rb +2 -0
  114. data/test/dummy/app/helpers/welcome_helper.rb +2 -0
  115. data/test/dummy/app/models/concerns/zero_oid_fix.rb +10 -0
  116. data/test/dummy/app/models/user.rb +43 -0
  117. data/test/dummy/app/views/layouts/application.haml +13 -0
  118. data/test/dummy/app/views/partials/_nav.haml +6 -0
  119. data/test/dummy/app/views/welcome/index.html.erb +2 -0
  120. data/test/dummy/bin/bundle +3 -0
  121. data/test/dummy/bin/rails +4 -0
  122. data/test/dummy/bin/rake +4 -0
  123. data/test/dummy/bin/setup +29 -0
  124. data/test/dummy/config.ru +4 -0
  125. data/test/dummy/config/application.rb +29 -0
  126. data/test/dummy/config/boot.rb +5 -0
  127. data/test/dummy/config/environment.rb +5 -0
  128. data/test/dummy/config/environments/development.rb +41 -0
  129. data/test/dummy/config/environments/production.rb +76 -0
  130. data/test/dummy/config/environments/test.rb +42 -0
  131. data/test/dummy/config/initializers/assets.rb +11 -0
  132. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  133. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  134. data/test/dummy/config/initializers/devise.rb +259 -0
  135. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  136. data/test/dummy/config/initializers/inflections.rb +16 -0
  137. data/test/dummy/config/initializers/kaminari_config.rb +10 -0
  138. data/test/dummy/config/initializers/mime_types.rb +4 -0
  139. data/test/dummy/config/initializers/mongoid_forums.rb +5 -0
  140. data/test/dummy/config/initializers/session_store.rb +3 -0
  141. data/test/dummy/config/initializers/simple_form.rb +166 -0
  142. data/test/dummy/config/initializers/wrap_parameters.rb +9 -0
  143. data/test/dummy/config/locales/devise.en.yml +60 -0
  144. data/test/dummy/config/locales/en.yml +23 -0
  145. data/test/dummy/config/locales/simple_form.en.yml +31 -0
  146. data/test/dummy/config/mongoid.yml +69 -0
  147. data/test/dummy/config/routes.rb +7 -0
  148. data/test/dummy/config/secrets.yml +22 -0
  149. data/test/dummy/lib/templates/erb/scaffold/_form.html.erb +13 -0
  150. data/test/dummy/public/404.html +67 -0
  151. data/test/dummy/public/422.html +67 -0
  152. data/test/dummy/public/500.html +66 -0
  153. data/test/dummy/public/favicon.ico +0 -0
  154. data/test/dummy/test/controllers/welcome_controller_test.rb +9 -0
  155. data/test/dummy/test/fixtures/users.yml +11 -0
  156. data/test/dummy/test/models/user_test.rb +7 -0
  157. data/test/fixtures/mongoid_forums/categories.yml +11 -0
  158. data/test/fixtures/mongoid_forums/forums.yml +11 -0
  159. data/test/fixtures/mongoid_forums/posts.yml +11 -0
  160. data/test/fixtures/mongoid_forums/ranks.yml +11 -0
  161. data/test/fixtures/mongoid_forums/topics.yml +11 -0
  162. data/test/integration/navigation_test.rb +9 -0
  163. data/test/models/mongoid_forums/category_test.rb +9 -0
  164. data/test/models/mongoid_forums/forum_test.rb +9 -0
  165. data/test/models/mongoid_forums/post_test.rb +9 -0
  166. data/test/models/mongoid_forums/rank_test.rb +9 -0
  167. data/test/models/mongoid_forums/topic_test.rb +9 -0
  168. data/test/mongoid_forums_test.rb +7 -0
  169. data/test/test_helper.rb +17 -0
  170. metadata +442 -0
@@ -0,0 +1 @@
1
+ MongoidForums.decorate_user_class! if MongoidForums.user_class
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module Admin::BaseHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module Admin::CategoriesHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module Admin::ForumsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module Admin::GroupsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module Admin::UsersHelper
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module MongoidForums
2
+ module ApplicationHelper
3
+ include FormattingHelper
4
+ # processes text with installed markup formatter
5
+ def mongoid_forums_format(text, *options)
6
+ as_formatted_html(text)
7
+ end
8
+
9
+ def mongoid_forums_quote(text)
10
+ as_quoted_text(text)
11
+ end
12
+
13
+ def mongoid_forums_markdown(text, *options)
14
+ #TODO: delete deprecated method
15
+ Rails.logger.warn("DEPRECATION: mongoid_forums_markdown is replaced by mongoid_forums_format(), and will be removed")
16
+ forem_format(text)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ module MongoidForums
2
+ module FormattingHelper
3
+ # override with desired markup formatter, e.g. textile or markdown
4
+ def as_formatted_html(text)
5
+ if MongoidForums.formatter
6
+ MongoidForums.formatter.format(as_sanitized_text(text))
7
+ else
8
+ MongoidForums::Sanitizer.sanitize(text).html_safe
9
+ end
10
+ end
11
+
12
+ def as_quoted_text(text)
13
+ if MongoidForums.formatter && MongoidForums.formatter.respond_to?(:blockquote)
14
+ MongoidForums.formatter.blockquote(as_sanitized_text(text)).html_safe
15
+ else
16
+ "<blockquote>#{(h(text))}</blockquote>\n\n".html_safe
17
+ end
18
+ end
19
+
20
+ def as_sanitized_text(text)
21
+ if MongoidForums.formatter.respond_to?(:sanitize)
22
+ MongoidForums.formatter.sanitize(text)
23
+ else
24
+ MongoidForums::Sanitizer.sanitize(text)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ module MongoidForums
2
+ module ForumsHelper
3
+ def topics_count(forum)
4
+ forum.topics.count
5
+ end
6
+
7
+ def posts_count(forum)
8
+ if forum.posts_count == nil
9
+ forum.posts_count = forum.topics.inject(0) {|sum, topic| topic.posts.count + sum }
10
+ forum.save
11
+ end
12
+ forum.posts_count
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module PostsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MongoidForums
2
+ module TopicsHelper
3
+ end
4
+ end
@@ -0,0 +1,65 @@
1
+ module MongoidForums
2
+ class Ability
3
+ include CanCan::Ability
4
+ class_attribute :abilities
5
+ self.abilities = Set.new
6
+
7
+ # Allows us to go beyond the standard cancan initialize method which makes it difficult for engines to
8
+ # modify the default +Ability+ of an application. The +ability+ argument must be a class that includes
9
+ # the +CanCan::Ability+ module. The registered ability should behave properly as a stand-alone class
10
+ # and therefore should be easy to test in isolation.
11
+ def self.register_ability(ability)
12
+ self.abilities.add(ability)
13
+ end
14
+
15
+ def self.remove_ability(ability)
16
+ self.abilities.delete(ability)
17
+ end
18
+
19
+ def initialize(user)
20
+ user ||= MongoidForums.user_class.new
21
+
22
+ can :read, MongoidForums::Category do |category|
23
+ user.can_read_mongoid_forums_category?(category)
24
+ end
25
+
26
+ can :read, MongoidForums::Topic do |topic|
27
+ user.can_read_mongoid_forums_forum?(topic.forum) && user.can_read_mongoid_forums_topic?(topic)
28
+ end
29
+
30
+ if user.can_read_mongoid_forums_forums?
31
+ can :read, MongoidForums::Forum do |forum|
32
+ user.can_read_mongoid_forums_category?(forum.category) && user.can_read_mongoid_forums_forum?(forum)
33
+ end
34
+ end
35
+
36
+ can :create_topic, MongoidForums::Forum do |forum|
37
+ can?(:read, forum) && user.can_create_mongoid_forums_topics?(forum)
38
+ end
39
+
40
+ can :reply, MongoidForums::Topic do |topic|
41
+ can?(:read, topic.forum) && user.can_reply_to_mongoid_forums_topic?(topic)
42
+ end
43
+
44
+ can :edit_post, MongoidForums::Forum do |forum|
45
+ user.can_edit_mongoid_forums_posts?(forum)
46
+ end
47
+
48
+ can :destroy_post, MongoidForums::Forum do |forum|
49
+ user.can_destroy_mongoid_forums_posts?(forum)
50
+ end
51
+
52
+ can :moderate, MongoidForums::Forum do |forum|
53
+ user.can_moderate_mongoid_forums_forum?(forum) || user.mongoid_forums_admin?
54
+ end
55
+
56
+ #include any abilities registered by extensions, etc.
57
+ Ability.abilities.each do |clazz|
58
+ ability = clazz.send(:new, user)
59
+ @rules = rules + ability.send(:rules)
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,80 @@
1
+ =begin
2
+
3
+ Copyright 2011 Ryan Bigg, Philip Arndt and Josh Adams
4
+
5
+ This code was obtained from: https://github.com/kultus/forem-2/blob/master/app/models/forem/alert.rb
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+
27
+ =end
28
+
29
+
30
+ module MongoidForums
31
+ class Alert
32
+ include Mongoid::Document
33
+ include Mongoid::Timestamps
34
+ include ActionView::Helpers::DateHelper
35
+
36
+ field :read, :type => Boolean, :default => false
37
+ field :read_at, :type => DateTime
38
+
39
+ # MongoidForums::Topic
40
+ field :mongoid_forums_topic_replier
41
+ field :mongoid_forums_topic_count, :default => 0
42
+ belongs_to :mongoid_forums_topic_post, :class_name => "MongoidForums::Post"
43
+
44
+ # Relations
45
+ belongs_to :subscription, :class_name => "MongoidForums::Subscription"
46
+ belongs_to :user, :index => true, :class_name => MongoidForums.user_class.to_s
47
+
48
+ def link
49
+ str = ""
50
+ case self.subscription.subscribable_type
51
+ when "MongoidForums::Topic"
52
+ str += "/forums/"
53
+ if self.mongoid_forums_topic_post == nil
54
+ str += "topics/" + self.subscription.subscribable.id.to_s
55
+ else
56
+ str += "posts/" + self.mongoid_forums_topic_post_id.to_s
57
+ end
58
+ else
59
+ str
60
+ end
61
+ str
62
+ end
63
+
64
+ def text
65
+ str = ""
66
+ case self.subscription.subscribable_type
67
+ when "MongoidForums::Topic"
68
+ str += self.mongoid_forums_topic_replier
69
+ if self.mongoid_forums_topic_count > 0
70
+ str += " and " + self.mongoid_forums_topic_count.to_s + " other" + (self.mongoid_forums_topic_count > 1 ? "s" : "")
71
+ end
72
+ str += " replied to " + self.subscription.subscribable.name
73
+ else
74
+ str
75
+ end
76
+ return str
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,31 @@
1
+ module MongoidForums
2
+ class Category
3
+ include Mongoid::Document
4
+
5
+ has_many :forums, :class_name => "MongoidForums::Forum", dependent: :destroy
6
+ has_and_belongs_to_many :moderator_groups, :class_name => "MongoidForums::Group", inverse_of: nil
7
+
8
+
9
+ field :name
10
+ validates :name, :presence => true
11
+
12
+ field :position, :type => Integer, :default => 0
13
+ validates :position, numericality: { only_integer: true }
14
+
15
+ def moderator?(user)
16
+ return false unless user
17
+ moderator_groups.each do |group|
18
+ return true if group.moderator && group.members.include?(user.id)
19
+ end
20
+ false
21
+ end
22
+
23
+ def moderators
24
+ array = Array.new
25
+ self.moderator_groups.each do |g|
26
+ array << g.group.members
27
+ end
28
+ return array
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,76 @@
1
+ =begin
2
+
3
+ Copyright 2011 Ryan Bigg, Philip Arndt and Josh Adams
4
+
5
+ This code was obtained from: https://github.com/kultus/forem-2/blob/master/app/models/forem/concerns/subscribable.rb
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+
27
+ =end
28
+
29
+ require 'active_support/concern'
30
+
31
+ module MongoidForums
32
+ module Concerns
33
+ module Subscribable
34
+ extend ActiveSupport::Concern
35
+
36
+ included do
37
+ has_many :subscriptions, :as => :subscribable, :class_name => "MongoidForums::Subscription"
38
+ end
39
+
40
+ def subscribe_creator
41
+ subscribe_user(self.user_id)
42
+ end
43
+
44
+ def subscribe_user(user_id)
45
+ if user_id && !subscriber?(user_id)
46
+ if sub = subscriptions.where(:subscriber_id => user_id, :unsubscribed => true).first
47
+ sub.unsubscribed = false
48
+ sub.save
49
+ else
50
+ subscriptions.create(:subscriber_id => user_id)
51
+ end
52
+ end
53
+ end
54
+
55
+ def unsubscribe_user(user_id)
56
+ sub = subscriptions.where(:subscriber_id => user_id).first
57
+ sub.unsubscribed = true
58
+ sub.save
59
+ end
60
+
61
+ def subscriber?(user_id)
62
+ subscriptions.where(:subscriber_id => user_id, :unsubscribed => false).count > 0
63
+ end
64
+
65
+ def subscription_for user_id
66
+ subscriptions.first(:conditions => { :subscriber_id=>user_id })
67
+ end
68
+
69
+ def alert_subscribers(*args)
70
+ subscriptions.where(:unsubscribed => false).each do |sub|
71
+ sub.alert_subscriber(args)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,60 @@
1
+ =begin
2
+
3
+ Copyright 2011 Ryan Bigg, Philip Arndt and Josh Adams
4
+
5
+ This code was obtained from: https://github.com/kultus/forem-2/blob/master/app/models/forem/concerns/viewable.rb
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+
27
+ =end
28
+
29
+ require 'active_support/concern'
30
+
31
+ module MongoidForums
32
+ module Concerns
33
+ module Viewable
34
+ extend ActiveSupport::Concern
35
+
36
+ included do
37
+ field :views_count
38
+ has_many :views, :as => :viewable, :class_name => "MongoidForums::View"
39
+ end
40
+
41
+ def view_for(user)
42
+ views.where(:user_id => user.id).first
43
+ end
44
+
45
+ # Track when users last viewed topics
46
+ def register_view_by(user)
47
+ return unless user
48
+
49
+ view = views.find_or_create_by(:user_id => user.id)
50
+ view.user_id = user.id
51
+ view.inc(count: 1)
52
+ inc(views_count: 1)
53
+
54
+ view.past_viewed_at = view.current_viewed_at
55
+ view.current_viewed_at = Time.now
56
+ view.save
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,67 @@
1
+ module MongoidForums
2
+ class Forum
3
+ include Mongoid::Document
4
+ include MongoidForums::Concerns::Viewable
5
+
6
+ belongs_to :category, :class_name => "MongoidForums::Category"
7
+ validates :category, :presence => true
8
+
9
+ has_many :topics, :class_name => "MongoidForums::Topic", dependent: :destroy
10
+
11
+ # Caching
12
+ field :posts_count, :type => Integer
13
+
14
+ field :name
15
+
16
+ has_and_belongs_to_many :moderator_groups, :class_name => "MongoidForums::Group", inverse_of: nil
17
+
18
+ validates :category, :name, :presence => true
19
+ field :position, :type => Integer, :default => 0
20
+ validates :position, numericality: { only_integer: true }
21
+
22
+ def unread_topic_count(user)
23
+ view = View.where(:viewable_id => id, :user_id => user.id).first
24
+ return topics.count unless view.present?
25
+ count = 0
26
+ topics.each do |topics|
27
+ if topics.created_at > view.current_viewed_at
28
+ count+=1
29
+ end
30
+ end
31
+ return count
32
+ end
33
+
34
+ def count_of_posts
35
+ topics.inject(0) {|sum, topic| topic.posts.count + sum }
36
+ end
37
+
38
+ def increment_posts_count
39
+ if self.posts_count == nil
40
+ self.posts_count = 1
41
+ else
42
+ self.posts_count += 1
43
+ end
44
+ self.save
45
+ end
46
+
47
+
48
+ def moderator?(user)
49
+ return false unless user
50
+ return true if category.moderator?(user)
51
+ moderator_groups.each do |group|
52
+ return true if group.moderator && group.members.include?(user.id)
53
+ end
54
+ false
55
+ end
56
+
57
+ def moderators
58
+ array = Array.new
59
+ self.moderator_groups.each do |g|
60
+ array << g.group.members
61
+ end
62
+ return array
63
+ end
64
+
65
+
66
+ end
67
+ end