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 @@ - + FarMarRails + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> - -<%= yield %> - + <%= render 'layouts/header' %> +
+
+ <%= image_tag("market2.jpg") %> +
+
+
+
+

<%= yield(:title) %>

+
+
+
+ <%= yield %> +
+
+
+
+
diff --git a/app/views/markets/_form.html.erb b/app/views/markets/_form.html.erb new file mode 100644 index 00000000..693cbe58 --- /dev/null +++ b/app/views/markets/_form.html.erb @@ -0,0 +1,19 @@ +<%= form_for @market, :html => {:class => "form-group"} do |f| %> +
+ <%= legend_text %> + <%= f.label :name %> + <%= f.text_field :name, class: 'form-control', :required => true %> + <%= f.label :address %> + <%= f.text_field :address, class: 'form-control' %> + <%= f.label :city %> + <%= f.text_field :city, class: 'form-control' %> + <%= f.label :county %> + <%= f.text_field :county, class: 'form-control' %> + <%= f.label :state %> + <%= f.text_field :state, class: 'form-control' %> + <%= f.label :zip %> + <%= f.text_field :zip, class: 'form-control' %> +
+ <%= f.submit value: "#{button_text}", class: "btn btn-primary"%> +
+<% end %> diff --git a/app/views/markets/_market_header.html.erb b/app/views/markets/_market_header.html.erb new file mode 100644 index 00000000..bfeb1a75 --- /dev/null +++ b/app/views/markets/_market_header.html.erb @@ -0,0 +1,31 @@ + diff --git a/app/views/markets/edit.html.erb b/app/views/markets/edit.html.erb new file mode 100644 index 00000000..23d38d74 --- /dev/null +++ b/app/views/markets/edit.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Edit Market Information") %> +<%= render partial: 'form', locals: {method_type: :patch, button_text: "Edit Market", legend_text: "Update Market"} %> diff --git a/app/views/markets/index.html.erb b/app/views/markets/index.html.erb new file mode 100644 index 00000000..85deec8a --- /dev/null +++ b/app/views/markets/index.html.erb @@ -0,0 +1,31 @@ +<% provide(:title, "Show All Markets") %> + + + + + + + + + + <% if @markets.empty? %> + + + + + + <% else %> + <% @markets.each do |market| %> + + + + + + <% end %> + <% end %> + +
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' %>
diff --git a/app/views/markets/new.html.erb b/app/views/markets/new.html.erb new file mode 100644 index 00000000..296a8456 --- /dev/null +++ b/app/views/markets/new.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Create a New Market") %> +<%= render partial: 'form', locals: {button_text: "Create Market", legend_text: "Add Market"} %> diff --git a/app/views/markets/show.html.erb b/app/views/markets/show.html.erb new file mode 100644 index 00000000..209f155c --- /dev/null +++ b/app/views/markets/show.html.erb @@ -0,0 +1,10 @@ +<% provide(:title, "Market Information") %> +

+ 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| %> +
+ <%= legend_text %> + <%= f.label :name %> + <%= f.text_field :name, class: 'form-control', :required => true %> +
+ <%= f.submit value: "#{button_text}", class: "btn btn-primary" %> +
+<% 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") %> + + + + + + + + + + <% @products.each do |product| %> + + + + + + <% end %> + +
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' %>
diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 00000000..2cf1676b --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Create New Vendor Product") %> +<%= render partial: 'form', locals: {button_text: "Create Product", legend_text: "Add Product"} %> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 00000000..fdcfe755 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,7 @@ +<% provide(:title, "Show Product Information") %> +

+ 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| %> +
+ <%= legend_text %> +

Product: <%= Product.find(params[:id]).name %>

+
+ <%= f.label :amount, "Amount of sale" %> +
+ $ + +
+
+ <%= f.submit value: "#{button_text}", class: "btn btn-primary" %> +
+<% 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 %> + + + + + + + + + + + + <% total_sales_amount = 0 %> + <% product_count = 0 %> + <% if @sales.empty? %> + + + + + + <% else %> + <% @sales.each do |sale| %> + + + + + + + + <% total_sales_amount += sale.amount %> + <% product_count += 1 %> + <% end %> + <% end %> + +
Product NamePriceDateTime
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' %>
+

+ Total sales: <%= number_to_currency(total_sales_amount / 100.0) %> +

+

Total products sold: <%= product_count %>

+

diff --git a/app/views/sales/new.html.erb b/app/views/sales/new.html.erb new file mode 100644 index 00000000..ed4d078f --- /dev/null +++ b/app/views/sales/new.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Vendor Create New Sale") %> +<%= render partial: 'form', locals: {button_text: "Create Sale", legend_text: "Add Sale"} %> diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 00000000..c468a0ee --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,7 @@ +<% provide(:title, "Farmers Market Central") %> +

Log in as a Vendor

+<%= form_for(:session, url: login_path) do |f| %> + <%= f.collection_select(:vendor_id, Vendor.all, :id, :name, {prompt: "Select the vendor:"}, {class: 'form-control'}) %> +
+ <%= f.submit "Log in", class: "btn btn-primary" %> +<% end %> diff --git a/app/views/static_pages/_home_header.html.erb b/app/views/static_pages/_home_header.html.erb new file mode 100644 index 00000000..0f556bb7 --- /dev/null +++ b/app/views/static_pages/_home_header.html.erb @@ -0,0 +1,20 @@ + diff --git a/app/views/static_pages/index.html.erb b/app/views/static_pages/index.html.erb new file mode 100644 index 00000000..35407888 --- /dev/null +++ b/app/views/static_pages/index.html.erb @@ -0,0 +1,12 @@ +<% provide(:title, "Farmers Market Central") %> +<%= button_to "View All Markets", markets_path, class: "btn btn-lg btn-primary center-block" %> +
+<%= form_tag("/markets/search", method: "get", class: "form-group") do %> +
+ Market Search + <%= label_tag(:search_term, "Search for markets by name:") %> + <%= text_field_tag(:search_term, nil, class: 'form-control') %> +
+ <%= submit_tag("Search", class: 'btn btn-primary') %> +
+<% end %> diff --git a/app/views/vendors/_form.html.erb b/app/views/vendors/_form.html.erb new file mode 100644 index 00000000..f859d625 --- /dev/null +++ b/app/views/vendors/_form.html.erb @@ -0,0 +1,13 @@ +<%= form_for @vendor, :html => {:class => "form-group"} do |f| %> +
+ <%= legend_text %> + <%= f.label :name %> + <%= f.text_field :name, class: 'form-control', :required => true %> + <%= f.label :no_employees %> + <%= f.text_field :no_employees, class: 'form-control' %> +
+ <%= f.collection_select(:market_id, Market.all, :id, :name, {prompt: "Select the market:"}, {class: 'form-control', :required => true}) %> +
+ <%= f.submit value: "#{button_text}", class: "btn btn-primary" %> +
+<% end %> diff --git a/app/views/vendors/_vendor_header.html.erb b/app/views/vendors/_vendor_header.html.erb new file mode 100644 index 00000000..81c34389 --- /dev/null +++ b/app/views/vendors/_vendor_header.html.erb @@ -0,0 +1,48 @@ + diff --git a/app/views/vendors/edit.html.erb b/app/views/vendors/edit.html.erb new file mode 100644 index 00000000..643319b8 --- /dev/null +++ b/app/views/vendors/edit.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Edit Vendor Information") %> +<%= render partial: 'form', locals: {method_type: :patch, button_text: "Edit Vendor", legend_text: "Update Vendor"} %> diff --git a/app/views/vendors/error.html.erb b/app/views/vendors/error.html.erb new file mode 100644 index 00000000..aa8cfbec --- /dev/null +++ b/app/views/vendors/error.html.erb @@ -0,0 +1,3 @@ +<% provide(:title, "Error") %> +

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") %> + + + + + + + + + + <% @vendors.each do |vendor| %> + + + + + + <% end %> + +
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' %>
diff --git a/app/views/vendors/new.html.erb b/app/views/vendors/new.html.erb new file mode 100644 index 00000000..83eeb9a4 --- /dev/null +++ b/app/views/vendors/new.html.erb @@ -0,0 +1,2 @@ +<% provide(:title, "Create a New Vendor") %> +<%= render partial: 'form', locals: {button_text: "Create Vendor", legend_text: "Add Vendor"} %> diff --git a/app/views/vendors/show.html.erb b/app/views/vendors/show.html.erb new file mode 100644 index 00000000..7f10943a --- /dev/null +++ b/app/views/vendors/show.html.erb @@ -0,0 +1,18 @@ +<% provide(:title, "Vendor Information") %> +
+
+

Vendor Name

+

<%= @vendor.name %>

+
+
+

Vendor Stats

+

Number of Employees: <%= @vendor.no_employees %>

+

Our Market: <%= @vendor.market.name %>

+
+
+

Products

+ <% @vendor.products.each do |product| %> +

<%= product.name %>

+ <% end %> +
+
diff --git a/bin/rails b/bin/rails index 1cd6d9f3..5191e692 100755 --- a/bin/rails +++ b/bin/rails @@ -1,10 +1,4 @@ #!/usr/bin/env ruby -begin - spring_bin_path = File.expand_path('../spring', __FILE__) - load spring_bin_path -rescue LoadError => e - raise unless e.message.end_with? spring_bin_path, 'spring/binstub' -end APP_PATH = File.expand_path('../../config/application', __FILE__) require_relative '../config/boot' require 'rails/commands' diff --git a/bin/rake b/bin/rake index 7b1f6408..17240489 100755 --- a/bin/rake +++ b/bin/rake @@ -1,10 +1,4 @@ #!/usr/bin/env ruby -begin - spring_bin_path = File.expand_path('../spring', __FILE__) - load spring_bin_path -rescue LoadError => e - raise unless e.message.end_with? spring_bin_path, 'spring/binstub' -end require_relative '../config/boot' require 'rake' Rake.application.run diff --git a/config/routes.rb b/config/routes.rb index 3f66539d..ae9900cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,31 @@ Rails.application.routes.draw do + get 'login' => 'sessions#new' + post 'login' => 'sessions#create' + delete 'logout' => 'sessions#destroy' + + root 'static_pages#index' + get 'markets/search' => 'markets#search' + + resources :markets do + collection do + get 'login/' => 'sessions#create' + end + end + + resources :vendors do + member do + get 'sales/' => 'sales#index' + delete 'sales/:id' => 'sales#destroy' + get 'sales/month/:id' => 'sales#month', as: :sales_month + end + resources :products do + member do + get 'sales/new' + post 'sales/create' + end + end + end + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20151118003220_add_index_to_vendor.rb b/db/migrate/20151118003220_add_index_to_vendor.rb new file mode 100644 index 00000000..4583aba6 --- /dev/null +++ b/db/migrate/20151118003220_add_index_to_vendor.rb @@ -0,0 +1,6 @@ +class AddIndexToVendor < ActiveRecord::Migration + def change + + add_index :vendors, :market_id + end +end diff --git a/db/migrate/20151118005047_create_products.rb b/db/migrate/20151118005047_create_products.rb new file mode 100644 index 00000000..b683e018 --- /dev/null +++ b/db/migrate/20151118005047_create_products.rb @@ -0,0 +1,10 @@ +class CreateProducts < ActiveRecord::Migration + def change + create_table :products do |t| + t.string :name + t.integer :vendor_id + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20151118005341_add_index_to_product.rb b/db/migrate/20151118005341_add_index_to_product.rb new file mode 100644 index 00000000..ec81a28a --- /dev/null +++ b/db/migrate/20151118005341_add_index_to_product.rb @@ -0,0 +1,5 @@ +class AddIndexToProduct < ActiveRecord::Migration + def change + add_index :products, :vendor_id + end +end diff --git a/db/migrate/20151118005422_create_sales.rb b/db/migrate/20151118005422_create_sales.rb new file mode 100644 index 00000000..6d4e2ea9 --- /dev/null +++ b/db/migrate/20151118005422_create_sales.rb @@ -0,0 +1,12 @@ +class CreateSales < ActiveRecord::Migration + def change + create_table :sales do |t| + t.integer :amount + t.datetime :purchase_time + t.integer :vendor_id + t.integer :product_id + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20151118173740_add_indices_to_sale.rb b/db/migrate/20151118173740_add_indices_to_sale.rb new file mode 100644 index 00000000..7105942a --- /dev/null +++ b/db/migrate/20151118173740_add_indices_to_sale.rb @@ -0,0 +1,6 @@ +class AddIndicesToSale < ActiveRecord::Migration + def change + add_index :sales, :vendor_id + add_index :sales, :product_id + end +end diff --git a/db/schema.rb b/db/schema.rb index e7fc356f..b7c9ab29 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,15 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151118002103) do - - create_table "vendors", force: :cascade do |t| - t.string "name" - t.integer "no_employees" - t.integer "market_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end +ActiveRecord::Schema.define(version: 20151118173740) do create_table "markets", force: :cascade do |t| t.string "name" @@ -30,7 +22,37 @@ t.string "zip" t.datetime "created_at", null: false t.datetime "updated_at", null: false + end + create_table "products", force: :cascade do |t| + t.string "name" + t.integer "vendor_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end + add_index "products", ["vendor_id"], name: "index_products_on_vendor_id" + + create_table "sales", force: :cascade do |t| + t.integer "amount" + t.datetime "purchase_time" + t.integer "vendor_id" + t.integer "product_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "sales", ["product_id"], name: "index_sales_on_product_id" + add_index "sales", ["vendor_id"], name: "index_sales_on_vendor_id" + + create_table "vendors", force: :cascade do |t| + t.string "name" + t.integer "no_employees" + t.integer "market_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "vendors", ["market_id"], name: "index_vendors_on_market_id" + end diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e85..7d9ab02f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,88 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) +require 'csv' + +def create_market_hash(market_array) + market_hash = {} + market_hash[:id] = market_array[0].to_i + market_hash[:name] = market_array[1] + market_hash[:address] = market_array[2] + market_hash[:city] = market_array[3] + market_hash[:county] = market_array[4] + market_hash[:state] = market_array[5] + market_hash[:zip] = market_array[6] + return market_hash +end + +csv_file = CSV.read('./seed_csvs/markets.csv') +# Create empty array which will hold all the objects +csv_file.each do |row| + # Convert the array to a hash + csv_hash = create_market_hash(row) + # Create an object from each row-hash in the csv file + Market.create(csv_hash) +end + +def create_vendor_hash(array) + vendor = {} + vendor[:id] = array[0].to_i + vendor[:name] = array[1] + vendor[:no_employees] = array[2].to_i + vendor[:market_id] = array[3].to_i + return vendor +end + +csv_file = CSV.read('./seed_csvs/vendors.csv') +# Create empty array which will hold all the objects +csv_file.each do |row| + # Convert the array to a hash + csv_hash = create_vendor_hash(row) + # Create an object from each row-hash in the csv file + Vendor.create(csv_hash) +end + +def create_product_hash(product_array) + product_hash = {} + product_hash[:id] = product_array[0].to_i + product_hash[:name] = product_array[1] + product_hash[:vendor_id] = product_array[2].to_i + return product_hash +end + +csv_file = CSV.read('./seed_csvs/products.csv') +# Create empty array which will hold all the objects +csv_file.each do |row| + # Convert the array to a hash + csv_hash = create_product_hash(row) + # Create an object from each row-hash in the csv file + Product.create(csv_hash) +end + +def create_sale_hash(sale_array) + sale_hash = {} + sale_hash[:id] = sale_array[0].to_i + sale_hash[:amount] = sale_array[1].to_i + sale_hash[:purchase_time] = rand(1..12).month.ago + sale_hash[:vendor_id] = sale_array[3].to_i + sale_hash[:product_id] = sale_array[4].to_i + return sale_hash +end + +csv_file = CSV.read('./seed_csvs/sales.csv') +# Create empty array which will hold all the objects +csv_file.each do |row| + # Convert the array to a hash + csv_hash = create_sale_hash(row) + # Create an object from each row-hash in the csv file + Sale.create(csv_hash) +end + +begin + con = PG::Connection + Market.connection.execute('SELECT setval(\'markets_id_seq\'::regclass, (SELECT MAX(id) FROM markets))') + Vendor.connection.execute('SELECT setval(\'vendors_id_seq\'::regclass, (SELECT MAX(id) FROM vendors))') + Product.connection.execute('SELECT setval(\'products_id_seq\'::regclass, (SELECT MAX(id) FROM products))') + Sale.connection.execute('SELECT setval(\'sales_id_seq\'::regclass, (SELECT MAX(id) FROM sales))') +rescue NameError +end diff --git a/public/favicon.ico b/public/favicon.ico index e69de29b..841af5d1 100644 Binary files a/public/favicon.ico and b/public/favicon.ico differ