<%= @app_name %>::<%= @admin_name %>.controllers :<%= @orm.name_plural %> do
  get :index do
    @title = '<%= @orm.name_plural.capitalize %>'
    @<%= @orm.name_plural %> = <%= @orm.all %>
    render '<%= @orm.name_plural %>/index'
  end

  get :new do
    @title = pat(:new_title, model: '<%= @orm.name_singular %>')
    @<%= @orm.name_singular %> = <%= @orm.build %>
    render '<%= @orm.name_plural %>/new'
  end

  post :create do
    @<%= @orm.name_singular %> = <%= @orm.build("params[:#{@orm.name_param}]") %>
    if <%= @orm.save %>
      @title = pat(:create_title, model: "<%= @orm.name_singular %> #{@<%= @orm.name_singular %>.id}")
      flash[:success] = pat(:create_success, model: '<%= @orm.klass_name %>')
      params[:save_and_continue] ? redirect(url(:<%= @orm.name_plural %>, :index)) : redirect(url(:<%= @orm.name_plural %>, :edit, id: @<%= @orm.name_singular %>.id))
    else
      @title = pat(:create_title, model: '<%= @orm.name_singular %>')
      flash.now[:error] = pat(:create_error, model: '<%= @orm.name_singular %>')
      render '<%= @orm.name_plural %>/new'
    end
  end

  get :edit, with: :id do
    @title = pat(:edit_title, model: "<%= @orm.name_singular %> #{params[:id]}")
    @<%= @orm.name_singular %> = <%= @orm.find("params[:id]") %>
    if @<%= @orm.name_singular %>
      render '<%= @orm.name_plural %>/edit'
    else
      flash[:warning] = pat(:create_error, model: '<%= @orm.name_singular %>', id: params[:id])
      halt 404
    end
  end

  put :update, with: :id do
    @title = pat(:update_title, model: "<%= @orm.name_singular %> #{params[:id]}")
    @<%= @orm.name_singular %> = <%= @orm.find("params[:id]") %>
    if @<%= @orm.name_singular %>
      if <%= @orm.update_attributes("params[:#{@orm.name_param}]") %>
        flash[:success] = pat(:update_success, model: '<%= @orm.name_singular.capitalize %>', id: params[:id])
        params[:save_and_continue] ?
          redirect(url(:<%= @orm.name_plural %>, :index)) :
          redirect(url(:<%= @orm.name_plural %>, :edit, id: @<%= @orm.name_singular %>.id))
      else
        flash.now[:error] = pat(:update_error, model: '<%= @orm.name_singular %>')
        render '<%= @orm.name_plural %>/edit'
      end
    else
      flash[:warning] = pat(:update_warning, model: '<%= @orm.name_singular %>', id: params[:id])
      halt 404
    end
  end

  delete :destroy, with: :id do
    @title = '<%= @orm.name_plural.capitalize %>'
    <%= @orm.name_singular %> = <%= @orm.find("params[:id]") %>
    if <%= @orm.name_singular %>
      if <%= @orm.destroy %>
        flash[:success] = pat(:delete_success, model: '<%= @orm.name_singular.capitalize %>', id: params[:id])
      else
        flash[:error] = pat(:delete_error, model: '<%= @orm.name_singular %>')
      end
      redirect url(:<%= @orm.name_plural %>, :index)
    else
      flash[:warning] = pat(:delete_warning, model: '<%= @orm.name_singular %>', id: params[:id])
      halt 404
    end
  end

  delete :destroy_many do
    @title = '<%= @orm.name_plural.capitalize %>'
    unless params[:<%= @orm.name_singular %>_ids]
      flash[:error] = pat(:destroy_many_error, model: '<%= @orm.name_singular %>')
      redirect(url(:<%= @orm.name_plural %>, :index))
    end
    ids = params[:<%= @orm.name_param %>_ids].split(',').map(&:strip)
    <%= @orm.name_plural %> = <%= @orm.find_by_ids("ids") %>
    <% if @orm.name_singular == @admin_model %>
    if <%= @orm.name_plural %>.include? current_account
      flash[:error] = pat(:delete_error, model: '<%= @orm.name_singular %>')
    elsif <%= @orm.multiple_destroy(@orm.name_plural) %>
    <% else %>
    if <%= @orm.multiple_destroy(@orm.name_plural) %>
    <% end %>
      flash[:success] = pat(:destroy_many_success, model: '<%= @orm.name_plural.capitalize %>', ids: ids.join(', '))
    end
    redirect url(:<%= @orm.name_plural %>, :index)
  end
end
