From 2db13f92f14ca273af0795417fc5bb09588b015d Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 1 Sep 2015 23:07:53 -0500 Subject: [PATCH] Locations abstraction --- app/controllers/equipment_controller.rb | 3 +- app/controllers/locations_controller.rb | 98 +------------------ app/views/locations/_list.html.erb | 2 +- app/views/locations/edit.html.erb | 6 +- app/views/locations/index.html.erb | 4 +- app/views/locations/new.html.erb | 4 +- app/views/locations/show.html.erb | 1 + test/controllers/locations_controller_test.rb | 6 +- 8 files changed, 15 insertions(+), 109 deletions(-) diff --git a/app/controllers/equipment_controller.rb b/app/controllers/equipment_controller.rb index 50f9a393..f0c08839 100644 --- a/app/controllers/equipment_controller.rb +++ b/app/controllers/equipment_controller.rb @@ -15,8 +15,7 @@ class EquipmentController < ContentController def populate_universe_fields @universe = Universe.where(user_id: session[:user], name: params[:universe].strip).first - @equipment = - @equipment.where(universe_id: @universe.id) if @equipment.blank? + @equipment = @equipment.where(universe_id: @universe.id) if @equipment.blank? end def universe_from_equipment_params diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 92574dfd..b6a18f48 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -1,101 +1,8 @@ # Controller for the Location model class LocationsController < ContentController - - def index - @locations = Location.where(user_id: session[:user]) - .order(:name).presence || [] - - respond_to do |format| - format.html # index.html.erb - format.json { render json: @locations } - end - end - - def show - @location = Location.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.json { render json: @location } - end - end - - def new - @location = Location.new - - respond_to do |format| - format.html # new.html.erb - format.json { render json: @location } - end - end - - def edit - @location = Location.find(params[:id]) - end - - # rubocop:disable LineLength - def create - @location = create_location_from_params - - respond_to do |format| - begin - if @location.save - notice = t(:create_success, model_name: Location.model_name.human) if notice.blank? - format.html { redirect_to @location, notice: notice } - format.json { render json: @location, status: :created, location: @location } - else - format.html { render action: 'new' } - format.json { render json: @location.errors, status: :unprocessable_entity } - end - rescue Errno::ECONNRESET - # Connection was reset, probably because of the file upload. - # Try again without it. - @location.map = nil - notice = t :location_create_upload_map_error - retry - end - end - end - # rubocop:enable LineLength - - # rubocop:disable LineLength - def update - @location = update_location_from_params - - respond_to do |format| - begin - if @location.update_attributes(location_params) - notice = t :update_success, model_name: Location.model_name.human if notice.blank? - format.html { redirect_to @location, notice: notice } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @location.errors, status: :unprocessable_entity } - end - rescue Errno::ECONNRESET - # Connection was reset, probably because of the file upload. - # Try again without it. - @location.map = nil - notice = t :location_update_upload_map_error - retry - end - end - end - # rubocop:enable LineLength - - def destroy - @location = Location.find(params[:id]) - @location.destroy - - respond_to do |format| - format.html { redirect_to location_list_url } - format.json { head :no_content } - end - end - private - def location_params + def content_params params.require(:location).permit( :universe_id, :user_id, :name, :type_of, :description, :map, :population, :currency, :motto, :capital, :largest_city, :notable_cities, @@ -116,7 +23,6 @@ class LocationsController < ContentController end def universe_from_location_params - Universe.where(user_id: session[:user], - name: params[:location][:universe].strip).first + Universe.where(user_id: session[:user], name: params[:location][:universe].strip).first end end diff --git a/app/views/locations/_list.html.erb b/app/views/locations/_list.html.erb index 98cea567..97cef6f9 100644 --- a/app/views/locations/_list.html.erb +++ b/app/views/locations/_list.html.erb @@ -1,4 +1,4 @@ -<% @locations.each do |location| %> +<% @content.each do |location| %>

<%= location.name %>

diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb index 3b5320a3..7571a822 100644 --- a/app/views/locations/edit.html.erb +++ b/app/views/locations/edit.html.erb @@ -1,4 +1,4 @@ -<%- model_class = @location.class -%> +<%- model_class = @content.class -%>
@@ -20,8 +20,8 @@
-

Editing <%= @location.name %>

- <%= form_for @location, +

Editing <%= @content.name %>

+ <%= form_for @content, :url => location_edit_process_path, :html => { :class => 'form-horizontal' } do |form| %>
diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index f19b53fe..b7b23c9b 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -1,8 +1,8 @@ -<%- model_class = Location.new.class -%> +<%- model_class = @content.class -%>
-<% if @locations.length > 0 %> +<% if @content.length > 0 %>

diff --git a/app/views/locations/new.html.erb b/app/views/locations/new.html.erb index 7779fb0f..8a2ee3f7 100644 --- a/app/views/locations/new.html.erb +++ b/app/views/locations/new.html.erb @@ -1,4 +1,4 @@ -<%- model_class = @location.class -%> +<%- model_class = @content.class -%>
@@ -10,7 +10,7 @@
- <%= form_for @location, + <%= form_for @content, :url => location_create_process_path, :html => { :class => 'form-horizontal' } do |form| %> diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index 1f059717..bace461b 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -1,3 +1,4 @@ +<% @location = @content %> <%- model_class = @location.class -%>
diff --git a/test/controllers/locations_controller_test.rb b/test/controllers/locations_controller_test.rb index 2f74839f..3ecc6bcd 100644 --- a/test/controllers/locations_controller_test.rb +++ b/test/controllers/locations_controller_test.rb @@ -12,7 +12,7 @@ class LocationsControllerTest < ActionController::TestCase test 'should get index' do get :index assert_response :success - assert_not_nil assigns(:locations) + assert_not_nil assigns(:content) end test 'should get new' do @@ -29,7 +29,7 @@ class LocationsControllerTest < ActionController::TestCase } end - assert_redirected_to location_path(assigns(:location)) + assert_redirected_to location_path(assigns(:content)) end test 'should show location' do @@ -75,7 +75,7 @@ class LocationsControllerTest < ActionController::TestCase } end - assert_redirected_to location_path(assigns(:location)) + assert_redirected_to location_path(assigns(:content)) end test 'should reject images with an invalid type' do