Merge pull request #1635 from entantoencuanto/issue#1354-suggest_on_investments_creation
Issue#1354 suggest on investments creation
This commit is contained in:
@@ -20,10 +20,11 @@ module Budgets
|
||||
|
||||
has_orders %w{most_voted newest oldest}, only: :show
|
||||
has_orders ->(c) { c.instance_variable_get(:@budget).investments_orders }, only: :index
|
||||
has_filters %w{not_unfeasible feasible unfeasible unselected selected}, only: [:index, :show]
|
||||
has_filters %w{not_unfeasible feasible unfeasible unselected selected}, only: [:index, :show, :suggest]
|
||||
|
||||
invisible_captcha only: [:create, :update], honeypot: :subtitle, scope: :budget_investment
|
||||
|
||||
helper_method :resource_model, :resource_name
|
||||
respond_to :html, :js
|
||||
|
||||
def index
|
||||
@@ -70,8 +71,22 @@ module Budgets
|
||||
end
|
||||
end
|
||||
|
||||
def suggest
|
||||
@resource_path_method = :namespaced_budget_investment_path
|
||||
@resource_relation = resource_model.where(budget: @budget).apply_filters_and_search(@budget, params, @current_filter)
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def resource_model
|
||||
Budget::Investment
|
||||
end
|
||||
|
||||
def resource_name
|
||||
"budget_investment"
|
||||
end
|
||||
|
||||
def load_investment_votes(investments)
|
||||
@investment_votes = current_user ? current_user.budget_investment_votes(investments) : {}
|
||||
end
|
||||
|
||||
@@ -34,7 +34,7 @@ module CommentableActions
|
||||
|
||||
def suggest
|
||||
@limit = 5
|
||||
@resources = @search_terms.present? ? resource_model.search(@search_terms) : nil
|
||||
@resources = @search_terms.present? ? resource_relation.search(@search_terms) : nil
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -10,6 +10,10 @@ module Polymorphic
|
||||
@resource_name ||= resource_model.to_s.downcase
|
||||
end
|
||||
|
||||
def resource_relation
|
||||
@resource_relation ||= resource_model.all
|
||||
end
|
||||
|
||||
def set_resource_instance
|
||||
instance_variable_set("@#{resource_name}", @resource)
|
||||
end
|
||||
|
||||
@@ -46,6 +46,7 @@ module Abilities
|
||||
can :create, SpendingProposal
|
||||
|
||||
can :create, Budget::Investment, budget: { phase: "accepting" }
|
||||
can :suggest, Budget::Investment, budget: { phase: "accepting" }
|
||||
can :destroy, Budget::Investment, budget: { phase: ["accepting", "reviewing"] }, author_id: user.id
|
||||
can :vote, Budget::Investment, budget: { phase: "selecting" }
|
||||
can [:show, :create], Budget::Ballot, budget: { phase: "balloting" }
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
</div>
|
||||
|
||||
<div class="small-12 column">
|
||||
<%= f.text_field :title, maxlength: SpendingProposal.title_max_length %>
|
||||
<%= f.text_field :title, maxlength: SpendingProposal.title_max_length, data: { js_suggest_result: "js_suggest_result", js_suggest: "#js-suggest", js_url: suggest_budget_investments_path(@budget) }%>
|
||||
</div>
|
||||
<div id="js-suggest"></div>
|
||||
|
||||
<%= f.invisible_captcha :subtitle %>
|
||||
|
||||
|
||||
1
app/views/budgets/investments/suggest.js.erb
Normal file
1
app/views/budgets/investments/suggest.js.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= render "shared/suggest" %>
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
<ul>
|
||||
<% @resources.limit(@limit).each do |resource| %>
|
||||
<li><%= link_to resource.title, resource %></li>
|
||||
<li><%= link_to resource.title, @resource_path_method.present? ? send(@resource_path_method, resource) : resource %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
|
||||
@@ -511,6 +511,12 @@ en:
|
||||
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: "Ver todos"
|
||||
budget_investment:
|
||||
found:
|
||||
one: "There is an investment with the term '%{query}', you can participate in it instead of opening a new one."
|
||||
other: "There are investments with the term '%{query}', you can participate in them instead of opening a new one."
|
||||
message: "You are seeing %{limit} of %{count} investments containing the term '%{query}'"
|
||||
see_all: "See all"
|
||||
proposal:
|
||||
found:
|
||||
one: "There is a proposal with the term '%{query}', you can contribute to it instead of creating a new"
|
||||
|
||||
@@ -511,6 +511,12 @@ es:
|
||||
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"
|
||||
budget_investment:
|
||||
found:
|
||||
one: "Existe una propuesta de inversión con el término '%{query}', puedes participar en ella en vez de abrir una nueva."
|
||||
other: "Existen propuestas de inversión con el término '%{query}', puedes participar en ellas en vez de abrir una nueva."
|
||||
message: "Estás viendo %{limit} de %{count} propuestas de inversión que contienen el término '%{query}'"
|
||||
see_all: "Ver todas"
|
||||
proposal:
|
||||
found:
|
||||
one: "Existe una propuesta con el término '%{query}', puedes participar en ella en vez de abrir uno nuevo."
|
||||
|
||||
@@ -78,7 +78,8 @@ Rails.application.routes.draw do
|
||||
resources :budgets, only: [:show, :index] do
|
||||
resources :groups, controller: "budgets/groups", only: [:show]
|
||||
resources :investments, controller: "budgets/investments", only: [:index, :new, :create, :show, :destroy] do
|
||||
member { post :vote }
|
||||
member { post :vote }
|
||||
collection { get :suggest }
|
||||
end
|
||||
resource :ballot, only: :show, controller: "budgets/ballots" do
|
||||
resources :lines, controller: "budgets/ballot/lines", only: [:create, :destroy]
|
||||
|
||||
@@ -4,6 +4,7 @@ feature 'Budget Investments' do
|
||||
|
||||
let(:author) { create(:user, :level_two, username: 'Isabel') }
|
||||
let(:budget) { create(:budget, name: "Big Budget") }
|
||||
let(:other_budget) { create(:budget, name: "What a Budget!") }
|
||||
let(:group) { create(:budget_group, name: "Health", budget: budget) }
|
||||
let!(:heading) { create(:budget_heading, name: "More hospitals", group: group) }
|
||||
|
||||
@@ -248,6 +249,56 @@ feature 'Budget Investments' do
|
||||
expect(page).to have_content error_message
|
||||
end
|
||||
|
||||
context 'Suggest' do
|
||||
factory = :budget_investment
|
||||
|
||||
scenario 'Show up to 5 suggestions', :js do
|
||||
login_as(author)
|
||||
|
||||
%w(first second third fourth fifth sixth).each do |ordinal|
|
||||
create(factory, title: "#{ordinal.titleize} #{factory}, has search term", budget: budget)
|
||||
end
|
||||
create(factory, title: "This is the last #{factory}", budget: budget)
|
||||
|
||||
visit new_budget_investment_path(budget)
|
||||
fill_in "budget_investment_title", with: "search"
|
||||
|
||||
within("div#js-suggest") do
|
||||
expect(page).to have_content ("You are seeing 5 of 6 investments containing the term 'search'")
|
||||
end
|
||||
end
|
||||
|
||||
scenario 'No found suggestions', :js do
|
||||
login_as(author)
|
||||
|
||||
%w(first second third fourth fifth sixth).each do |ordinal|
|
||||
create(factory, title: "#{ordinal.titleize} #{factory}, has search term", budget: budget)
|
||||
end
|
||||
|
||||
visit new_budget_investment_path(budget)
|
||||
fill_in "budget_investment_title", with: "item"
|
||||
|
||||
within('div#js-suggest') do
|
||||
expect(page).to_not have_content ('You are seeing')
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Don't show suggestions from a different budget", :js do
|
||||
login_as(author)
|
||||
|
||||
%w(first second third fourth fifth sixth).each do |ordinal|
|
||||
create(factory, title: "#{ordinal.titleize} #{factory}, has search term", budget: budget)
|
||||
end
|
||||
|
||||
visit new_budget_investment_path(other_budget)
|
||||
fill_in "budget_investment_title", with: "search"
|
||||
|
||||
within('div#js-suggest') do
|
||||
expect(page).to_not have_content ('You are seeing')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
scenario 'Ballot is not visible' do
|
||||
login_as(author)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user