Extract HasOrders + partial from DebatesController
This commit is contained in:
@@ -3,6 +3,7 @@ require "application_responder"
|
|||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
include SimpleCaptcha::ControllerHelpers
|
include SimpleCaptcha::ControllerHelpers
|
||||||
include HasFilters
|
include HasFilters
|
||||||
|
include HasOrders
|
||||||
|
|
||||||
before_action :authenticate_http_basic, if: :http_basic_auth_site?
|
before_action :authenticate_http_basic, if: :http_basic_auth_site?
|
||||||
before_action :authenticate_user!, unless: :devise_controller?, if: :beta_site?
|
before_action :authenticate_user!, unless: :devise_controller?, if: :beta_site?
|
||||||
|
|||||||
12
app/controllers/concerns/has_orders.rb
Normal file
12
app/controllers/concerns/has_orders.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module HasOrders
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def has_orders(valid_orders, *args)
|
||||||
|
before_action(*args) do
|
||||||
|
@valid_orders = valid_orders
|
||||||
|
@current_order = @valid_orders.include?(params[:order]) ? params[:order] : @valid_orders.first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
class DebatesController < ApplicationController
|
class DebatesController < ApplicationController
|
||||||
before_action :parse_order, only: :index
|
|
||||||
before_action :parse_tag_filter, only: :index
|
before_action :parse_tag_filter, only: :index
|
||||||
before_action :parse_search_terms, only: :index
|
before_action :parse_search_terms, only: :index
|
||||||
before_action :authenticate_user!, except: [:index, :show]
|
before_action :authenticate_user!, except: [:index, :show]
|
||||||
|
has_orders %w{confidence_score hot_score created_at most_commented random}, only: :index
|
||||||
|
|
||||||
load_and_authorize_resource
|
load_and_authorize_resource
|
||||||
|
|
||||||
respond_to :html, :js
|
respond_to :html, :js
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@debates = @search_terms.present? ? Debate.search(@search_terms) : Debate.all
|
@debates = @search_terms.present? ? Debate.search(@search_terms) : Debate.all
|
||||||
@debates = @debates.tagged_with(@tag_filter) if @tag_filter
|
@debates = @debates.tagged_with(@tag_filter) if @tag_filter
|
||||||
@debates = @debates.page(params[:page]).for_render.send("sort_by_#{@order}")
|
@debates = @debates.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
||||||
@tag_cloud = Debate.tag_counts.order(taggings_count: :desc, name: :asc).limit(20)
|
@tag_cloud = Debate.tag_counts.order(taggings_count: :desc, name: :asc).limit(20)
|
||||||
set_debate_votes(@debates)
|
set_debate_votes(@debates)
|
||||||
end
|
end
|
||||||
@@ -82,11 +83,6 @@ class DebatesController < ApplicationController
|
|||||||
@featured_tags = ActsAsTaggableOn::Tag.where(featured: true)
|
@featured_tags = ActsAsTaggableOn::Tag.where(featured: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_order
|
|
||||||
@valid_orders = ['confidence_score', 'hot_score', 'created_at', 'most_commented', 'random']
|
|
||||||
@order = @valid_orders.include?(params[:order]) ? params[:order] : @valid_orders.first
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_tag_filter
|
def parse_tag_filter
|
||||||
if params[:tag].present?
|
if params[:tag].present?
|
||||||
@tag_filter = params[:tag] if ActsAsTaggableOn::Tag.where(name: params[:tag]).exists?
|
@tag_filter = params[:tag] if ActsAsTaggableOn::Tag.where(name: params[:tag]).exists?
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<section role="main">
|
<section role="main">
|
||||||
|
|
||||||
<div class="wrap row">
|
<div class="wrap row">
|
||||||
<div id="debates" class="debates-list small-12 medium-9 column js-order-<%= @order.dasherize %>">
|
<div id="debates" class="debates-list small-12 medium-9 column js-order-<%= @current_order.dasherize %>">
|
||||||
<div class="filters">
|
<div class="filters">
|
||||||
<div class="small-12 medium-7 column">
|
<div class="small-12 medium-7 column">
|
||||||
<% if @search_terms %>
|
<% if @search_terms %>
|
||||||
@@ -27,17 +27,7 @@
|
|||||||
<%= t("debates.index.select_order_long") %>
|
<%= t("debates.index.select_order_long") %>
|
||||||
</h2>
|
</h2>
|
||||||
<% end %>
|
<% end %>
|
||||||
<form class="inline-block">
|
<%= render 'shared/order_select', i18n_namespace: "debates.index" %>
|
||||||
<select class="js-location-changer" name="order-selector">
|
|
||||||
<% @valid_orders.each do |order| %>
|
|
||||||
<option <%= 'selected' if order == @order %>
|
|
||||||
value='<%= current_path_with_query_params(order: order, page: 1) %>'>
|
|
||||||
<%= t("debates.index.orders.#{order}") %>
|
|
||||||
</option>
|
|
||||||
<% end %>
|
|
||||||
</select>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="show-for-small-only">
|
<div class="show-for-small-only">
|
||||||
<%= link_to t("debates.index.start_debate"), new_debate_path, class: 'button radius expand' %>
|
<%= link_to t("debates.index.start_debate"), new_debate_path, class: 'button radius expand' %>
|
||||||
|
|||||||
15
app/views/shared/_order_select.html.erb
Normal file
15
app/views/shared/_order_select.html.erb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<% # Params:
|
||||||
|
#
|
||||||
|
# i18n_namespace: for example "moderation.debates.index"
|
||||||
|
%>
|
||||||
|
|
||||||
|
<form class="inline-block">
|
||||||
|
<select class="js-location-changer" name="order-selector">
|
||||||
|
<% @valid_orders.each do |order| %>
|
||||||
|
<option <%= 'selected' if order == @current_order %>
|
||||||
|
value='<%= current_path_with_query_params(order: order, page: 1) %>'>
|
||||||
|
<%= t("#{i18n_namespace}.orders.#{order}") %>
|
||||||
|
</option>
|
||||||
|
<% end %>
|
||||||
|
</select>
|
||||||
|
</form>
|
||||||
37
spec/controllers/concerns/has_orders_spec.rb
Normal file
37
spec/controllers/concerns/has_orders_spec.rb
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe 'HasOrders' do
|
||||||
|
|
||||||
|
class FakeController < ActionController::Base; end
|
||||||
|
|
||||||
|
controller(FakeController) do
|
||||||
|
include HasOrders
|
||||||
|
has_orders ['created_at', 'votes_count', 'flags_count'], only: :index
|
||||||
|
|
||||||
|
def index
|
||||||
|
render text: "#{@current_order} (#{@valid_orders.join(' ')})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "has the valid orders set up" do
|
||||||
|
get :index
|
||||||
|
expect(response.body).to eq('created_at (created_at votes_count flags_count)')
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "the current order" do
|
||||||
|
it "defaults to the first one on the list" do
|
||||||
|
get :index
|
||||||
|
expect(response.body).to eq('created_at (created_at votes_count flags_count)')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can be changed by the order param" do
|
||||||
|
get :index, order: 'votes_count'
|
||||||
|
expect(response.body).to eq('votes_count (created_at votes_count flags_count)')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "defaults to the first one on the list if given a bogus order" do
|
||||||
|
get :index, order: 'foobar'
|
||||||
|
expect(response.body).to eq('created_at (created_at votes_count flags_count)')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user