Skip to content

Commit

Permalink
Use respond_with for CalendarsController#create. [#64]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Dec 29, 2011
1 parent 6be8231 commit 8ffe513
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions app/controllers/calendars_controller.rb
Expand Up @@ -20,27 +20,18 @@ def edit
respond_with @calendar
end

make_resourceful do
actions :create, :update

# TODO: Shouldn't this be replaced by Calendar#set_admin ?
after :create do
p = User.current_user.permissions
@admin ||= Role.find_or_create_by_name('admin')
if !p.find_by_calendar_id_and_role_id(current_object.id, @admin.id)
p << Permission.create!(:user => User.current_user, :calendar => current_object, :role => @admin)
end
end

response_for :create do
flash[:notice] = _('Your calendar was successfully created.')
redirect_to '/admin'
def create
@calendar = Calendar.new params[:calendar]
if @calendar.save!
make_admin_permission_for @calendar
redirect_to '/admin', notice: _('Your calendar was successfully created.')
else
redirect_to :back, error: _("Couldn't create your calendar!")
end
end

response_for :create_fails do
flash[:error] = _('Couldn\'t create your calendar!')
redirect_to :back
end
make_resourceful do
actions :update

response_for :update do
flash[:notice] = _('Your calendar was successfully saved.')
Expand All @@ -58,4 +49,14 @@ def users
@page_title = _('Users for calendar %{calendar_name}') % {:calendar_name => current_object}
@users = current_object.users.find(:all, :order => 'lastname, firstname')
end

private

def make_admin_permission_for(calendar)
p = User.current_user.permissions
@admin ||= Role.find_or_create_by_name('admin')
if !p.find_by_calendar_id_and_role_id(calendar.id, @admin.id)
p << Permission.create!(:user => User.current_user, :calendar => calendar, :role => @admin)
end
end
end

0 comments on commit 8ffe513

Please sign in to comment.