Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Location model for Item assocation. #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/log/*.log
/tmp

*.swp

# Ignore coverage
coverage/

Expand Down
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM phusion/passenger-ruby21:0.9.12

ENV HOME /root



CMD ["bash"]
CMD ["/sbin/my_init"]

RUN rm -f /etc/service/nginx/down

RUN rm /etc/nginx/sites-enabled/default

ADD nginx.conf /etc/nginx/sites-enabled/webapp.conf

RUN mkdir /home/app/webapp

WORKDIR /tmp
Add Gemfile /tmp/
Add Gemfile.lock /tmp/
RUN bundle install

ADD . /home/app/webapp
RUN cd /home/app/webapp && rake db:migrate

RUN chown app:app -R /home/app/webapp
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*


1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'will_paginate', '~> 3.0'
gem 'will_paginate-bootstrap'
gem 'jquery-rails'
gem 'pundit'
gem 'pg'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ GEM
nokogiri (1.6.5)
mini_portile (~> 0.6.0)
orm_adapter (0.5.0)
pg (0.18.1)
protected_attributes (1.0.8)
activemodel (>= 4.0.1, < 5.0)
pry (0.10.1)
Expand Down Expand Up @@ -267,6 +268,7 @@ DEPENDENCIES
jquery-rails
launchy
mini_magick
pg
protected_attributes
pundit
rails
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/locations.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/locations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Locations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
69 changes: 69 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;
&:visited {
color: #666;
}
&:hover {
color: #fff;
background-color: #000;
}
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
ul li {
font-size: 12px;
list-style: square;
}
}
2 changes: 2 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def show
# GET /items/new
# GET /items/new.json
def new

@item = Item.new

respond_to do |format|
Expand All @@ -38,6 +39,7 @@ def edit
# POST /items
# POST /items.json
def create

@item = Item.new(params[:item])

respond_to do |format|
Expand Down
49 changes: 49 additions & 0 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class LocationsController < ApplicationController
before_action :set_location, only: [:show, :edit, :update, :destroy]

respond_to :html

def index
@locations = Location.all
respond_with(@locations)
end

def show

@items = @location.items
respond_with(@location, @items)
end

def new
@location = Location.new
respond_with(@location)
end

def edit
end

def create
@location = Location.new(location_params)
@location.save
respond_with(@location)
end

def update
@location.update(location_params)
respond_with(@location)
end

def destroy
@location.destroy
respond_with(@location)
end

private
def set_location
@location = Location.find(params[:id])
end

def location_params
params.require(:location).permit(:name)
end
end
2 changes: 2 additions & 0 deletions app/helpers/locations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LocationsHelper
end
4 changes: 3 additions & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
class Item < ActiveRecord::Base
mount_uploader :picture, PictureUploader
attr_accessible :description, :location, :name, :picture, :product_model_number, :vendor_part_number, :quantity, :unit_value, :vendor_url, :value, :vendor_name, :category
attr_accessible :description, :location, :name, :picture, :product_model_number, :vendor_part_number, :quantity, :unit_value, :vendor_url, :value, :vendor_name, :category, :location_id

self.per_page = 25

belongs_to :item
belongs_to :location
after_save :update_value
accepts_nested_attributes_for :location
protected

def update_value
Expand Down
6 changes: 6 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Location < ActiveRecord::Base

attr_accessible :name

has_many :items
end
Binary file added app/views/items/._form.html.erb.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion app/views/items/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<div class="form-group">
<%= f.label :location, class: "col-sm-3 control-label" %>
<div class="col-sm-5">
<%= f.text_field :location, class: "form-control" %>
<%= collection_select(:item, :location_id, Location.all, :id, :name, {:selected => @item.location(:id), prompt: @item.location(:id) }) %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<td><center><%= number_to_currency(item.unit_value, :unit => "$") %></center></td>
<td><center><%= number_to_currency(item.value, :unit => "$") %></center></td>
<td><center><%= item.vendor_name %></center></td>
<td><center><%= item.location %></center></td>
<td><center><%= item.location.name %></center></td>
<td><center><%= item.category %></center></td>
<td><center><%= link_to '', edit_item_path(item), :class=>"fa fa-edit" %> | <%= link_to content_tag(:i,nil, :class=>"fa fa-trash-o"), item, data: {confirm: 'Are you sure you want to delete this item?'}, method: :delete %></center>
</td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/items/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<div class="row">
<div class="col-md-2"><b>Location</b></div>
<div class="col-md-4"><%= @item.location %></div>
<div class="col-md-4"><%= @item.location.name %></div>
</div>

<div class="row">
Expand Down
21 changes: 21 additions & 0 deletions app/views/locations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_for(@location) do |f| %>
<% if @location.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:</h2>

<ul>
<% @location.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/locations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Location</h1>

<%= render 'form' %>

<%= link_to 'Show', @location %> |
<%= link_to 'Back', locations_path %>
27 changes: 27 additions & 0 deletions app/views/locations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<p id="notice"><%= notice %></p>

<h1>Listing Locations</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @locations.each do |location| %>
<tr>
<td><%= location.name %></td>
<td><%= link_to 'Show', location %></td>
<td><%= link_to 'Edit', edit_location_path(location) %></td>
<td><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Location', new_location_path %>
5 changes: 5 additions & 0 deletions app/views/locations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Location</h1>

<%= render 'form' %>

<%= link_to 'Back', locations_path %>
18 changes: 18 additions & 0 deletions app/views/locations/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @location.name %>
</p>

<p>
<strong>Items in <%= @location.name %> </strong>

<ul>
<% @items.each do |i| %>
<li><%= i.name %> </li>
<% end %>
</ul>

<%= link_to 'Edit', edit_location_path(@location) %> |
<%= link_to 'Back', locations_path %>
9 changes: 9 additions & 0 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
<li><%= link_to 'New Item', new_item_path %></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Locations <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><%= link_to 'All Locations', locations_path %></li>
<li><%= link_to 'New Location', new_location_path %></li>
</ul>
</li>


</ul>

<ul class="nav navbar-nav navbar-right">
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Rims::Application.routes.draw do

get "welcome/index"
devise_for :users

root :to => 'welcome#index'
#root :to => redirect('/items')
resources :items
resources :locations
resources :users
end
9 changes: 9 additions & 0 deletions db/migrate/20150310173115_add_location_to_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddLocationToItem < ActiveRecord::Migration
def change
remove_column :items, :location
add_reference :items, :location, index: true
add_foreign_key :items, :locations


end
end
9 changes: 9 additions & 0 deletions db/migrate/20150310173414_create_locations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateLocations < ActiveRecord::Migration
def change
create_table :locations do |t|
t.string :name

t.timestamps null: false
end
end
end
Loading