diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index a3d4d1b7c..98e378e69 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -39,6 +39,7 @@ //= require annotatable //= require advanced_search //= require registration_form +//= require suggest var initialize_modules = function() { App.Comments.initialize(); @@ -53,6 +54,7 @@ var initialize_modules = function() { App.Annotatable.initialize(); App.AdvancedSearch.initialize(); App.RegistrationForm.initialize(); + App.Suggest.initialize(); }; $(function(){ diff --git a/app/assets/javascripts/suggest.js.coffee b/app/assets/javascripts/suggest.js.coffee new file mode 100644 index 000000000..ab803439d --- /dev/null +++ b/app/assets/javascripts/suggest.js.coffee @@ -0,0 +1,16 @@ +App.Suggest = + + initialize: -> + $('[data-js-suggest-result]').on('blur',(event) -> + + js_suggest = $(this).data('js-suggest') + + $.ajax + url: $(this).data('js-url') + data: {search: $(this).val()}, + type: 'GET', + dataType: 'html' + success: (stHtml) -> + $(js_suggest).html(stHtml) + error: (xhr, status) -> + complete: (xhr, status) ->) diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 286d2a297..4e7ce8848 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -29,6 +29,16 @@ module CommentableActions set_resource_instance end + def suggest + @resources = @search_terms.present? ? resource_model.search(@search_terms).sort_by_confidence_score: nil + set_resources_instance + if @resources then + @resources_count = @resources.count + @reg_show = 5 + render layout: false + end + end + def create @resource = resource_model.new(strong_params) @resource.author = current_user diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index ae8aa6f2a..c0365d310 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -3,7 +3,7 @@ class DebatesController < ApplicationController include CommentableActions include FlagActions - before_action :parse_search_terms, only: :index + before_action :parse_search_terms, only: [:index, :suggest] before_action :parse_advanced_search_terms, only: :index before_action :parse_tag_filter, only: :index before_action :set_search_order, only: :index diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 1ee319b93..92a302a49 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -2,7 +2,7 @@ class ProposalsController < ApplicationController include CommentableActions include FlagActions - before_action :parse_search_terms, only: :index + before_action :parse_search_terms, only: [:index, :suggest] before_action :parse_advanced_search_terms, only: :index before_action :parse_tag_filter, only: :index before_action :set_search_order, only: :index diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index 4d74ce375..002518dac 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -23,6 +23,9 @@ module Abilities can :create, Debate can :create, Proposal + can :suggest, Debate + can :suggest, Proposal + can [:flag, :unflag], Comment cannot [:flag, :unflag], Comment, user_id: user.id diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index b26ac555b..60bee011d 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -1,12 +1,11 @@ <%= form_for(@debate) do |f| %> <%= render 'shared/errors', resource: @debate %> -
<%= f.label :title, t("debates.form.debate_title") %> - <%= f.text_field :title, maxlength: Debate.title_max_length, placeholder: t("debates.form.debate_title"), label: false %> + <%= f.text_field :title, maxlength: Debate.title_max_length, placeholder: t("debates.form.debate_title"), label: false, data: {js_suggest_result: "js_suggest_result", js_suggest: "#js-suggest", js_url: suggest_debates_path}%>
- +
<%= f.label :description, t("debates.form.debate_text") %> <%= f.cktext_area :description, maxlength: Debate.description_max_length, ckeditor: { language: I18n.locale }, label: false %> @@ -41,4 +40,4 @@ <%= f.submit(class: "button radius", value: t("debates.#{action_name}.form.submit_button")) %>
-<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/debates/_suggest.html.erb b/app/views/debates/_suggest.html.erb new file mode 100644 index 000000000..c043a7e93 --- /dev/null +++ b/app/views/debates/_suggest.html.erb @@ -0,0 +1,22 @@ +
+ <% if @search_terms && @resources_count > 0 %> +
+

+ <%= t("debates.new.suggestions.found", count: @resources_count, query: @search_terms)%> +

+ + <% if @resources_count > @reg_show %> +

+ <%= t("debates.new.suggestions.message", count: @resources_count, + query: @search_terms, + limit: @reg_show) %> + <%= link_to t("debates.new.suggestions.see_all"), debates_path(search: @search_terms)%> +

+ <% end %> +
+ <% end %> +
\ No newline at end of file diff --git a/app/views/debates/suggest.js.erb b/app/views/debates/suggest.js.erb new file mode 100644 index 000000000..de10bc0ad --- /dev/null +++ b/app/views/debates/suggest.js.erb @@ -0,0 +1 @@ +<%= render "suggest" %> \ No newline at end of file diff --git a/app/views/proposals/_form.html.erb b/app/views/proposals/_form.html.erb index af1d219d3..7dd16812e 100644 --- a/app/views/proposals/_form.html.erb +++ b/app/views/proposals/_form.html.erb @@ -4,8 +4,9 @@
<%= f.label :title, t("proposals.form.proposal_title") %> - <%= f.text_field :title, maxlength: Proposal.title_max_length, placeholder: t("proposals.form.proposal_title"), label: false %> + <%= f.text_field :title, maxlength: Proposal.title_max_length, placeholder: t("proposals.form.proposal_title"), label: false, data: {js_suggest_result: "js_suggest_result", js_suggest: "#js-suggest", js_url: suggest_proposals_path}%>
+
<%= f.label :question, t("proposals.form.proposal_question") %> diff --git a/app/views/proposals/_suggest.html.erb b/app/views/proposals/_suggest.html.erb new file mode 100644 index 000000000..fe94f7fc9 --- /dev/null +++ b/app/views/proposals/_suggest.html.erb @@ -0,0 +1,22 @@ +
+ <% if @search_terms && @resources_count > 0 %> +
+

+ <%= t("proposals.new.suggestions.found", count: @resources_count, query: @search_terms)%> +

+
    + <% @proposals.take(@reg_show).each do |proposal| %> +
  • <%= link_to proposal.title, proposal %>
  • + <% end %> +
+ <% if @resources_count > @reg_show %> +

+ <%= t("proposals.new.suggestions.message", count: @resources_count, + query: @search_terms, + limit: @reg_show) %> + <%= link_to t("proposals.new.suggestions.see_all"), proposals_path(search: @search_terms)%> +

+ <% end %> +
+ <% end %> +
\ No newline at end of file diff --git a/app/views/proposals/suggest.js.erb b/app/views/proposals/suggest.js.erb new file mode 100644 index 000000000..de10bc0ad --- /dev/null +++ b/app/views/proposals/suggest.js.erb @@ -0,0 +1 @@ +<%= render "suggest" %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 555b47413..583fcf496 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -121,6 +121,12 @@ en: recommendation_two: Any debate or comment suggesting illegal action will be deleted, as well as those intending to sabotage the debate spaces. Anything else is allowed. recommendations_title: Recommendations for creating a debate start_new: Start a debate + suggestions: + found: + one: "There is a debate with the term '%{query}', you can participate in it instead of opening a new one." + other: "There are debates with the term '%{query}', you can participate in them instead of opening a new one." + message: "You are seeing %{limit} of %{count} debates containing the term '%{query}'" + see_all: "See all" show: author_deleted: User deleted back_link: Go back @@ -300,6 +306,12 @@ en: recommendation_two: Any proposal or comment suggesting illegal action will be deleted, as well as those intending to sabotage the debate spaces. Anything else is allowed. recommendations_title: Recommendations for creating a proposal start_new: Create new proposal + suggestions: + found: + one: "There is a proposal with the term '%{query}', you can contribute to it instead of creating a new" + other: "There are proposals with the term '%{query}', you can contribute to them instead of creating a new" + message: "You are seeing %{limit} of %{count} proposals containing the term '%{query}'" + see_all: "See all" proposal: already_supported: You have already supported this proposal. Share it! comments: diff --git a/config/locales/es.yml b/config/locales/es.yml old mode 100644 new mode 100755 index 87de46def..9dfb2147f --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -26,7 +26,8 @@ es: user_permission_votes: Participar en las votaciones finales* username_label: Nombre de usuario verified_account: Cuenta verificada - verify_my_account: Verificar mi cuenta + verify_my_account: Verificar mi cuentaquer + application: close: Cerrar menu: Menú @@ -121,6 +122,12 @@ es: recommendation_two: Cualquier debate o comentario que implique una acción ilegal será eliminado, también los que tengan la intención de sabotear los espacios de debate, todo lo demás está permitido. recommendations_title: Recomendaciones para crear un debate start_new: Empezar un debate + suggestions: + found: + one: "Existe un debate con el término '%{query}', puedes participar en él en vez de abrir uno nuevo." + other: "Existen debates con el término '%{query}', puedes participar en ellos en vez de abrir uno nuevo." + message: "Estás viendo %{limit} de %{count} debates que contienen el término '%{query}'" + see_all: "Ver todos" show: author_deleted: Usuario eliminado back_link: Volver @@ -300,6 +307,12 @@ es: recommendation_two: Cualquier propuesta o comentario que implique una acción ilegal será eliminada, también las que tengan la intención de sabotear los espacios de propuesta, todo lo demás está permitido. recommendations_title: Recomendaciones para crear una propuesta start_new: Crear una propuesta + suggestions: + found: + one: "Existe una propuesta con el término '%{query}', puedes contribuir en ella en vez de crear una nueva." + other: "Existen propuestas con el término '%{query}', puedes contribuir en ellas en vez de crear una nueva." + message: "Estás viendo %{limit} de %{count} propuestas que contienen el término '%{query}'" + see_all: "Ver todas" proposal: already_supported: "¡Ya has apoyado esta propuesta, compártela!" comments: diff --git a/config/routes.rb b/config/routes.rb index 37578ed20..b48b4628f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,9 +38,9 @@ Rails.application.routes.draw do put :flag put :unflag end - collection do get :map + get :suggest end end @@ -51,9 +51,9 @@ Rails.application.routes.draw do put :flag put :unflag end - collection do get :map + get :suggest end end @@ -242,4 +242,4 @@ Rails.application.routes.draw do # static pages get '/blog' => redirect("http://diario.madrid.es/participa/") resources :pages, path: '/', only: [:show] -end \ No newline at end of file +end diff --git a/db/schema.rb b/db/schema.rb index a9cce3582..ce259bc39 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -103,21 +103,22 @@ ActiveRecord::Schema.define(version: 20160126090634) do t.string "title", limit: 80 t.text "description" t.integer "author_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "visit_id" t.datetime "hidden_at" - t.integer "flags_count", default: 0 + t.integer "flags_count", default: 0 + t.integer "cached_votes_total", default: 0 + t.integer "cached_votes_up", default: 0 + t.integer "cached_votes_down", default: 0 t.datetime "ignored_flag_at" - t.integer "cached_votes_total", default: 0 - t.integer "cached_votes_up", default: 0 - t.integer "cached_votes_down", default: 0 - t.integer "comments_count", default: 0 + t.integer "comments_count", default: 0 t.datetime "confirmed_hide_at" - t.integer "cached_anonymous_votes_total", default: 0 - t.integer "cached_votes_score", default: 0 - t.integer "hot_score", limit: 8, default: 0 - t.integer "confidence_score", default: 0 + t.integer "cached_anonymous_votes_total", default: 0 + t.integer "cached_votes_score", default: 0 + t.integer "hot_score", limit: 8, default: 0 + t.integer "confidence_score", default: 0 + t.string "external_link", limit: 100 t.integer "geozone_id" end @@ -128,6 +129,7 @@ ActiveRecord::Schema.define(version: 20160126090634) do add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree add_index "debates", ["confidence_score"], name: "index_debates_on_confidence_score", using: :btree + add_index "debates", ["description"], name: "index_debates_on_description", using: :btree add_index "debates", ["geozone_id"], name: "index_debates_on_geozone_id", using: :btree add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree @@ -201,7 +203,7 @@ ActiveRecord::Schema.define(version: 20160126090634) do create_table "locks", force: :cascade do |t| t.integer "user_id" t.integer "tries", default: 0 - t.datetime "locked_until", default: '2000-01-01 07:01:01', null: false + t.datetime "locked_until", default: '2000-01-01 00:01:01', null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end @@ -261,6 +263,7 @@ ActiveRecord::Schema.define(version: 20160126090634) do add_index "proposals", ["author_id"], name: "index_proposals_on_author_id", using: :btree add_index "proposals", ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree add_index "proposals", ["confidence_score"], name: "index_proposals_on_confidence_score", using: :btree + add_index "proposals", ["description"], name: "index_proposals_on_description", using: :btree add_index "proposals", ["geozone_id"], name: "index_proposals_on_geozone_id", using: :btree add_index "proposals", ["hidden_at"], name: "index_proposals_on_hidden_at", using: :btree add_index "proposals", ["hot_score"], name: "index_proposals_on_hot_score", using: :btree @@ -319,8 +322,8 @@ ActiveRecord::Schema.define(version: 20160126090634) do t.boolean "featured", default: false t.integer "debates_count", default: 0 t.integer "proposals_count", default: 0 - t.string "kind" t.integer "spending_proposals_count", default: 0 + t.string "kind" end add_index "tags", ["debates_count"], name: "index_tags_on_debates_count", using: :btree @@ -396,8 +399,8 @@ ActiveRecord::Schema.define(version: 20160126090634) do t.boolean "public_activity", default: true t.boolean "newsletter", default: false t.integer "notifications_count", default: 0 - t.string "locale" t.boolean "registering_with_oauth", default: false + t.string "locale" t.string "oauth_email" end diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 44955bb29..477994c1f 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -947,4 +947,43 @@ feature 'Debates' do end end -end \ No newline at end of file + + context 'Suggesting debates' do + scenario 'Shows up to 5 suggestions', :js do + author = create(:user) + login_as(author) + + debate1 = create(:debate, title: "First debate has 1 vote", cached_votes_up: 1) + debate2 = create(:debate, title: "Second debate has 2 votes", cached_votes_up: 2) + debate3 = create(:debate, title: "Third debate has 3 votes", cached_votes_up: 3) + debate4 = create(:debate, title: "This one has 4 votes", description: "This is the fourth debate", cached_votes_up: 4) + debate5 = create(:debate, title: "Fifth debate has 5 votes", cached_votes_up: 5) + debate6 = create(:debate, title: "Sixth debate has 6 votes", description: 'This is the sixth debate', cached_votes_up: 6) + debate7 = create(:debate, title: "This has seven votes, and is not suggest", description: 'This is the seven', cached_votes_up: 7) + + visit new_debate_path + fill_in 'debate_title', with: 'debate' + check "debate_terms_of_service" + + within('div#js-suggest') do + expect(page).to have_content ("You are seeing 5 of 6 debates containing the term 'debate'") + end + end + + scenario 'No found suggestions', :js do + author = create(:user) + login_as(author) + + debate1 = create(:debate, title: "First debate has 10 vote", cached_votes_up: 10) + debate2 = create(:debate, title: "Second debate has 2 votes", cached_votes_up: 2) + + visit new_debate_path + fill_in 'debate_title', with: 'proposal' + check "debate_terms_of_service" + + within('div#js-suggest') do + expect(page).to_not have_content ('You are seeing') + end + end + end +end diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 08b99463c..acd2a6e0d 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -1105,4 +1105,42 @@ feature 'Proposals' do end end + context 'Suggesting proposals' do + scenario 'Show up to 5 suggestions', :js do + author = create(:user) + login_as(author) + + create(:proposal, title: 'First proposal, has search term') + create(:proposal, title: 'Second title') + create(:proposal, title: 'Third proposal, has search term') + create(:proposal, title: 'Fourth proposal, has search term') + create(:proposal, title: 'Fifth proposal, has search term') + create(:proposal, title: 'Sixth proposal, has search term') + create(:proposal, title: 'Seventh proposal, has search term') + + visit new_proposal_path + fill_in 'proposal_title', with: 'search' + check "proposal_terms_of_service" + + within('div#js-suggest') do + expect(page).to have_content ("You are seeing 5 of 6 proposals containing the term 'search'") + end + end + + scenario 'No found suggestions', :js do + author = create(:user) + login_as(author) + + create(:proposal, title: 'First proposal').update_column(:confidence_score, 10) + create(:proposal, title: 'Second proposal').update_column(:confidence_score, 8) + + visit new_proposal_path + fill_in 'proposal_title', with: 'debate' + check "proposal_terms_of_service" + + within('div#js-suggest') do + expect(page).to_not have_content ('You are seeing') + end + end + end end