From a8daa1a9bd3d87eb8183c5fbbd23f5f6f9ab545b Mon Sep 17 00:00:00 2001 From: Bryan Taylor Date: Tue, 10 Mar 2015 15:32:14 -0400 Subject: [PATCH 1/2] Added Location Model Items can be viewed by Item Location Locations can be selected by collection_select although, the :selected method does not work properly when editing an item to show what location is already selected. --- Dockerfile | 29 ++++ Gemfile | 1 + Gemfile.lock | 2 + app/assets/javascripts/locations.coffee | 3 + app/assets/stylesheets/locations.scss | 3 + app/assets/stylesheets/scaffolds.scss | 69 ++++++++ app/controllers/.locations_controller.rb.swp | Bin 0 -> 12288 bytes app/controllers/items_controller.rb | 2 + app/controllers/locations_controller.rb | 49 ++++++ app/helpers/locations_helper.rb | 2 + app/models/item.rb | 4 +- app/models/location.rb | 6 + app/views/items/._form.html.erb.swp | Bin 0 -> 20480 bytes app/views/items/_form.html.erb | 2 +- app/views/items/index.html.erb | 2 +- app/views/items/show.html.erb | 2 +- app/views/locations/_form.html.erb | 21 +++ app/views/locations/edit.html.erb | 6 + app/views/locations/index.html.erb | 27 +++ app/views/locations/new.html.erb | 5 + app/views/locations/show.html.erb | 18 ++ app/views/shared/_header.html.erb | 9 + config/routes.rb | 2 + .../20150310173115_add_location_to_item.rb | 9 + db/migrate/20150310173414_create_locations.rb | 9 + db/schema.rb | 12 +- nginx.conf | 11 ++ spec/controllers/locations_controller_spec.rb | 159 ++++++++++++++++++ spec/factories/locations.rb | 6 + spec/models/location_spec.rb | 5 + spec/requests/locations_spec.rb | 10 ++ 31 files changed, 479 insertions(+), 6 deletions(-) create mode 100644 Dockerfile create mode 100644 app/assets/javascripts/locations.coffee create mode 100644 app/assets/stylesheets/locations.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/.locations_controller.rb.swp create mode 100644 app/controllers/locations_controller.rb create mode 100644 app/helpers/locations_helper.rb create mode 100644 app/models/location.rb create mode 100644 app/views/items/._form.html.erb.swp create mode 100644 app/views/locations/_form.html.erb create mode 100644 app/views/locations/edit.html.erb create mode 100644 app/views/locations/index.html.erb create mode 100644 app/views/locations/new.html.erb create mode 100644 app/views/locations/show.html.erb create mode 100644 db/migrate/20150310173115_add_location_to_item.rb create mode 100644 db/migrate/20150310173414_create_locations.rb create mode 100644 nginx.conf create mode 100644 spec/controllers/locations_controller_spec.rb create mode 100644 spec/factories/locations.rb create mode 100644 spec/models/location_spec.rb create mode 100644 spec/requests/locations_spec.rb diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..00c3614 --- /dev/null +++ b/Dockerfile @@ -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/* + + diff --git a/Gemfile b/Gemfile index 9d9cd02..5dfe2fc 100755 --- a/Gemfile +++ b/Gemfile @@ -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. diff --git a/Gemfile.lock b/Gemfile.lock index 41afb60..6db0c59 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -267,6 +268,7 @@ DEPENDENCIES jquery-rails launchy mini_magick + pg protected_attributes pundit rails diff --git a/app/assets/javascripts/locations.coffee b/app/assets/javascripts/locations.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/locations.coffee @@ -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/ diff --git a/app/assets/stylesheets/locations.scss b/app/assets/stylesheets/locations.scss new file mode 100644 index 0000000..b6e6e3a --- /dev/null +++ b/app/assets/stylesheets/locations.scss @@ -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/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -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; + } +} diff --git a/app/controllers/.locations_controller.rb.swp b/app/controllers/.locations_controller.rb.swp new file mode 100644 index 0000000000000000000000000000000000000000..6965981aee5d2d843248b5fab7a94d6aed8e7158 GIT binary patch literal 12288 zcmeI2Id2m|7=|Z=DiDs)1ghbbBh1<+k$@FQi6{UGI-m)WHTKTNEB0b$oY+EwE)fO4 zftms;dI}^OBm@Pbn+tAOHQxf)%Z}t$ezbBp>6UX_HxjPd@ z(92|i43GgbKnBPF86X2>fDG(x116nfk1>Tk`7|1NJ2lqkm(-8}GC&5%02v?yWPl8i z0Wv@a$N(8213S=w@EH5DkFnSLQ9Ss02v?yWPl8i0Wv@a$iS{KAY+el97W4{DwK${qvV5bpk&1@ zHWV*7sZ>#5DsC(yxiIMENuL+bn^{LT7PnkH=U4d6d~dSj2eDVl-d}YCZ*7>SrD+yD zkd&%m1Dm03Xi=e(I!$7)xfz&FrA*Hf%1g(S8e5|K=x1vzt|pG@rXGf`6cy!jw1e5$ z?CIpiX|W*@!FgHlsGL}C{%OFrlE2xoMMy6&6PGYHUOpd;{O_bHUk;3nbZ&fbj<|9v z&ft#CvDN8L@*kN+Sm9ZZ)+fR+55uaFJ)2yt0h4gIW1`UBYe_#*vMJhj_d9n-7gCic vaoBhHs%y)sa@RUTlYM_U$=AmkYllMXa-Hg{<;Hq`nXja27-XC4hxgep3+!3R literal 0 HcmV?d00001 diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 3d2c120..26be1a0 100755 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -22,6 +22,7 @@ def show # GET /items/new # GET /items/new.json def new + @item = Item.new respond_to do |format| @@ -38,6 +39,7 @@ def edit # POST /items # POST /items.json def create + @item = Item.new(params[:item]) respond_to do |format| diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb new file mode 100644 index 0000000..2eeac23 --- /dev/null +++ b/app/controllers/locations_controller.rb @@ -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 diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb new file mode 100644 index 0000000..46f9428 --- /dev/null +++ b/app/helpers/locations_helper.rb @@ -0,0 +1,2 @@ +module LocationsHelper +end diff --git a/app/models/item.rb b/app/models/item.rb index a4d846f..d99e9fb 100755 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/app/models/location.rb b/app/models/location.rb new file mode 100644 index 0000000..41b2b4e --- /dev/null +++ b/app/models/location.rb @@ -0,0 +1,6 @@ +class Location < ActiveRecord::Base + +attr_accessible :name + +has_many :items +end diff --git a/app/views/items/._form.html.erb.swp b/app/views/items/._form.html.erb.swp new file mode 100644 index 0000000000000000000000000000000000000000..a25e41961b358272ac9740b946c5cd84ab453873 GIT binary patch literal 20480 zcmeI2UyLM09mgBb$blXhPEp^AKJNC;%%9y`E?$P&Rc{xO2#A>6*Z=PHw{N(KeW@|);rD6JJA2)^o4=sAxAb|>>l(hUbV4E9 z{`x9dyJmXkn#S6Cmf|nrMyxe@Ivk7jU2CwJaH-N}BEz)Fm+Op zO{f|o>5dt2PN#rV;QSQGOuv47pErASW||Bf@ULR~ulUya*>bU*0!{&^fK$LJ;1qBQ zI0c*nPJs(j0Tb`>?nTN!W>ddtuNRG7KW^QJ?DcD7;d`w6QhU8~EZjQX#VOzva0)mD zoB~b(r+`zyDc}@v3OEIv0#1Sdg91G8yca*=dH=c?FUS6WIsZR*x#ztDegl39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eePVzal}s7+j;xRFkaaQP8nBeFh@jPO!f^hMe7qrpxxz+AtYP>U zjQE?2Ol4%dQ@0VjI=8pWExO-Ng{jk?MrA9c`rznh z2F?F3!ru3@fad>)Y=OOpdHp}Y-@&Wk0g!>?pboAE2f$wNGS&~C0>8i>{e$2~;C}E! z@O7{sTnzquPt3OEIv0!{&^ zfK$LJurmtSap-n~(~)hn$|1asvsmuHpF8ju)!l(VdjOEmp;eCfxdVUhz~8PP_?vX7 RV$uo1^FLa#*(=yC <%= f.label :location, class: "col-sm-3 control-label" %>
- <%= f.text_field :location, class: "form-control" %> + <%= collection_select(:item, :location_id, Location.all, :id, :name, {:selected => @item.location(:id), prompt: @item.location(:id) }) %>
diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb index 40d66e1..625014d 100755 --- a/app/views/items/index.html.erb +++ b/app/views/items/index.html.erb @@ -29,7 +29,7 @@
<%= number_to_currency(item.unit_value, :unit => "$") %>
<%= number_to_currency(item.value, :unit => "$") %>
<%= item.vendor_name %>
-
<%= item.location %>
+
<%= item.location.name %>
<%= item.category %>
<%= 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 %>
diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb index 01060be..756fdad 100755 --- a/app/views/items/show.html.erb +++ b/app/views/items/show.html.erb @@ -55,7 +55,7 @@
Location
-
<%= @item.location %>
+
<%= @item.location.name %>
diff --git a/app/views/locations/_form.html.erb b/app/views/locations/_form.html.erb new file mode 100644 index 0000000..7dc7fca --- /dev/null +++ b/app/views/locations/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_for(@location) do |f| %> + <% if @location.errors.any? %> +
+

<%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:

+ +
    + <% @location.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb new file mode 100644 index 0000000..89278ed --- /dev/null +++ b/app/views/locations/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Location

+ +<%= render 'form' %> + +<%= link_to 'Show', @location %> | +<%= link_to 'Back', locations_path %> diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb new file mode 100644 index 0000000..dc3e09c --- /dev/null +++ b/app/views/locations/index.html.erb @@ -0,0 +1,27 @@ +

<%= notice %>

+ +

Listing Locations

+ + + + + + + + + + + <% @locations.each do |location| %> + + + + + + + <% end %> + +
Name
<%= location.name %><%= link_to 'Show', location %><%= link_to 'Edit', edit_location_path(location) %><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Location', new_location_path %> diff --git a/app/views/locations/new.html.erb b/app/views/locations/new.html.erb new file mode 100644 index 0000000..bdb0925 --- /dev/null +++ b/app/views/locations/new.html.erb @@ -0,0 +1,5 @@ +

New Location

+ +<%= render 'form' %> + +<%= link_to 'Back', locations_path %> diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb new file mode 100644 index 0000000..d2646c9 --- /dev/null +++ b/app/views/locations/show.html.erb @@ -0,0 +1,18 @@ +

<%= notice %>

+ +

+ Name: + <%= @location.name %> +

+ +

+Items in <%= @location.name %> + +

    +<% @items.each do |i| %> +
  • <%= i.name %>
  • +<% end %> +
+ +<%= link_to 'Edit', edit_location_path(@location) %> | +<%= link_to 'Back', locations_path %> diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index b5e1277..cb49126 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -24,6 +24,15 @@
  • <%= link_to 'New Item', new_item_path %>
  • + + +