From 0795da02d8c5d2b44c35ca304c290fc7cd3ad3f4 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sun, 25 Sep 2016 10:20:38 +0200 Subject: [PATCH] adds poll CRU actions --- .../admin/poll/polls_controller.rb | 22 ++++++++ app/models/poll.rb | 2 + app/views/admin/poll/polls/_form.html.erb | 34 ++++++++++++ app/views/admin/poll/polls/_poll.html.erb | 2 +- app/views/admin/poll/polls/edit.html.erb | 5 +- app/views/admin/poll/polls/index.html.erb | 37 ++++++------- app/views/admin/poll/polls/new.html.erb | 33 +----------- app/views/admin/poll/polls/show.html.erb | 6 +-- config/locales/admin.en.yml | 2 +- config/locales/responders.en.yml | 3 +- config/locales/responders.es.yml | 1 + spec/features/admin/poll/polls_spec.rb | 53 ++++++++++++++++++- spec/models/poll_spec.rb | 16 ++++++ 13 files changed, 157 insertions(+), 59 deletions(-) create mode 100644 app/views/admin/poll/polls/_form.html.erb create mode 100644 spec/models/poll_spec.rb diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index eb8b417b8..2a7c1e730 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -10,7 +10,29 @@ class Admin::Poll::PollsController < Admin::BaseController def new end + def create + if @poll.save + redirect_to [:admin, @poll], notice: t("flash.actions.create.poll") + else + render :new + end + end + def edit end + def update + if @poll.update(poll_params) + redirect_to [:admin, @poll], notice: t("flash.actions.update.poll") + else + render :edit + end + end + + private + + def poll_params + params.require(:poll).permit(:name) + end + end \ No newline at end of file diff --git a/app/models/poll.rb b/app/models/poll.rb index 507c848aa..4543a2544 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -1,4 +1,6 @@ class Poll < ActiveRecord::Base has_many :booths has_many :voters, through: :booths, class_name: "Poll::Voter" + + validates :name, presence: true end \ No newline at end of file diff --git a/app/views/admin/poll/polls/_form.html.erb b/app/views/admin/poll/polls/_form.html.erb new file mode 100644 index 000000000..64d7177a6 --- /dev/null +++ b/app/views/admin/poll/polls/_form.html.erb @@ -0,0 +1,34 @@ +<%= form_for [:admin, @poll] do |f| %> +
+
+ <%= f.text_field :name, + placeholder: t('admin.polls.new.name'), + label: t("admin.polls.new.name") %> +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+ +
+
+ +
+
+<% end %> \ No newline at end of file diff --git a/app/views/admin/poll/polls/_poll.html.erb b/app/views/admin/poll/polls/_poll.html.erb index a9f2dcd1b..4128ad222 100644 --- a/app/views/admin/poll/polls/_poll.html.erb +++ b/app/views/admin/poll/polls/_poll.html.erb @@ -2,7 +2,7 @@ <%= link_to poll.name, admin_poll_path(poll) %> - ß + Próximamente diff --git a/app/views/admin/poll/polls/edit.html.erb b/app/views/admin/poll/polls/edit.html.erb index 68eb7cc3b..0158655a5 100644 --- a/app/views/admin/poll/polls/edit.html.erb +++ b/app/views/admin/poll/polls/edit.html.erb @@ -1,8 +1,9 @@ <%= render 'shared/back_link' %>

<%= t("admin.polls.edit.title") %>

+<%= render "form" %> -
+
\ No newline at end of file diff --git a/app/views/admin/poll/polls/index.html.erb b/app/views/admin/poll/polls/index.html.erb index b24cc10f6..2954e44b8 100644 --- a/app/views/admin/poll/polls/index.html.erb +++ b/app/views/admin/poll/polls/index.html.erb @@ -1,21 +1,22 @@

<%= t("admin.polls.index.title") %>

-<%= link_to t("admin.polls.index.create"), new_admin_poll_path, class: "button success float-right" %> +<%= link_to t("admin.polls.index.create"), + new_admin_poll_path, + class: "button success float-right" %> - -
- <%= t("admin.polls.index.no_polls") %> -
- - - - - - - - - - - <%= render @polls %> - -
<%= t("admin.polls.index.name") %><%= t("admin.polls.index.status") %> 
+<% if Poll.any? %> + + + + + + + + <%= render @polls %> + +
<%= t("admin.polls.index.name") %><%= t("admin.polls.index.status") %> 
+<% else %> +
+ <%= t("admin.polls.index.no_polls") %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/poll/polls/new.html.erb b/app/views/admin/poll/polls/new.html.erb index b2d74cffc..ba1ae7260 100644 --- a/app/views/admin/poll/polls/new.html.erb +++ b/app/views/admin/poll/polls/new.html.erb @@ -2,35 +2,4 @@

<%= t("admin.polls.new.title") %>

-
-
-
- - -
- -
- - -
-
- -
-
- - -
- -
- - -
-
- -
-
- -
-
- -
\ No newline at end of file +<%= render "form" %> \ No newline at end of file diff --git a/app/views/admin/poll/polls/show.html.erb b/app/views/admin/poll/polls/show.html.erb index 105cf2fe6..1b0d1e198 100644 --- a/app/views/admin/poll/polls/show.html.erb +++ b/app/views/admin/poll/polls/show.html.erb @@ -5,7 +5,7 @@ <%= @poll.name %> -<%= link_to t("admin.actions.edit"), "#", class: "button hollow float-right" %> +<%= link_to t("admin.actions.edit"), edit_admin_poll_path(@poll), class: "button hollow float-right" %>

(REFNUM)

Próximamente (15/12/2016 - 15/02/2017)

@@ -26,7 +26,7 @@   - + <%= @poll.booths.each do |booth| %> @@ -40,6 +40,6 @@ <%= link_to t("admin.actions.edit"), "#", class: "button hollow" %> - + <% end %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 40f959a3a..bb8093b03 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -155,7 +155,7 @@ en: polls: index: title: "List of polls" - no_polls: "There are any poll." + no_polls: "There are no polls." create: "Create poll" name: "Name" status: "Status" diff --git a/config/locales/responders.en.yml b/config/locales/responders.en.yml index ab1641799..422a59e0c 100755 --- a/config/locales/responders.en.yml +++ b/config/locales/responders.en.yml @@ -6,15 +6,16 @@ en: notice: "%{resource_name} created successfully." debate: "Debate created successfully." direct_message: "You message has been sent successfully." + poll: "Poll created successfully." proposal: "Proposal created successfully." proposal_notification: "Your message has been sent correctly." spending_proposal: "Spending proposal created successfully. You can access it from %{activity}" - save_changes: notice: Changes saved update: notice: "%{resource_name} updated successfully." debate: "Debate updated successfully." + poll: "Poll updated successfully." proposal: "Proposal updated successfully." spending_proposal: "Investment project updated succesfully." destroy: diff --git a/config/locales/responders.es.yml b/config/locales/responders.es.yml index 387085d69..c9292b187 100644 --- a/config/locales/responders.es.yml +++ b/config/locales/responders.es.yml @@ -6,6 +6,7 @@ es: notice: "%{resource_name} creado correctamente." debate: "Debate creado correctamente." direct_message: "Tu mensaje ha sido enviado correctamente." + poll: "Votación presencial creada correctamente." proposal: "Propuesta creada correctamente." proposal_notification: "Tu message ha sido enviado correctamente." spending_proposal: "Propuesta de inversión creada correctamente. Puedes acceder a ella desde %{activity}" diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index a94c81cc9..8d130e67a 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -7,6 +7,15 @@ feature 'Admin polls' do login_as(admin.user) end + scenario 'Index empty' do + visit admin_root_path + within('#side_menu') do + click_link "Polls" + end + + expect(page).to have_content "There are no polls" + end + scenario 'Index' do 3.times { create(:poll) } @@ -24,9 +33,10 @@ feature 'Admin polls' do #expect(page).to have_content "Status/Dates" - Hardcoded end end + expect(page).to_not have_content "There are no polls" end - scenario 'Show', :focus do + scenario 'Show' do poll = create(:poll) visit admin_polls_path @@ -37,4 +47,45 @@ feature 'Admin polls' do #expect(page).to have_content "REFNUM" - Hardcoded end + scenario "Create" do + visit admin_polls_path + click_link "Create poll" + + fill_in "poll_name", with: "Upcoming poll" +#fill_in reference_number - Hardcoded +#fill_in open_date - Hardcoded +#fill_in close_date - Hardcoded + click_button "Create poll" + expect(page).to have_content "Poll created successfully" + + expect(page).to have_content "Upcoming poll" + end + + scenario "Edit" do + poll = create(:poll) + + visit admin_poll_path(poll) + click_link "Edit" +save_and_open_page + fill_in "poll_name", with: "Next Poll" +#fill_in reference_number - Hardcoded +#fill_in open_date - Hardcoded +#fill_in close_date - Hardcoded + click_button "Update poll" + expect(page).to have_content "Poll updated successfully" + + expect(page).to have_content "Next Poll" + end + + scenario 'Edit from index' do + poll = create(:poll) + visit admin_polls_path + + within("#poll_#{poll.id}") do + click_link "Edit" + end + + expect(current_path).to eq(edit_admin_poll_path(poll)) + end + end \ No newline at end of file diff --git a/spec/models/poll_spec.rb b/spec/models/poll_spec.rb new file mode 100644 index 000000000..7442cdc5a --- /dev/null +++ b/spec/models/poll_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +describe :poll do + + let(:poll) { build(:poll) } + + it "should be valid" do + expect(poll).to be_valid + end + + it "should not be valid without a name" do + poll.name = nil + expect(poll).to_not be_valid + end + +end \ No newline at end of file