add categories to forum topics
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
module ForumTopicsHelper
|
||||
def forum_topic_category_select(object, field)
|
||||
select(object, field, ForumTopic.reverse_category_mapping.to_a)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
<%= simple_form_for(@forum_topic) do |f| %>
|
||||
<%= f.input :title %>
|
||||
|
||||
<div class="input">
|
||||
<label for="forum_topic_category_id">Category</label>
|
||||
<%= forum_topic_category_select("forum_topic", "category_id") %>
|
||||
</div>
|
||||
|
||||
<%= 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 %>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
<% end %>
|
||||
</h1>
|
||||
|
||||
<p class="info">Posted under <%= link_to @forum_topic.category_name, forum_topics_path(:search => {:category_id => @forum_topic.category_id}) %></p>
|
||||
|
||||
<% if @forum_topic.is_locked? %>
|
||||
<div class="notice">
|
||||
<p>This topic has been locked.</p>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddCategoryToForumTopics < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :forum_topics, :category_id, :integer, :default => 0, :null => false
|
||||
end
|
||||
end
|
||||
@@ -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');
|
||||
INSERT INTO schema_migrations (version) VALUES ('20130506154136');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20130606224559');
|
||||
@@ -3,5 +3,6 @@ FactoryGirl.define do
|
||||
title {Faker::Lorem.words.join(" ")}
|
||||
is_sticky false
|
||||
is_locked false
|
||||
category_id 0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user