Locations abstraction

This commit is contained in:
Andrew Brown 2015-09-01 23:07:53 -05:00
parent 3a98e48bf5
commit 2db13f92f1
8 changed files with 15 additions and 109 deletions

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,4 @@
<% @locations.each do |location| %>
<% @content.each do |location| %>
<div class="col-md-4">
<div class="card">
<h1 class="card-heading"><%= location.name %></h1>

View File

@ -1,4 +1,4 @@
<%- model_class = @location.class -%>
<%- model_class = @content.class -%>
<div class="row">
<div class="col-xs-3">
@ -20,8 +20,8 @@
<div class="col-xs-8">
<div class="card">
<h1 class="card-heading">Editing <%= @location.name %></h1>
<%= form_for @location,
<h1 class="card-heading">Editing <%= @content.name %></h1>
<%= form_for @content,
:url => location_edit_process_path,
:html => { :class => 'form-horizontal' } do |form| %>
<div class="card-body">

View File

@ -1,8 +1,8 @@
<%- model_class = Location.new.class -%>
<%- model_class = @content.class -%>
<div class="row">
<% if @locations.length > 0 %>
<% if @content.length > 0 %>
<div class="col-md-12">
<div class="card">
<h1 class="card-heading">

View File

@ -1,4 +1,4 @@
<%- model_class = @location.class -%>
<%- model_class = @content.class -%>
<div class="row">
<div class="col-xs-3">
@ -10,7 +10,7 @@
<div class="col-md-9">
<div class="card">
<%= form_for @location,
<%= form_for @content,
:url => location_create_process_path,
:html => { :class => 'form-horizontal' } do |form| %>

View File

@ -1,3 +1,4 @@
<% @location = @content %>
<%- model_class = @location.class -%>
<div class="row">

View File

@ -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