Skip to content

Commit dc8f343

Browse files
committed
Merge pull request #611 from Jake0oo0/add-sorting
Sort forums and categories by order (Changed to position)
2 parents dee7350 + 3cda8f3 commit dc8f343

File tree

10 files changed

+19
-6
lines changed

10 files changed

+19
-6
lines changed

app/controllers/forem/admin/categories_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def destroy
3535
private
3636

3737
def category_params
38-
params.require(:category).permit(:name)
38+
params.require(:category).permit(:name, :position)
3939
end
4040

4141
def find_category

app/controllers/forem/forums_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ForumsController < Forem::ApplicationController
44
helper 'forem/topics'
55

66
def index
7-
@categories = Forem::Category.all
7+
@categories = Forem::Category.order('position DESC')
88
end
99

1010
def show

app/models/forem/category.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Category < ActiveRecord::Base
77

88
has_many :forums
99
validates :name, :presence => true
10+
validates :position, numericality: { only_integer: true }
1011

1112
def to_s
1213
name

app/models/forem/concerns/.keep

Whitespace-only changes.

app/models/forem/forum.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class Forum < ActiveRecord::Base
1515
has_many :moderator_groups
1616

1717
validates :category, :name, :description, :presence => true
18+
validates :position, numericality: { only_integer: true }
1819

1920
alias_attribute :title, :name
2021

21-
# Fix for #339
22-
default_scope { order(:name) }
22+
default_scope { order(:position) }
2323

2424
def last_post_for(forem_user)
2525
if forem_user && (forem_user.forem_admin? || moderator?(forem_user))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<%= simple_form_for [forem, :admin, @category] do |f| %>
22
<%= f.input :name %>
3+
<%= f.input :position %>
34
<%= f.submit :class => "btn btn-primary" %>
45
<% end %>

app/views/forem/admin/forums/_form.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%= simple_form_for [forem, :admin, @forum] do |f| %>
22
<%= f.association :category, :include_blank => false %>
33
<%= f.input :title %>
4+
<%= f.input :position %>
45
<%= f.input :description, :as => :text %>
56

67
<h3><%= t('forem.admin.forum.moderator_groups') %></h3>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPositionToCategories < ActiveRecord::Migration
2+
def change
3+
add_column :forem_categories, :position, :integer, :default => 0
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPositionToForums < ActiveRecord::Migration
2+
def change
3+
add_column :forem_forums, :position, :integer, :default => 0
4+
end
5+
end

spec/models/forum_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
it "is scoped by default" do
1111
if ActiveRecord::Base.connection.class.to_s =~ /Mysql/
12-
Forem::Forum.all.to_sql.should =~ /ORDER BY `forem_forums`.`name` ASC/
12+
Forem::Forum.all.to_sql.should =~ /ORDER BY `forem_forums`.`position` ASC/
1313
else
14-
Forem::Forum.all.to_sql.should =~ /ORDER BY \"forem_forums\".\"name\" ASC/
14+
Forem::Forum.all.to_sql.should =~ /ORDER BY \"forem_forums\".\"position\" ASC/
1515
end
1616
end
1717

0 commit comments

Comments
 (0)