From f16178623b390eb79a5908225143365d24587808 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 6 Jun 2013 16:18:04 -0700 Subject: [PATCH] add categories to forum topics --- .../stylesheets/specific/forum.css.scss | 6 ++++ app/helpers/forum_topics_helper.rb | 3 ++ app/models/forum_topic.rb | 36 ++++++++++++++++++- app/views/forum_topics/_form.html.erb | 5 +++ app/views/forum_topics/show.html.erb | 2 ++ ...0606224559_add_category_to_forum_topics.rb | 5 +++ db/structure.sql | 7 ++-- test/factories/forum_topic.rb | 1 + test/unit/forum_topic_test.rb | 5 +++ 9 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20130606224559_add_category_to_forum_topics.rb diff --git a/app/assets/stylesheets/specific/forum.css.scss b/app/assets/stylesheets/specific/forum.css.scss index 5b1d67a80..656219336 100644 --- a/app/assets/stylesheets/specific/forum.css.scss +++ b/app/assets/stylesheets/specific/forum.css.scss @@ -30,6 +30,12 @@ div.list-of-forum-posts { } div#c-forum-topics { + p.info { + color: #AAA; + font-style: italic; + font-size: 80%; + } + div.single-forum-post { } diff --git a/app/helpers/forum_topics_helper.rb b/app/helpers/forum_topics_helper.rb index 39d0dbab9..12673c391 100644 --- a/app/helpers/forum_topics_helper.rb +++ b/app/helpers/forum_topics_helper.rb @@ -1,2 +1,5 @@ module ForumTopicsHelper + def forum_topic_category_select(object, field) + select(object, field, ForumTopic.reverse_category_mapping.to_a) + end end diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index f51519d12..e018552a9 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -1,5 +1,11 @@ class ForumTopic < ActiveRecord::Base - attr_accessible :title, :original_post_attributes, :as => [:member, :builder, :gold, :platinum, :contributor, :janitor, :moderator, :admin, :default] + CATEGORIES = { + 0 => "General", + 1 => "Tags", + 2 => "Bugs & Suggestions" + } + + attr_accessible :title, :original_post_attributes, :category_id, :as => [:member, :builder, :gold, :platinum, :contributor, :janitor, :moderator, :admin, :default] attr_accessible :is_sticky, :is_locked, :is_deleted, :as => [:janitor, :admin, :moderator] belongs_to :creator, :class_name => "User" belongs_to :updater, :class_name => "User" @@ -10,8 +16,31 @@ class ForumTopic < ActiveRecord::Base before_validation :initialize_is_deleted, :on => :create validates_presence_of :title, :creator_id validates_associated :original_post + validates_inclusion_of :category_id, :in => CATEGORIES.keys accepts_nested_attributes_for :original_post + module CategoryMethods + extend ActiveSupport::Concern + + module ClassMethods + def categories + CATEGORIES.values + end + + def reverse_category_mapping + @reverse_category_mapping ||= CATEGORIES.invert + end + + def for_category_id(cid) + where(:category_id => cid) + end + end + + def category_name + CATEGORIES[category_id] + end + end + module SearchMethods def title_matches(title) where("text_index @@ plainto_tsquery(E?)", title.to_escaped_for_tsquery_split) @@ -29,6 +58,10 @@ class ForumTopic < ActiveRecord::Base q = q.title_matches(params[:title_matches]) end + if params[:category_id].present? + q = q.for_category_id(params[:category_id]) + end + if params[:title].present? q = q.where("title = ?", params[:title]) end @@ -38,6 +71,7 @@ class ForumTopic < ActiveRecord::Base end extend SearchMethods + include CategoryMethods def editable_by?(user) creator_id == user.id || user.is_janitor? diff --git a/app/views/forum_topics/_form.html.erb b/app/views/forum_topics/_form.html.erb index cf05d22c0..0fe1d1db6 100644 --- a/app/views/forum_topics/_form.html.erb +++ b/app/views/forum_topics/_form.html.erb @@ -2,6 +2,11 @@ <%= simple_form_for(@forum_topic) do |f| %> <%= f.input :title %> +
+ + <%= forum_topic_category_select("forum_topic", "category_id") %> +
+ <%= f.simple_fields_for :original_post do |pf| %> <% if !@forum_topic.new_record? %> <%= hidden_field_tag "forum_topic[original_post_attributes][topic_id]", @forum_topic.id %> diff --git a/app/views/forum_topics/show.html.erb b/app/views/forum_topics/show.html.erb index 77cb0dc1a..2ac465a81 100644 --- a/app/views/forum_topics/show.html.erb +++ b/app/views/forum_topics/show.html.erb @@ -7,6 +7,8 @@ <% end %> +

Posted under <%= link_to @forum_topic.category_name, forum_topics_path(:search => {:category_id => @forum_topic.category_id}) %>

+ <% if @forum_topic.is_locked? %>

This topic has been locked.

diff --git a/db/migrate/20130606224559_add_category_to_forum_topics.rb b/db/migrate/20130606224559_add_category_to_forum_topics.rb new file mode 100644 index 000000000..852c8f7cc --- /dev/null +++ b/db/migrate/20130606224559_add_category_to_forum_topics.rb @@ -0,0 +1,5 @@ +class AddCategoryToForumTopics < ActiveRecord::Migration + def change + add_column :forum_topics, :category_id, :integer, :default => 0, :null => false + end +end diff --git a/db/structure.sql b/db/structure.sql index 7302164fb..ca0827ab0 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1780,7 +1780,8 @@ CREATE TABLE forum_topics ( is_deleted boolean DEFAULT false NOT NULL, text_index tsvector NOT NULL, created_at timestamp without time zone NOT NULL, - updated_at timestamp without time zone NOT NULL + updated_at timestamp without time zone NOT NULL, + category_id integer DEFAULT 0 NOT NULL ); @@ -6415,4 +6416,6 @@ INSERT INTO schema_migrations (version) VALUES ('20130417221643'); INSERT INTO schema_migrations (version) VALUES ('20130424121410'); -INSERT INTO schema_migrations (version) VALUES ('20130506154136'); \ No newline at end of file +INSERT INTO schema_migrations (version) VALUES ('20130506154136'); + +INSERT INTO schema_migrations (version) VALUES ('20130606224559'); \ No newline at end of file diff --git a/test/factories/forum_topic.rb b/test/factories/forum_topic.rb index 89ee0aaca..fbd77765b 100644 --- a/test/factories/forum_topic.rb +++ b/test/factories/forum_topic.rb @@ -3,5 +3,6 @@ FactoryGirl.define do title {Faker::Lorem.words.join(" ")} is_sticky false is_locked false + category_id 0 end end diff --git a/test/unit/forum_topic_test.rb b/test/unit/forum_topic_test.rb index ceea59e9a..4c1826bbe 100644 --- a/test/unit/forum_topic_test.rb +++ b/test/unit/forum_topic_test.rb @@ -27,6 +27,11 @@ class ForumTopicTest < ActiveSupport::TestCase assert_equal(0, ForumTopic.title_matches("aaa").count) end + should "be searchable by category id" do + assert_equal(1, ForumTopic.search(:category_id => 0).count) + assert_equal(0, ForumTopic.search(:category_id => 1).count) + end + should "initialize its creator" do assert_equal(@user.id, @topic.creator_id) end