diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb
index b5a0ae25a..4a22d2e3e 100644
--- a/app/controllers/debates_controller.rb
+++ b/app/controllers/debates_controller.rb
@@ -28,6 +28,16 @@ class DebatesController < ApplicationController
set_debate_votes(@debate)
end
+ def remove_feature
+ @debate.update_attribute(:featured_at, nil)
+ redirect_to request.query_parameters.merge(action: :index)
+ end
+
+ def feature
+ @debate.update_attribute(:featured_at, Time.now)
+ redirect_to request.query_parameters.merge(action: :index)
+ end
+
private
def debate_params
diff --git a/app/helpers/debates_helper.rb b/app/helpers/debates_helper.rb
new file mode 100644
index 000000000..d12db7f78
--- /dev/null
+++ b/app/helpers/debates_helper.rb
@@ -0,0 +1,6 @@
+module DebatesHelper
+
+ def has_featured
+ Debate.all.featured.count > 0
+ end
+end
\ No newline at end of file
diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb
index 1b440795f..9727e8e35 100644
--- a/app/models/abilities/administrator.rb
+++ b/app/models/abilities/administrator.rb
@@ -30,6 +30,10 @@ module Abilities
can :confirm_hide, User
cannot :confirm_hide, User, hidden_at: nil
+ can :feature, Debate
+
+ can :remove_feature, Debate
+
can :comment_as_administrator, [Debate, Comment, Proposal]
can [:search, :create, :index, :destroy], ::Moderator
diff --git a/app/models/debate.rb b/app/models/debate.rb
index ceec9946b..249ae0c44 100644
--- a/app/models/debate.rb
+++ b/app/models/debate.rb
@@ -36,7 +36,8 @@ class Debate < ActiveRecord::Base
scope :sort_by_random, -> { reorder("RANDOM()") }
scope :sort_by_relevance, -> { all }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
- scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
+ scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
+ scope :featured, -> { where("featured_at is not null")}
# Ahoy setup
visitable # Ahoy will automatically assign visit_id on create
@@ -132,4 +133,8 @@ class Debate < ActiveRecord::Base
self.tags.each{ |t| t.increment_custom_counter_for('Debate') }
end
+ def featured?
+ ! self.featured_at.nil?
+ end
+
end
diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb
index f435c5511..313c34042 100644
--- a/app/views/admin/_menu.html.erb
+++ b/app/views/admin/_menu.html.erb
@@ -25,7 +25,7 @@
<%= t("admin.menu.hidden_debates") %>
<% end %>
- <% end %>
+ <% end %>
>
<%= link_to admin_comments_path do %>
diff --git a/app/views/debates/_actions.html.erb b/app/views/debates/_actions.html.erb
index db9821ad0..b88d4830c 100644
--- a/app/views/debates/_actions.html.erb
+++ b/app/views/debates/_actions.html.erb
@@ -8,3 +8,14 @@
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(debate.author_id),
method: :put, data: { confirm: t('admin.actions.confirm') } %>
<% end %>
+
+<% if can? :feature, debate %>
+ |
+ <% if debate.featured? %>
+ <%= link_to t("admin.actions.remove_feature").capitalize, remove_feature_debate_path(debate),
+ method: :put, data: { confirm: t('admin.actions.confirm') } %>
+ <% else %>
+ <%= link_to t("admin.actions.feature").capitalize, feature_debate_path(debate),
+ method: :put, data: { confirm: t('admin.actions.confirm') } %>
+ <% end %>
+<% end %>
diff --git a/app/views/debates/_featured_debates.html.erb b/app/views/debates/_featured_debates.html.erb
new file mode 100644
index 000000000..8b0450e16
--- /dev/null
+++ b/app/views/debates/_featured_debates.html.erb
@@ -0,0 +1,13 @@
+
+
+
+
<%= t("debates.index.featured_debates") %>
+
+
+ <% @debates.featured.each do |debate| %>
+
+
<%= link_to debate.title, debate %>
+ <%= link_to debate.author.name, user_path(debate.author)%>
+
+ <% end %>
+
diff --git a/app/views/debates/_featured_debates_static.html.erb b/app/views/debates/_featured_debates_static.html.erb
deleted file mode 100644
index 8f7d0b68d..000000000
--- a/app/views/debates/_featured_debates_static.html.erb
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
-
-
<%= t("debates.index.featured_debates") %>
-
-
-
-
<%= link_to "#Túpreguntas: Manuela Carmena, Alcaldesa de Madrid.", "https://decide.madrid.es/debates/4320" %>
- <%= link_to "Manuela Carmena ", "https://decide.madrid.es/users/21452"%>
-
-
-
-
<%= link_to "¿Y si pudieras entrevistar a Manuela Carmena o a los otros concejales?", "https://decide.madrid.es/debates/4299" %>
- <%= link_to "ParticipaciónYTransparenciaAytoMadrid", "https://decide.madrid.es/users/52055"%>
-
-
-
diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb
index 6488b5844..7ee9d12d4 100644
--- a/app/views/debates/index.html.erb
+++ b/app/views/debates/index.html.erb
@@ -25,8 +25,8 @@
<% end %>
- <% unless @tag_filter || @search_terms %>
- <%= render "featured_debates_static" %>
+ <% unless @tag_filter || @search_terms || !has_featured %>
+ <%= render "featured_debates", debate: @debates.featured %>
<% end %>
<%= render "shared/advanced_search", search_path: debates_path(page: 1) %>
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml
index 47cbad031..16d0a72a3 100755
--- a/config/locales/admin.en.yml
+++ b/config/locales/admin.en.yml
@@ -7,6 +7,8 @@ en:
hide: Hide
hide_author: Hide author
restore: Restore
+ feature: Feature
+ remove_feature: Remove feature
activity:
show:
action: Action
@@ -50,6 +52,7 @@ en:
debate_topics: Debate topics
hidden_comments: Hidden comments
hidden_debates: Hidden debates
+ featured_debates: Featured debates
hidden_proposals: Hidden proposals
hidden_users: Hidden users
incomplete_verifications: Incomplete verifications
@@ -233,4 +236,4 @@ en:
index:
phone_not_given: Phone not given
sms_code_not_confirmed: Has not confirmed the sms code
- title: Incomplete verifications
\ No newline at end of file
+ title: Incomplete verifications
diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml
index c8a710d72..ca1255560 100644
--- a/config/locales/admin.es.yml
+++ b/config/locales/admin.es.yml
@@ -7,6 +7,8 @@ es:
hide: Ocultar
hide_author: Bloquear al autor
restore: Volver a mostrar
+ feature: Destacar
+ remove_feature: Quitar destacado
activity:
show:
action: Acción
@@ -50,6 +52,7 @@ es:
debate_topics: Temas de debate
hidden_comments: Comentarios ocultos
hidden_debates: Debates ocultos
+ featured_debates: Debates destacados
hidden_proposals: Propuestas ocultas
hidden_users: Usuarios bloqueados
incomplete_verifications: Verificaciones incompletas
diff --git a/config/routes.rb b/config/routes.rb
index 0d704be04..79aa167e5 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -36,6 +36,8 @@ Rails.application.routes.draw do
post :vote
put :flag
put :unflag
+ put :remove_feature
+ put :feature
end
collection do
get :map
diff --git a/db/migrate/20160315084335_add_featured_at_to_debates.rb b/db/migrate/20160315084335_add_featured_at_to_debates.rb
new file mode 100644
index 000000000..81093aed3
--- /dev/null
+++ b/db/migrate/20160315084335_add_featured_at_to_debates.rb
@@ -0,0 +1,5 @@
+class AddFeaturedAtToDebates < ActiveRecord::Migration
+ def change
+ add_column :debates, :featured_at, :datetime
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 55021dfa0..e4057e7ed 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -120,6 +120,7 @@ ActiveRecord::Schema.define(version: 20160329160106) do
t.integer "confidence_score", default: 0
t.integer "geozone_id"
t.tsvector "tsv"
+ t.datetime "featured_at"
end
add_index "debates", ["author_id", "hidden_at"], name: "index_debates_on_author_id_and_hidden_at", using: :btree
diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb
index d9ab8c8eb..b7cc3ea3a 100644
--- a/spec/features/debates_spec.rb
+++ b/spec/features/debates_spec.rb
@@ -1008,4 +1008,43 @@ feature 'Debates' do
end
end
end
+
+ scenario 'Mark debate as featured' do
+ admin = create(:administrator)
+ login_as(admin.user)
+
+ debate1 = create(:debate)
+ debate2 = create(:debate, featured_at: Time.now)
+
+ visit debate_path(debate1)
+ expect(page).to have_content("Feature")
+
+ visit debate_path(debate2)
+ expect(page).to have_content("Remove feature")
+
+ end
+
+
+ scenario 'Show featured debates' do
+ admin = create(:administrator)
+ login_as(admin.user)
+
+ debate1 = create(:debate, featured_at: Time.now)
+ debate2 = create(:debate)
+
+ visit debates_path
+ expect(page).to have_content("Featured")
+ end
+
+
+ scenario 'Dont show featured debates' do
+ admin = create(:administrator)
+ login_as(admin.user)
+
+ debate1 = create(:debate)
+ debate2 = create(:debate)
+
+ visit debates_path
+ expect(page).to_not have_content("Featured")
+ end
end