diff --git a/Gemfile b/Gemfile index fdc8529d..6375b32b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' - +ruby "2.2.3" # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.4' @@ -22,6 +22,9 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +# Include Bootstrap +gem 'bootstrap-sass', '~> 3.3.5' + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index eb9e0d41..9653a63e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,12 +37,18 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) + autoprefixer-rails (6.1.0.1) + execjs + json better_errors (2.1.1) coderay (>= 1.0.0) erubis (>= 2.6.6) rack (>= 0.9.0) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) + bootstrap-sass (3.3.5.1) + autoprefixer-rails (>= 5.0.0.1) + sass (>= 3.3.0) builder (3.2.2) byebug (8.2.0) coderay (1.1.0) @@ -117,7 +123,7 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - spring (1.4.3) + spring (1.3.6) sprockets (3.4.0) rack (> 1, < 3) sprockets-rails (2.3.3) @@ -147,6 +153,7 @@ PLATFORMS DEPENDENCIES better_errors binding_of_caller + bootstrap-sass (~> 3.3.5) byebug coffee-rails (~> 4.1.0) jbuilder (~> 2.0) diff --git a/app/assets/images/market.jpg b/app/assets/images/market.jpg new file mode 100644 index 00000000..af1d0f85 Binary files /dev/null and b/app/assets/images/market.jpg differ diff --git a/app/assets/images/market2.jpg b/app/assets/images/market2.jpg new file mode 100644 index 00000000..bf985154 Binary files /dev/null and b/app/assets/images/market2.jpg differ diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e07c5a83..f91cae5d 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -11,6 +11,7 @@ // about supported directives. // //= require jquery +//= require bootstrap-sprockets //= require jquery_ujs //= require turbolinks //= require_tree . diff --git a/app/assets/javascripts/products.coffee b/app/assets/javascripts/products.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/products.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/javascripts/sales.coffee b/app/assets/javascripts/sales.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/sales.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/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/sessions.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/javascripts/static_pages.coffee b/app/assets/javascripts/static_pages.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/static_pages.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/application.css b/app/assets/stylesheets/application.css deleted file mode 100644 index f9cd5b34..00000000 --- a/app/assets/stylesheets/application.css +++ /dev/null @@ -1,15 +0,0 @@ -/* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, - * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the bottom of the - * compiled file so the styles you add here take precedence over styles defined in any styles - * defined in the other CSS/SCSS files in this directory. It is generally better to create a new - * file per style scope. - * - *= require_tree . - *= require_self - */ diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss new file mode 100644 index 00000000..b9f808ff --- /dev/null +++ b/app/assets/stylesheets/application.scss @@ -0,0 +1,136 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any styles + * defined in the other CSS/SCSS files in this directory. It is generally better to create a new + * file per style scope. + * + */ + + // "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables" +@import "bootstrap-sprockets"; +@import "bootstrap"; +@import url(https://fonts.googleapis.com/css?family=Bitter:700); +@import url(https://fonts.googleapis.com/css?family=Lato); + +$purple: rgb(80, 48, 90); +$yellow: rgb(227, 230, 61); +$orange: rgb(231, 110, 38); + +$jumbo-height: 350px; +$title-font: 'Bitter', serif; +$body-font: 'Lato', sans-serif; + +.navbar-inverse { + background-color: $purple; +} +.navbar-inverse .navbar-nav > li > a, +.btn, +.btn-primary + { + background-color: $purple; + font-size: 1.2em; + color: $yellow; + font-weight: bold; + transition: all 0.5s ease-out; + &:hover, + &:active { + background-color: $yellow; + color: $purple; + transition: all 0.2s ease-out; + } +} + +.navbar-inverse .navbar-nav > .open > a, +.navbar-inverse .navbar-nav > .open > a:hover, +.navbar-inverse .navbar-nav > .open > a:focus { + background-color: $yellow; + color: $purple; + transition: all 0.2s ease-out; +} + + +#logo { + float: left; + margin-right: 10px; + font-size: 1.7em; + color: $yellow; + letter-spacing: -1px; + padding-top: 9px; + font-weight: bold; + &:hover { + color: $yellow; + text-decoration: none; + } +} + +.jumbotron { + height: $jumbo-height; + color: $yellow; + text-shadow: #444 1px 2px 1px; + background:transparent; + padding-top: 70px; + h1{ + color: $yellow; + font-size: 5em; + } +} + +h1 { + font-family: $title-font; +} + +.centered-block { + margin: auto; +} + +.centered-button { + margin-top: 20vh; +} + +thead { + font-size: 1.3em; + font-weight: bold; +} + +.table-color { + border-collapse:collapse; + border:2px solid $purple; + thead { + background: $yellow; + } +} + +body { + font-family: $body-font; +} + +// Parallax code from http://www.cssscript.com/simple-parallax-scrolling-effect-pure-css3/ +// Parallax necessary CSS +.parallax { + height: 100vh; + overflow-x: hidden; + overflow-y: auto; + perspective: 1px; + -webkit-perspective: 1px; +} +.parallax_layer { + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; +} +.parallax_layer_base { + transform: translateZ(0); + -webkit-transform: translateZ(0); +} +.parallax_layer_back { + -webkit-transform: translateZ(-1px); + transform: translateZ(-0.5px) scale(2); +} diff --git a/app/assets/stylesheets/products.scss b/app/assets/stylesheets/products.scss new file mode 100644 index 00000000..89e2e8db --- /dev/null +++ b/app/assets/stylesheets/products.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the products 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/sales.scss b/app/assets/stylesheets/sales.scss new file mode 100644 index 00000000..6f3405b9 --- /dev/null +++ b/app/assets/stylesheets/sales.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the sales 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/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 00000000..ccb1ed25 --- /dev/null +++ b/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions 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/static_pages.scss b/app/assets/stylesheets/static_pages.scss new file mode 100644 index 00000000..40608b2f --- /dev/null +++ b/app/assets/stylesheets/static_pages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the static_pages controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e1..d8b3d09e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + include SessionsHelper end diff --git a/app/controllers/markets_controller.rb b/app/controllers/markets_controller.rb index fa158671..63130690 100644 --- a/app/controllers/markets_controller.rb +++ b/app/controllers/markets_controller.rb @@ -1,2 +1,58 @@ class MarketsController < ApplicationController + before_action :get_market, only: [:edit, :show, :update] + + def index + @markets = Market.all + end + + def new + @market = Market.new + end + + def create + Market.create(market_params[:market]) + redirect_to "/markets" + end + + def edit + end + + def show + @vendors = Market.find(id).vendors + end + + def update + @market.update( + name: market_params[:market][:name], + address: market_params[:market][:address], + city: market_params[:market][:city], + county: market_params[:market][:county], + state: market_params[:market][:state], + zip: market_params[:market][:zip] + ) + redirect_to "/markets" + end + + def destroy + id = params[:id] + Market.destroy(id) + redirect_to "/markets" + end + + def search + @markets = Market.search(params[:search_term]) + render :index + end + + private + + def get_market + id = params[:id] + @market = Market.find(id) + end + + def market_params + params.permit(market:[:id, :name, :address, :city, :county, :state, :zip]) + end + end diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 00000000..950dfe45 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,57 @@ +class ProductsController < ApplicationController + before_action :get_product, only: [:update, :show, :edit] + + def index + vendor_id = params[:vendor_id] + @products = Vendor.find(vendor_id).products + end + + def new + @product = Product.new + @url_path = vendor_products_path + end + + def create + vendor_id = params[:vendor_id] + Product.create( + name: product_params[:product][:name], + vendor_id: session[:vendor_id]) + redirect_to vendor_products_path(vendor_id) + end + + def edit + @url_path = vendor_product_path + end + + def show + end + + def update + vendor_id = params[:vendor_id] + @product.update( + name: product_params[:product][:name], + vendor_id: vendor_id + ) + redirect_to vendor_product_path(vendor_id, id) + end + + def destroy + id = params[:id] + vendor_id = params[:vendor_id] + Product.destroy(id) + sales = Sale.where('product_id = ?', id) + sales.each { |sale| sale.destroy } + redirect_to vendor_products_path(vendor_id) + end + + private + def get_product + id = params[:id] + @product = Product.find(id) + end + + def product_params + params.permit(product:[:name]) + end + +end diff --git a/app/controllers/sales_controller.rb b/app/controllers/sales_controller.rb new file mode 100644 index 00000000..005d8319 --- /dev/null +++ b/app/controllers/sales_controller.rb @@ -0,0 +1,50 @@ +class SalesController < ApplicationController + + MONTH_HASH = {1 => "January", 2 => "February", 3 => "March", 4 => "April", + 5 => "May", 6 => "June", 7 => "July", 8 => "August", + 9 => "September", 10 => "October", 11 => "November", 12 => "December"} + + def index + @sales = Sale.where("vendor_id = ?", params[:id]) #id is vendor_id + end + + def new + @sale = Sale.new + @url_path = sales_create_vendor_product_path + end + + def create + num_pennies = sale_params[:sale][:amount].to_f * 100 + Sale.create( + amount: num_pennies, + purchase_time: Time.now, + vendor_id: params[:vendor_id], + product_id: params[:id] + ) + redirect_to sales_vendor_path(session[:vendor_id]) + end + + def month + month_id = params[:id] + @sales = Sale.where("vendor_id = ?", session[:vendor_id]).select{ |sale| sale.purchase_time.month == month_id.to_i } + @month = MONTH_HASH[month_id.to_i] + render :index + end + + def destroy + id = params[:id] + Sale.destroy(id) + redirect_to sales_vendor_path(session[:vendor_id]) + end + + private + + def sale_params + params.permit(sale:[:amount, :purchase_time, :vendor_id, :product_id]) + end + + def month_params + params.permit(params[:id]) + end + +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 00000000..8b507913 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,32 @@ +class SessionsController < ApplicationController + def new + if vendor_logged_in? + redirect_to vendor_path(session[:vendor_id]) + elsif market_logged_in? + redirect_to markets_path + end + end + + def create + if !params[:session].nil? && !params[:session][:vendor_id].nil? + session[:vendor_id] = params[:session][:vendor_id] + session[:vendor_name] = Vendor.find(params[:session][:vendor_id]).name + redirect_to vendor_path(session[:vendor_id]) + else # elsif params[:action] == "create" + session[:market] = true + redirect_to markets_path + end + + end + + def destroy + if vendor_logged_in? + session.delete(:vendor_id) + session.delete(:vendor_name) + @user = nil + elsif market_logged_in? + session.delete(:market)# remove market logged in + end + redirect_to root_path + end +end diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb new file mode 100644 index 00000000..91da0c26 --- /dev/null +++ b/app/controllers/static_pages_controller.rb @@ -0,0 +1,4 @@ +class StaticPagesController < ApplicationController + def index + end +end diff --git a/app/controllers/vendors_controller.rb b/app/controllers/vendors_controller.rb index f14c7ec9..78fc5b7b 100644 --- a/app/controllers/vendors_controller.rb +++ b/app/controllers/vendors_controller.rb @@ -1,2 +1,55 @@ class VendorsController < ApplicationController + before_action :get_vendor, only: [:edit, :update, :show] + + def index + @vendors = Vendor.all + end + + def new + @vendor = Vendor.new + end + + def create + Vendor.create(vendor_params[:vendor]) + redirect_to "/vendors" + end + + def edit + end + + def show + id = params[:id] + if session[:vendor_id] != id + render 'vendors/error' + else + @vendor = Vendor.find(id) + end + end + + def update + @vendor.update( + name: vendor_params[:vendor][:name], + no_employees: vendor_params[:vendor][:no_employees], + market_id: vendor_params[:vendor][:market_id] + ) + redirect_to vendor_path(session[:vendor_id]) + end + + def destroy + id = params[:id] + Vendor.destroy(id) + redirect_to "/vendors" + end + + private + + def get_vendor + id = params[:id] + @vendor = Vendor.find(id) + end + + def vendor_params + params.permit(vendor:[:id, :name, :no_employees, :market_id]) + end + end diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb new file mode 100644 index 00000000..ab5c42b3 --- /dev/null +++ b/app/helpers/products_helper.rb @@ -0,0 +1,2 @@ +module ProductsHelper +end diff --git a/app/helpers/sales_helper.rb b/app/helpers/sales_helper.rb new file mode 100644 index 00000000..8611c4a0 --- /dev/null +++ b/app/helpers/sales_helper.rb @@ -0,0 +1,2 @@ +module SalesHelper +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 00000000..1f8e2993 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,10 @@ +module SessionsHelper + def vendor_logged_in? + !session[:vendor_id].nil? + end + + def market_logged_in? + !session[:market].nil? + end + +end diff --git a/app/helpers/static_pages_helper.rb b/app/helpers/static_pages_helper.rb new file mode 100644 index 00000000..2d63e79e --- /dev/null +++ b/app/helpers/static_pages_helper.rb @@ -0,0 +1,2 @@ +module StaticPagesHelper +end diff --git a/app/models/market.rb b/app/models/market.rb index 1a4a52dd..a00dd5b0 100644 --- a/app/models/market.rb +++ b/app/models/market.rb @@ -1,2 +1,9 @@ class Market < ActiveRecord::Base + has_many :vendors + validates :name, presence: true + + def self.search(term) + t = "%#{term}%" + return Market.where("name LIKE ?" , t) + end end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 00000000..2997fe7f --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,6 @@ +class Product < ActiveRecord::Base + belongs_to :vendor + has_many :sales + validates :name, presence: true + validates :vendor_id, presence: true +end diff --git a/app/models/sale.rb b/app/models/sale.rb new file mode 100644 index 00000000..68f55827 --- /dev/null +++ b/app/models/sale.rb @@ -0,0 +1,6 @@ +class Sale < ActiveRecord::Base + belongs_to :vendor + belongs_to :product + validates :vendor_id, presence: true + validates :product_id, presence: true +end diff --git a/app/models/vendor.rb b/app/models/vendor.rb index dac23f0a..4208990c 100644 --- a/app/models/vendor.rb +++ b/app/models/vendor.rb @@ -1,2 +1,7 @@ class Vendor < ActiveRecord::Base + belongs_to :market + has_many :products + has_many :sales + validates :name, presence: true + validates :market_id, presence: true end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb new file mode 100644 index 00000000..469e985b --- /dev/null +++ b/app/views/layouts/_header.html.erb @@ -0,0 +1,7 @@ +<% if vendor_logged_in? %> + <%= render 'vendors/vendor_header' %> +<% elsif market_logged_in? %> + <%= render 'markets/market_header' %> +<% else %> + <%= render 'static_pages/home_header' %> +<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 443f3152..a7ba587a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,14 +1,30 @@ - +
Market Name | ++ | + |
+ There are no markets with that name. + | ++ | ++ | +
<%= market.name %> | +<%= button_to "Edit", edit_market_path(market), method: :get %> | +<%= button_to "Delete", market_path(market), method: :delete, data: { confirm: "Are you sure you want to delete #{market.name}?" } , class: 'x-button' %> | +
+ I'm the market: <%= @market.name %> +
++ Here's some info about me: <%= @market.address %> <%= @market.city %> <%= @market.county %> <%= @market.state %> <%= @market.zip %> +
++ Here are my vendors: <% @vendors.each do |vendor| %> <%= vendor.name %> <% end %> +
diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb new file mode 100644 index 00000000..fe8d695b --- /dev/null +++ b/app/views/products/_form.html.erb @@ -0,0 +1,9 @@ +<%= form_for @product, url: @url_path, :html => {:class => "form-group"} do |f| %> + +<% end %> diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 00000000..98ef9435 --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Edit Product Information") %> +<%= render partial: 'form', locals: {method_type: :patch, button_text: "Edit Product", legend_text: "Update Product"} %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 00000000..61b19066 --- /dev/null +++ b/app/views/products/index.html.erb @@ -0,0 +1,19 @@ +<% provide(:title, "View Vendor's Products") %> +Product Name | ++ | + |
<%= product.name %> | +<%= button_to "Add Sale", sales_new_vendor_product_path(session[:vendor_id], product.id), method: :get %> | +<%= button_to "Delete Me", vendor_product_path(session[:vendor_id],product.id), method: :delete, data: { confirm: "Are you sure you want to delete this #{product.name}?" } , class: 'x-button' %> | +
+ I'm the product: <%= @product.name %> +
++ Here's some info about me: <%= @product.vendor_id %> +
diff --git a/app/views/sales/_form.html.erb b/app/views/sales/_form.html.erb new file mode 100644 index 00000000..1c6f0aff --- /dev/null +++ b/app/views/sales/_form.html.erb @@ -0,0 +1,14 @@ +<%= form_for @sale, url: @url_path, :html => {:class => "form-group"} do |f| %> + +<% end %> diff --git a/app/views/sales/index.html.erb b/app/views/sales/index.html.erb new file mode 100644 index 00000000..02df9694 --- /dev/null +++ b/app/views/sales/index.html.erb @@ -0,0 +1,44 @@ +<% if @month.nil? %> + <% provide(:title, "Show All Vendor Sales") %> +<% else %> + <% provide(:title, "Sales For #{@month}") %> +<% end %> +Product Name | +Price | +Date | +Time | ++ |
No sales in this time period. | ++ | + | ||
<%= sale.product.name %> | +<%= number_to_currency(sale.amount / 100.0) %> | +<%= sale.purchase_time.strftime("%b %-d, %Y") %> | +<%= sale.purchase_time.strftime("%l:%M %P") %> | +<%= button_to "Delete", "/vendors/#{sale.vendor_id}/sales/#{sale.id}", method: :delete, data: { confirm: "Are you sure you want to delete this sale?" } , class: 'x-button' %> | +
Sorry, you are trying to access a page you do not have access to.
+Try logging in as another vendor.
diff --git a/app/views/vendors/index.html.erb b/app/views/vendors/index.html.erb new file mode 100644 index 00000000..e5d2a07d --- /dev/null +++ b/app/views/vendors/index.html.erb @@ -0,0 +1,19 @@ +<% provide(:title, "Show All Vendors") %> +Vendor Name | ++ | + |
<%= vendor.name %> | +<%= button_to "Edit", edit_vendor_path(vendor), method: :get %> | +<%= button_to "Delete", vendor_path(vendor), method: :delete, data: { confirm: "Are you sure you want to delete #{vendor.name}?" } , class: 'x-button' %> | +
<%= @vendor.name %>
+Number of Employees: <%= @vendor.no_employees %>
+Our Market: <%= @vendor.market.name %>
+<%= product.name %>
+ <% end %> +