Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use respond_with for EventsController#edit. [#64]

  • Loading branch information...
commit 90ea857d5fe1a3e5ac8b5faa026b6fece7ba20f2 1 parent 5704bfc
@marnen authored
View
29 app/controllers/events_controller.rb
@@ -27,30 +27,31 @@ def index
def new
@page_title = _("Add event")
@event = Event.new
+ respond_with @event
end
def create
@event = Event.new params[:event]
- with_flash { @event.save }
+ respond_with_flash { @event.save }
+ end
+
+ def edit
+ @event = Event.find params[:id]
+ if @event.allow?(:edit)
+ @page_title = _("Edit event")
+ respond_with @event
+ else
+ redirect_to({action: :index}, flash: {error: _("You are not authorized to edit that event.")})
+ end
end
def update
@event = Event.find params[:id]
- with_flash { @event.update_attributes params[:event] }
+ respond_with_flash { @event.update_attributes params[:event] }
end
make_resourceful do
- actions :edit, :show
-
- response_for :edit do
- if !current_object.allow?(:edit)
- flash[:error] = _("You are not authorized to edit that event.")
- redirect_to :action => :index
- else
- @page_title = _("Edit event")
- render :action => 'new'
- end
- end
+ actions :show
response_for :show do
if !current_object.allow?(:show)
@@ -183,7 +184,7 @@ def set_table_headers
@search = params[:search].extend(Search) if params[:search]
end
- def with_flash
+ def respond_with_flash
raise ArgumentError, 'no block specified' unless block_given?
if yield
View
45 app/views/events/_form.html.haml
@@ -0,0 +1,45 @@
+= form_for event do |f|
+ %table.edit
+ - if current_user.calendars.size > 1
+ %tr
+ %th= _("Calendar")
+ %td= f.collection_select :calendar_id, User.current_user.calendars.sort{|x, y| x.name <=> y.name}, :id, :name
+ - else
+ = f.hidden_field :calendar_id, :value => User.current_user.calendars[0].id
+ %tr
+ %th= _("Event name")
+ %td
+ = f.text_field :name
+ = _(error_message_on(:event, :name)) # TODO: Do we need the :event now?
+ %tr
+ %th
+ = _("Description")
+ %br
+ = markdown_hint
+ %td= f.text_area :description, :cols => 40, :rows => 6
+ %tr
+ %th= _("Date")
+ %td
+ = f.date_select :date, :order => [:day, :month, :year], :start_year => 2006
+ = _(error_message_on(:event, :date))
+ %tr
+ %th= _("Site")
+ %td= f.text_field :site
+ %tr
+ %th= _("Street address")
+ %td
+ = f.text_field :street
+ %br
+ = f.text_field :street2
+ %tr
+ %th= _("City")
+ %td= f.text_field :city
+ %tr
+ %th= _("State")
+ %td
+ = f.collection_select :state_id, Acts::Addressed::State.find(:all), :id, :name
+ = _(error_message_on :event, :state)
+ %tr
+ %th= _("Zip")
+ %td= f.text_field :zip
+ = submit_tag _("Save changes")
View
1  app/views/events/edit.html.haml
@@ -0,0 +1 @@
+= render partial: 'form', locals: {event: @event}
View
46 app/views/events/new.html.haml
@@ -1,45 +1 @@
-= form_for @event do |f|
- %table.edit
- - if current_user.calendars.size > 1
- %tr
- %th= _("Calendar")
- %td= f.collection_select :calendar_id, User.current_user.calendars.sort{|x, y| x.name <=> y.name}, :id, :name
- - else
- = f.hidden_field :calendar_id, :value => User.current_user.calendars[0].id
- %tr
- %th= _("Event name")
- %td
- = f.text_field :name
- = _(error_message_on(:event, :name)) # TODO: Do we need the :event now?
- %tr
- %th
- = _("Description")
- %br
- = markdown_hint
- %td= f.text_area :description, :cols => 40, :rows => 6
- %tr
- %th= _("Date")
- %td
- = f.date_select :date, :order => [:day, :month, :year], :start_year => 2006
- = _(error_message_on(:event, :date))
- %tr
- %th= _("Site")
- %td= f.text_field :site
- %tr
- %th= _("Street address")
- %td
- = f.text_field :street
- %br
- = f.text_field :street2
- %tr
- %th= _("City")
- %td= f.text_field :city
- %tr
- %th= _("State")
- %td
- = f.collection_select :state_id, Acts::Addressed::State.find(:all), :id, :name
- = _(error_message_on :event, :state)
- %tr
- %th= _("Zip")
- %td= f.text_field :zip
- = submit_tag _("Save changes")
+= render partial: 'form', locals: {event: @event}
View
5 spec/controllers/events_controller_spec.rb
@@ -300,11 +300,6 @@
response.should redirect_to(:action => :index)
end
- it "should reuse the new-event form" do
- get 'edit', :id => @event.id
- response.should render_template(:new)
- end
-
it "should set the page title" do
get 'edit', :id => @event.id
assigns[:page_title].should_not be_nil
Please sign in to comment.
Something went wrong with that request. Please try again.