Skip to content

Commit

Permalink
Add staff member editing of question
Browse files Browse the repository at this point in the history
Closes #407
  • Loading branch information
walter committed Apr 28, 2014
1 parent 3758f85 commit f4bc486
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
25 changes: 20 additions & 5 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ class QuestionsController < ApplicationController
belongs_to :jurisdiction, parent_class: Metadatum, finder: :find_by_abbreviation, param: :jurisdiction
respond_to :html
respond_to :js, only: :index
actions :index, :show, :new, :create, :update, :destroy
actions :index, :show, :new, :create, :edit, :update, :destroy
custom_actions resource: [:need_signatures, :have_answers, :need_answers, :recent]

before_filter :set_state_code, only: [:show, :new, :create, :need_signatures,
:have_answers, :need_answers, :recent, :update,
:destroy]
:have_answers, :need_answers, :recent,
:edit, :update, :destroy]
before_filter :set_question_person_id, only: :create
before_filter :set_is_unaffiliated, only: [:index, :show, :need_signatures,
:have_answers, :need_answers, :recent]
before_filter :redirect_to_unaffiliated_route_if_necessary, only: [:index, :show]
before_filter :authenticate_user!, only: [:destroy, :update]
before_filter :check_can_manage_question, only: [:destroy, :update]
before_filter :authenticate_user!, only: [:edit, :destroy, :update]
before_filter :check_can_manage_question, only: [:edit, :destroy, :update]
before_filter :set_person, only: [:index, :need_signatures, :have_answers,
:need_answers, :recent]
before_filter :set_is_national, only: [:index, :need_signatures, :have_answers,
Expand Down Expand Up @@ -137,6 +137,17 @@ def create
end
end

def edit
set_up_steps

@question = Question.connected_to(@state_code).find(params[:id])
@user = @question.user
@bill = @question.bill
@person = @question.person

render layout: "data_collection"
end

def update
update!(notice: "Question updated") do |format|
format.html { redirect_to question_path(@question.state, @question) }
Expand Down Expand Up @@ -181,6 +192,10 @@ def set_up_steps
# user is logged in (no sign_up step)
# person is passed in (no recipient step)
def relevant_steps
if %w(edit update).include?(params[:action])
return @relevant_steps = ["content"]
end

@relevant_steps = %w(recipient content sign_up confirm)
@relevant_steps.delete("recipient") if params[:person]
@relevant_steps.delete("sign_up") if user_signed_in?
Expand Down
2 changes: 1 addition & 1 deletion app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def answered?
# i.e. does belongs_to now work
# @return [Bill] the bill the question is about
def bill
Bill.find(bill_id)
Bill.where(id: bill_id).first
end

# @todo delete if unnecessary with only 1 db
Expand Down
13 changes: 13 additions & 0 deletions app/views/questions/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<section class="question ask">
<%= form_for @question, url: question_path(@state_code, @question), validate: true, html: { accept_charset: "utf-8" } do |f| %>
<% @question.errors.full_messages.each do |message| -%>
<p class="alert"><%= message -%></p>
<% end -%>

<% locals = { jurisdiction: @jurisdiction, question: @question, person: @person, bill: @bill, relevant_steps: @relevant_steps, f: f } -%>

<%= render partial: "content_step", locals: locals.merge({ step: "content" }) %>

<div class="controls"><%= f.submit "Update", class: "cta-pill" %></div>
<% end %>
</section>
3 changes: 3 additions & 0 deletions app/views/questions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
<%- end %>
<%- end %>
</li>
<li>
<%= link_to "Edit", edit_question_path(@jurisdiction, @question), class: "cta-pill" %>
</li>
<li>
<%= button_to "Delete", question_path(@jurisdiction, @question),
method: :delete, data: { confirm: "Are you sure?" }, class: "cta-pill" %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
end
end

resources :questions, only: [:index, :show, :new, :create, :update, :destroy] do
resources :questions, only: [:index, :show, :new, :create, :edit, :update, :destroy] do
collection do
get 'preview'
get 'need_signatures'
Expand Down

0 comments on commit f4bc486

Please sign in to comment.