my_forum 0.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +16 -0
  4. data/Rakefile +35 -0
  5. data/app/assets/javascripts/my_forum/admin/categories.js.coffee +0 -0
  6. data/app/assets/javascripts/my_forum/admin/dashboard.js.coffee +0 -0
  7. data/app/assets/javascripts/my_forum/admin/forums.js.coffee +0 -0
  8. data/app/assets/javascripts/my_forum/admin/roles.js +2 -0
  9. data/app/assets/javascripts/my_forum/admin/users.js +2 -0
  10. data/app/assets/javascripts/my_forum/application.js +16 -0
  11. data/app/assets/javascripts/my_forum/forums.js.coffee +0 -0
  12. data/app/assets/javascripts/my_forum/posts.js.coffee +0 -0
  13. data/app/assets/javascripts/my_forum/topics.js.coffee +0 -0
  14. data/app/assets/javascripts/my_forum/users.js.coffee +0 -0
  15. data/app/assets/javascripts/my_forum/welcome.js.coffee +0 -0
  16. data/app/assets/stylesheets/my_forum/admin/categories.css.scss +0 -0
  17. data/app/assets/stylesheets/my_forum/admin/dashboard.css.scss +3 -0
  18. data/app/assets/stylesheets/my_forum/admin/forums.css.scss +7 -0
  19. data/app/assets/stylesheets/my_forum/admin/roles.css +4 -0
  20. data/app/assets/stylesheets/my_forum/admin/users.css +4 -0
  21. data/app/assets/stylesheets/my_forum/application.css.scss +29 -0
  22. data/app/assets/stylesheets/my_forum/forums.css.scss +31 -0
  23. data/app/assets/stylesheets/my_forum/posts.css.scss +76 -0
  24. data/app/assets/stylesheets/my_forum/private_messages.css.scss +7 -0
  25. data/app/assets/stylesheets/my_forum/topics.css.scss +12 -0
  26. data/app/assets/stylesheets/my_forum/users.css.scss +0 -0
  27. data/app/assets/stylesheets/my_forum/welcome.css.scss +66 -0
  28. data/app/controllers/my_forum/admin/categories_controller.rb +36 -0
  29. data/app/controllers/my_forum/admin/dashboard_controller.rb +13 -0
  30. data/app/controllers/my_forum/admin/forums_controller.rb +35 -0
  31. data/app/controllers/my_forum/admin/roles_controller.rb +38 -0
  32. data/app/controllers/my_forum/admin/users_controller.rb +14 -0
  33. data/app/controllers/my_forum/application_controller.rb +56 -0
  34. data/app/controllers/my_forum/forums_controller.rb +39 -0
  35. data/app/controllers/my_forum/posts_controller.rb +29 -0
  36. data/app/controllers/my_forum/private_messages_controller.rb +67 -0
  37. data/app/controllers/my_forum/topics_controller.rb +47 -0
  38. data/app/controllers/my_forum/users_controller.rb +89 -0
  39. data/app/controllers/my_forum/welcome_controller.rb +12 -0
  40. data/app/helpers/my_forum/admin/dashboard_helper.rb +4 -0
  41. data/app/helpers/my_forum/admin/forums_helper.rb +4 -0
  42. data/app/helpers/my_forum/admin/roles_helper.rb +4 -0
  43. data/app/helpers/my_forum/admin/users_helper.rb +4 -0
  44. data/app/helpers/my_forum/application_helper.rb +39 -0
  45. data/app/helpers/my_forum/forums_helper.rb +42 -0
  46. data/app/helpers/my_forum/posts_helper.rb +29 -0
  47. data/app/helpers/my_forum/private_messages_helper.rb +18 -0
  48. data/app/helpers/my_forum/topics_helper.rb +11 -0
  49. data/app/helpers/my_forum/users_helper.rb +23 -0
  50. data/app/helpers/my_forum/welcome_helper.rb +4 -0
  51. data/app/mailers/application_mailer.rb +4 -0
  52. data/app/mailers/my_forum/user_mailer.rb +14 -0
  53. data/app/models/my_forum/category.rb +7 -0
  54. data/app/models/my_forum/category_permission.rb +6 -0
  55. data/app/models/my_forum/forum.rb +56 -0
  56. data/app/models/my_forum/log_read_mark.rb +4 -0
  57. data/app/models/my_forum/post.rb +14 -0
  58. data/app/models/my_forum/private_message.rb +8 -0
  59. data/app/models/my_forum/role.rb +6 -0
  60. data/app/models/my_forum/topic.rb +33 -0
  61. data/app/models/my_forum/user.rb +45 -0
  62. data/app/models/my_forum/user_group.rb +13 -0
  63. data/app/models/my_forum/user_group_link.rb +6 -0
  64. data/app/models/my_forum/user_roles.rb +6 -0
  65. data/app/views/layouts/mailer.html.erb +5 -0
  66. data/app/views/layouts/mailer.text.erb +1 -0
  67. data/app/views/layouts/my_forum/_guest_navbar.html.haml +4 -0
  68. data/app/views/layouts/my_forum/_user_navbar.html.haml +18 -0
  69. data/app/views/layouts/my_forum/admin_application.haml +27 -0
  70. data/app/views/layouts/my_forum/application.haml +21 -0
  71. data/app/views/my_forum/admin/categories/edit.haml +9 -0
  72. data/app/views/my_forum/admin/categories/new.haml +8 -0
  73. data/app/views/my_forum/admin/dashboard/index.haml +0 -0
  74. data/app/views/my_forum/admin/forums/index.haml +37 -0
  75. data/app/views/my_forum/admin/forums/new.haml +6 -0
  76. data/app/views/my_forum/admin/roles/index.haml +12 -0
  77. data/app/views/my_forum/admin/roles/new.haml +22 -0
  78. data/app/views/my_forum/admin/users/index.haml +10 -0
  79. data/app/views/my_forum/forums/_topic_subject.html.haml +15 -0
  80. data/app/views/my_forum/forums/show.haml +28 -0
  81. data/app/views/my_forum/private_messages/_sidebar.haml +10 -0
  82. data/app/views/my_forum/private_messages/inbox.haml +24 -0
  83. data/app/views/my_forum/private_messages/new.haml +16 -0
  84. data/app/views/my_forum/private_messages/show.haml +10 -0
  85. data/app/views/my_forum/topics/_post.haml +12 -0
  86. data/app/views/my_forum/topics/_quick_answer.haml +8 -0
  87. data/app/views/my_forum/topics/_topic_header.haml +4 -0
  88. data/app/views/my_forum/topics/_user_info.haml +9 -0
  89. data/app/views/my_forum/topics/new.haml +12 -0
  90. data/app/views/my_forum/topics/show.haml +21 -0
  91. data/app/views/my_forum/user_mailer/reset_password_email.haml +1 -0
  92. data/app/views/my_forum/user_mailer/reset_password_email.text.erb +1 -0
  93. data/app/views/my_forum/users/edit.haml +12 -0
  94. data/app/views/my_forum/users/forgot_password.haml +10 -0
  95. data/app/views/my_forum/users/new.haml +14 -0
  96. data/app/views/my_forum/users/signin.haml +14 -0
  97. data/app/views/my_forum/users/signout.haml +0 -0
  98. data/app/views/my_forum/welcome/index.haml +21 -0
  99. data/config/locales/en.yml +65 -0
  100. data/config/locales/ru.yml +121 -0
  101. data/config/routes.rb +31 -0
  102. data/db/migrate/20141117122725_create_my_forum_forums.rb +12 -0
  103. data/db/migrate/20141117122742_create_my_forum_topics.rb +17 -0
  104. data/db/migrate/20141117122751_create_my_forum_posts.rb +11 -0
  105. data/db/migrate/20141118081021_create_my_forum_categories.rb +8 -0
  106. data/db/migrate/20141118131215_create_my_forum_users.rb +25 -0
  107. data/db/migrate/20141222094522_create_my_forum_roles.rb +10 -0
  108. data/db/migrate/20141222094538_create_my_forum_user_roles.rb +9 -0
  109. data/db/migrate/20150202115250_create_my_forum_log_read_marks.rb +10 -0
  110. data/db/migrate/20150215200453_create_my_forum_user_groups.rb +15 -0
  111. data/db/migrate/20150215204852_create_my_forum_user_group_links.rb +9 -0
  112. data/db/migrate/20150215212443_create_my_forum_category_permissions.rb +9 -0
  113. data/db/migrate/20150227210814_create_my_forum_private_messages.rb +14 -0
  114. data/lib/my_forum/engine.rb +31 -0
  115. data/lib/my_forum/version.rb +3 -0
  116. data/lib/my_forum.rb +4 -0
  117. data/lib/tasks/my_forum_tasks.rake +4 -0
  118. data/spec/controllers/my_forum/private_messages_controller_spec.rb +7 -0
  119. data/spec/controllers/my_forum/users_controller_spec.rb +76 -0
  120. data/spec/dummy/README.rdoc +28 -0
  121. data/spec/dummy/Rakefile +6 -0
  122. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  123. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  124. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  125. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  126. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  127. data/spec/dummy/bin/bundle +3 -0
  128. data/spec/dummy/bin/rails +4 -0
  129. data/spec/dummy/bin/rake +4 -0
  130. data/spec/dummy/config/application.rb +23 -0
  131. data/spec/dummy/config/boot.rb +5 -0
  132. data/spec/dummy/config/database.yml +25 -0
  133. data/spec/dummy/config/environment.rb +5 -0
  134. data/spec/dummy/config/environments/development.rb +37 -0
  135. data/spec/dummy/config/environments/production.rb +78 -0
  136. data/spec/dummy/config/environments/test.rb +39 -0
  137. data/spec/dummy/config/initializers/assets.rb +8 -0
  138. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  139. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  140. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  141. data/spec/dummy/config/initializers/inflections.rb +16 -0
  142. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  143. data/spec/dummy/config/initializers/session_store.rb +3 -0
  144. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  145. data/spec/dummy/config/locales/en.yml +23 -0
  146. data/spec/dummy/config/routes.rb +4 -0
  147. data/spec/dummy/config/secrets.yml +22 -0
  148. data/spec/dummy/config.ru +4 -0
  149. data/spec/dummy/db/development.sqlite3 +0 -0
  150. data/spec/dummy/db/schema.rb +128 -0
  151. data/spec/dummy/db/test.sqlite3 +0 -0
  152. data/spec/dummy/log/development.log +97 -0
  153. data/spec/dummy/log/test.log +4926 -0
  154. data/spec/dummy/public/404.html +67 -0
  155. data/spec/dummy/public/422.html +67 -0
  156. data/spec/dummy/public/500.html +66 -0
  157. data/spec/dummy/public/favicon.ico +0 -0
  158. data/spec/helpers/my_forum/private_messages_helper_spec.rb +17 -0
  159. data/spec/models/my_forum/private_message_spec.rb +7 -0
  160. data/spec/models/my_forum/user_spec.rb +13 -0
  161. data/spec/rails_helper.rb +52 -0
  162. data/spec/spec_helper.rb +87 -0
  163. metadata +348 -0
@@ -0,0 +1,67 @@
1
+ require_dependency "my_forum/application_controller"
2
+
3
+ module MyForum
4
+ class PrivateMessagesController < ApplicationController
5
+
6
+ before_filter :only_for_registered_users
7
+
8
+ def index
9
+ @private_messages = PrivateMessage.inbox_for(current_user)
10
+ render :inbox
11
+ end
12
+
13
+ def inbox
14
+
15
+ end
16
+
17
+ def outbox
18
+
19
+ end
20
+
21
+ def deleted
22
+
23
+ end
24
+
25
+ def new
26
+ @pm = PrivateMessage.new
27
+ end
28
+
29
+ def create
30
+ @pm = PrivateMessage.new
31
+ recipient = begin
32
+ User.find_by_login(params[:private_message].delete(:recipient))
33
+ rescue
34
+ #TODO send notification to admin
35
+ nil
36
+ end
37
+
38
+ unless recipient
39
+ @pm.errors.add(:base, t('.cant_find_recipient'))
40
+ render :new
41
+ return
42
+ end
43
+
44
+ @pm.recipient_id = recipient.id
45
+ @pm.sender_id = current_user_id
46
+ @pm.assign_attributes(pm_params)
47
+ @pm.save
48
+
49
+ redirect_to action: :index
50
+ end
51
+
52
+ def show
53
+ @pm = PrivateMessage.inbox_for(current_user).find(params[:id])
54
+ @pm.update!(unread: false)
55
+ end
56
+
57
+ private
58
+
59
+ def pm_params
60
+ params.require(:private_message).permit(:subject, :body)
61
+ end
62
+
63
+ def only_for_registered_users
64
+ redirect_to root_path unless current_user
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,47 @@
1
+ require_dependency "my_forum/application_controller"
2
+
3
+ module MyForum
4
+ class TopicsController < ApplicationController
5
+ before_filter :find_forum, only: [:new, :create, :show]
6
+
7
+ def new
8
+ @topic = @forum.topics.build
9
+ end
10
+
11
+ def show
12
+ @topic = Topic.find(params[:id])
13
+ check_access_permissions(@topic)
14
+ @topic_posts = @topic.posts.paginate(per_page: 16, page: params[:page])
15
+ @new_post = Post.new #TODO if quick_answer_enabled
16
+
17
+ @topic.mark_as_read(current_user, @topic_posts.last)
18
+ @topic.increment!(:views) if current_user
19
+ end
20
+
21
+ def create
22
+ raise unless current_user
23
+ #TODO !
24
+ topic = @forum.topics.build(topic_params)
25
+ post = topic.posts.build(post_params)
26
+ post.user = current_user
27
+
28
+ topic.save
29
+ post.save
30
+ redirect_to forum_path(@forum)
31
+ end
32
+
33
+ private
34
+
35
+ def find_forum
36
+ @forum = Forum.find(params[:forum_id])
37
+ end
38
+
39
+ def topic_params
40
+ params.require(:topic).permit(:name, :description)
41
+ end
42
+
43
+ def post_params
44
+ params.require(:post).permit(:text)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,89 @@
1
+ require_dependency "my_forum/application_controller"
2
+
3
+ module MyForum
4
+ class UsersController < ApplicationController
5
+
6
+ before_filter :authorize_user, only: [:edit, :update, :forgot_password]
7
+
8
+ def new
9
+ @user = User.new
10
+ end
11
+
12
+ def edit
13
+ end
14
+
15
+ def update
16
+ update_password
17
+ current_user.update_columns(user_update_params)
18
+ redirect_to edit_user_path(current_user)
19
+ end
20
+
21
+ def create
22
+ @user = User.new(user_params)
23
+ if @user.valid?
24
+ @user.user_groups << UserGroup::MEMBER_GROUP
25
+ @user.save
26
+ session[:user_id] = @user.id
27
+ redirect_to root_path
28
+ else
29
+ render :new
30
+ end
31
+ end
32
+
33
+ def signin
34
+ if request.post?
35
+ if params[:user].blank?
36
+ render :layout => 'signin'
37
+ return
38
+ end
39
+
40
+ user = User.find_by_login(params[:user][:login])
41
+ if user && user.valid_password?(params[:user][:password])
42
+ session[:user_id] = user.id
43
+ redirect_to root_path
44
+ return
45
+ else
46
+ flash[:error] = t('.invalid_login_or_password')
47
+ return
48
+ end
49
+ end
50
+ end
51
+
52
+ def forgot_password
53
+ if request.post?
54
+ return unless user = User.find_by_email(params[:user][:email])
55
+ UserMailer.reset_password_email(user).deliver_now
56
+ redirect_to root_path
57
+ end
58
+ end
59
+
60
+ def logout
61
+ session[:user_id] = nil
62
+ redirect_to admin_root_path
63
+ end
64
+
65
+ def authorize_user
66
+ redirect_to root_path unless current_user
67
+ end
68
+
69
+ private
70
+
71
+ def user_params
72
+ params.require(:user).permit(:login, :email, :password)
73
+ end
74
+
75
+ def user_update_params
76
+ params.require(:user).permit(:email)
77
+ end
78
+
79
+ def update_password
80
+ current_password = user_params.delete(:password)
81
+ new_password = params[:user].delete(:new_password)
82
+
83
+ return unless new_password
84
+ return unless current_user.valid_password?(current_password)
85
+ current_user.update_attributes(password: new_password)
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,12 @@
1
+ require_dependency "my_forum/application_controller"
2
+
3
+ module MyForum
4
+ class WelcomeController < ApplicationController
5
+ def index
6
+ return @forum_categories = Category.includes(:forums) if current_user && current_user.is_admin?
7
+
8
+ @forum_categories = Category.includes(:forums, :user_groups).
9
+ reject{|category| (category.user_groups.map(&:name) & (current_user_groups)).blank? }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module MyForum
2
+ module Admin::DashboardHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MyForum
2
+ module Admin::ForumsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MyForum
2
+ module Admin::RolesHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MyForum
2
+ module Admin::UsersHelper
3
+ end
4
+ end
@@ -0,0 +1,39 @@
1
+ module MyForum
2
+ module ApplicationHelper
3
+
4
+ def draw_navigation
5
+ html_li = content_tag :li, link_to('Forums', root_path)
6
+
7
+ if @forum
8
+ html_li += content_tag :li, '&rarr;'.html_safe
9
+ html_li += content_tag :li, link_to(@forum.name, forum_path(@forum))
10
+ end
11
+
12
+ if @forum && @topic && !@topic.new_record?
13
+ html_li += content_tag :li, '&rarr;'.html_safe
14
+ html_li += content_tag :li, link_to(@topic.name.html_safe, forum_topic_path(@forum, @topic))
15
+ end
16
+
17
+ html = content_tag :ul, class: 'draw-navigation' do
18
+ html_li
19
+ end
20
+
21
+ html.html_safe
22
+ end
23
+
24
+ def online_users
25
+ User.online.pluck(:login).join(', ')
26
+ end
27
+
28
+ def today_was_users
29
+ User.today_visited.pluck(:login).join(', ')
30
+ end
31
+
32
+ def errors_for(obj)
33
+ return if obj.errors.blank?
34
+
35
+ content_tag :div, obj.errors.full_messages.to_sentence, class: 'errors_for'
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ module MyForum
2
+ module ForumsHelper
3
+
4
+ def forum_status_img(forum)
5
+ unread = 'lada_logo_unread.jpg'
6
+ read = 'lada_logo.jpg'
7
+
8
+ display_as = unread
9
+ display_as = read unless current_user && forum.has_unread_posts?(current_user)
10
+
11
+ image_tag(display_as, width: '66px')
12
+ end
13
+
14
+ def forum_name(forum)
15
+ html = content_tag :strong, link_to(forum.name, forum_path(forum))
16
+ html += content_tag :div, forum.description
17
+ html.html_safe
18
+ end
19
+
20
+ def forum_stat(forum)
21
+ html = content_tag(:div, t('.topics_count', topics_count: forum.topics_count))
22
+ html += content_tag(:div, t('.messages_count', messages_count: forum.topics_count))
23
+ html.html_safe
24
+ end
25
+
26
+ def forum_last_message_info(forum)
27
+ info = forum.latest_topic_info
28
+
29
+ html = content_tag(:div, info ? (t('.last_answer_from') + info.user_login) : '-' )
30
+ html += content_tag(:div, info ? (t('.in_forum') + info.topic_name) : '-' )
31
+ html += content_tag(:div, info ? info.post_created_at : '-' )
32
+ html.html_safe
33
+ end
34
+
35
+ def new_topic_button
36
+ return unless @forum
37
+ content_tag :div, class: 'buttons_for_new_topic' do
38
+ link_to t('my_forum.create_new_topic'), new_forum_topic_path(@forum), class: 'btn btn-primary'
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ module MyForum
2
+ module PostsHelper
3
+ def format_post_text(post)
4
+ text = post.text
5
+
6
+ # Images
7
+ text.gsub!(/\[img\]/i, '<img src="')
8
+ text.gsub!(/\[\/img\]/i, '" />')
9
+
10
+ # Bold text
11
+ text.gsub!(/\[b\]/i, '<strong>')
12
+ text.gsub!(/\[\/b\]/i, '</strong>')
13
+
14
+ # Quote
15
+ # text.gsub!(/\[quote.*]/i, '<span class="bbqoute">')
16
+ # text.gsub!(/\[\/quote\]/i, '</span>')
17
+ text.gsub!(/\[quote.*]/i, 'quote:')
18
+ text.gsub!(/\[\/quote\]/i, '')
19
+
20
+ # Link
21
+ #text.scan(/(?<url>\[url=(.*?)\])(?<url_text>.*?)\[\/url\]/) {|m| puts m.first}
22
+ #text.match(/(?<url>\[url=(.*?)\])(?<url_text>.*?)\[\/url\]/)
23
+ #\[url=(.*?)\](.*?)\[\/url\]
24
+ text.gsub!(/\[url=(.*?)\](.*?)\[\/url\]/i) { "<a href='#{$1}'>#{$2}</a>" }
25
+
26
+ text.html_safe
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ module MyForum
2
+ module PrivateMessagesHelper
3
+
4
+ def new_pm_button
5
+ return unless current_user
6
+ content_tag :div, class: 'new_pm_button' do
7
+ link_to t('my_forum.create_new_pm'), new_private_message_path, class: 'btn btn-primary'
8
+ end
9
+ end
10
+
11
+ def new_private_messages_count_bage
12
+ return unless current_user
13
+ return if (count = new_pm_count).eql?(0)
14
+ content_tag(:span, count, class: 'badge')
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module MyForum
2
+ module TopicsHelper
3
+
4
+ def topic_last_post_info(topic)
5
+ html = content_tag(:div, topic.last_post_time)
6
+ html += content_tag(:div, topic.last_post_user_login)
7
+ html.html_safe
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module MyForum
2
+ module UsersHelper
3
+ # Display title name dependent user posts count
4
+ def user_title(user)
5
+ #TODO
6
+ t('.beginner')
7
+ end
8
+
9
+ # Display user avatar
10
+ def user_avatar(user)
11
+ image_tag('blank_avatar.png', class: 'user-avatar')
12
+ end
13
+
14
+ def user_posts_count(user)
15
+ user.posts_count
16
+ end
17
+
18
+ def online_user_marker(user_login)
19
+ return unless User.online.pluck(:login).include?(user_login)
20
+ content_tag :div, '&nbsp;'.html_safe, class: 'label label-success'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module MyForum
2
+ module WelcomeHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: "from@example.com"
3
+ layout 'mailer'
4
+ end
@@ -0,0 +1,14 @@
1
+ module MyForum
2
+ class UserMailer < ApplicationMailer
3
+ def reset_password_email(user)
4
+ @user = user
5
+
6
+ o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
7
+ @new_password = (0...10).map { o[rand(o.length)] }.join
8
+
9
+ @user.password = @new_password
10
+ @user.save
11
+ mail(to: @user.email, subject: 'vaz.od.ua - New password')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module MyForum
2
+ class Category < ActiveRecord::Base
3
+ has_many :forums
4
+ has_many :category_permission
5
+ has_many :user_groups, through: :category_permission
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module MyForum
2
+ class CategoryPermission < ActiveRecord::Base
3
+ belongs_to :category
4
+ belongs_to :user_group
5
+ end
6
+ end
@@ -0,0 +1,56 @@
1
+ module MyForum
2
+ class Forum < ActiveRecord::Base
3
+ belongs_to :category
4
+ has_many :topics
5
+ has_many :posts
6
+
7
+ def latest_topic_info
8
+ Topic.find_by_sql("
9
+ SELECT my_forum_topics.name AS topic_name, my_forum_posts.created_at AS post_created_at, my_forum_users.login AS user_login
10
+ FROM my_forum_posts
11
+ JOIN my_forum_topics ON my_forum_topics.id = my_forum_posts.topic_id
12
+ JOIN my_forum_users ON my_forum_posts.user_id = my_forum_users.id
13
+ WHERE my_forum_topics.forum_id = #{self.id}
14
+ ORDER BY my_forum_posts.id DESC
15
+ LIMIT 1
16
+ ").first
17
+ end
18
+
19
+ def has_unread_posts?(current_user)
20
+ latest_post_ids = self.topics.pluck(:latest_post_id)
21
+ read_log = LogReadMark.where("user_id = ? AND post_id IN (?)", current_user.id, latest_post_ids).count
22
+
23
+ latest_post_ids.length != read_log
24
+ end
25
+
26
+ def topics_with_latest_post_info(page: 0, per_page: 30)
27
+ Topic.paginate_by_sql("
28
+ SELECT my_forum_topics.*, my_forum_users.login AS last_post_user_login, my_forum_posts.created_at AS last_post_time
29
+ FROM my_forum_topics
30
+ JOIN my_forum_posts ON my_forum_posts.id = my_forum_topics.latest_post_id
31
+ JOIN my_forum_users ON my_forum_posts.user_id = my_forum_users.id
32
+ WHERE my_forum_topics.forum_id = #{self.id}
33
+ ORDER BY my_forum_posts.id DESC
34
+ ", page: page, per_page: per_page)
35
+ end
36
+
37
+ def self.unread_topics_with_latest_post_info(user_id:, page: 0, per_page: 30)
38
+ log_topic_ids = LogReadMark.where(user_id: user_id).pluck(:topic_id)
39
+ log_topic_ids = [0] if log_topic_ids.empty?
40
+
41
+ Topic.paginate_by_sql("
42
+ SELECT my_forum_topics.*, my_forum_users.login AS last_post_user_login, my_forum_posts.created_at AS last_post_time
43
+ FROM my_forum_topics
44
+ JOIN my_forum_posts ON my_forum_posts.id = my_forum_topics.latest_post_id
45
+ JOIN my_forum_users ON my_forum_posts.user_id = my_forum_users.id
46
+ WHERE my_forum_topics.id NOT IN (#{log_topic_ids.join(',')})
47
+ ORDER BY my_forum_posts.id DESC
48
+ ", page: page, per_page: per_page)
49
+ end
50
+
51
+ def unread_topics_with_user
52
+ log_topic_ids = LogReadMark.where(user_id: current_user.id).pluck(:topic_id)
53
+ Topic.where('id NOT IN (?)', log_topic_ids).order('updated_at DESC').paginate(:page => params[:page], :per_page => 30)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,4 @@
1
+ module MyForum
2
+ class LogReadMark < ActiveRecord::Base
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module MyForum
2
+ class Post < ActiveRecord::Base
3
+ belongs_to :topic, :counter_cache => true
4
+ belongs_to :user, :counter_cache => true
5
+
6
+ after_create :update_topic_latest_post
7
+
8
+ private
9
+
10
+ def update_topic_latest_post
11
+ self.topic.update_attribute(:latest_post_id, self.id)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module MyForum
2
+ class PrivateMessage < ActiveRecord::Base
3
+ scope :unread_count_for, -> (user) { where(recipient_id: user.id, unread: true).count }
4
+ scope :inbox_for, -> (user) { where(recipient_id: user.id) }
5
+
6
+ attr_accessor :recipient
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module MyForum
2
+ class Role < ActiveRecord::Base
3
+ has_many :user_roles
4
+ has_many :users, through: :user_roles
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ module MyForum
2
+ class Topic < ActiveRecord::Base
3
+ has_many :posts, :counter_cache => true, dependent: :destroy
4
+ belongs_to :forum, :counter_cache => true
5
+ belongs_to :user
6
+
7
+ def info
8
+ author = (post = posts.first).user.login
9
+ created = post.created_at
10
+
11
+ { author: author, created: created }
12
+ end
13
+
14
+ def owner
15
+ posts.first.user
16
+ end
17
+
18
+ def unread?(current_user, last_post)
19
+ return true unless current_user
20
+
21
+ log = LogReadMark.where(user_id: current_user.id, topic_id: self.id, post_id: last_post.id).count
22
+ log >= 1 ? false : true
23
+ end
24
+
25
+ def mark_as_read(current_user, last_topic)
26
+ return true unless current_user
27
+
28
+ log = LogReadMark.find_or_create_by(user_id: current_user.id, topic_id: self.id)
29
+ log.post_id = last_topic.id
30
+ log.save
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ module MyForum
2
+ class User < ActiveRecord::Base
3
+ require 'digest'
4
+
5
+ has_many :posts, class_name: 'MyForum::Post'
6
+ has_many :user_roles
7
+ has_many :roles, through: :user_roles
8
+ has_many :user_group_links
9
+ has_many :user_groups, through: :user_group_links
10
+
11
+ scope :online, -> { where("updated_at > ?", 10.minutes.ago) }
12
+ scope :today_visited, -> { where("updated_at > ?", Time.now.beginning_of_day) }
13
+
14
+ validates_uniqueness_of :login, :email
15
+
16
+ before_save :encrypt_password
17
+
18
+ def valid_password?(submitted_password)
19
+ password == encrypt(submitted_password)
20
+ end
21
+
22
+ def can_quick_answer?(forum)
23
+ true
24
+ end
25
+
26
+ private
27
+
28
+ def encrypt_password
29
+ self.salt = make_salt unless valid_password?(password)
30
+ self.password = encrypt(password)
31
+ end
32
+
33
+ def encrypt(string)
34
+ secure_hash("#{salt}--#{string}")
35
+ end
36
+
37
+ def make_salt
38
+ secure_hash("#{Time.now.utc}--#{password}")
39
+ end
40
+
41
+ def secure_hash(string)
42
+ Digest::SHA2.hexdigest(string)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module MyForum
2
+ class UserGroup < ActiveRecord::Base
3
+ has_many :user_group_links
4
+ has_many :users, through: :user_group_links
5
+
6
+ has_many :category_permissions
7
+ has_many :categories, through: :category_permissions
8
+
9
+ # TODO fix this
10
+ GUEST_GROUP = UserGroup.find_by_name('Guests') #find(1)
11
+ MEMBER_GROUP = UserGroup.find_by_name('Member') #.find(2)
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ module MyForum
2
+ class UserGroupLink < ActiveRecord::Base
3
+ belongs_to :user
4
+ belongs_to :user_group
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module MyForum
2
+ class UserRoles < ActiveRecord::Base
3
+ belongs_to :user
4
+ belongs_to :role
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ <%= yield %>
4
+ </body>
5
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1,4 @@
1
+ .row
2
+ .col-md-12
3
+ = link_to t('.login'), signin_path
4
+ = link_to t('.register'), new_user_path