diff --git a/Gemfile b/Gemfile index fba3b4e..229a96a 100644 --- a/Gemfile +++ b/Gemfile @@ -38,3 +38,8 @@ gem 'spring', group: :development # Use debugger # gem 'debugger', group: [:development, :test] +gem "devise" + +gem "bootstrap-sass" + +gem 'simple_form' diff --git a/Gemfile.lock b/Gemfile.lock index 53538d9..ce7e1ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,6 +28,9 @@ GEM thread_safe (~> 0.1) tzinfo (~> 1.1) arel (5.0.1.20140414130214) + bcrypt (3.1.7) + bootstrap-sass (3.2.0.0) + sass (~> 3.2) builder (3.2.2) coffee-rails (4.0.1) coffee-script (>= 2.2.0) @@ -36,6 +39,12 @@ GEM coffee-script-source execjs coffee-script-source (1.7.0) + devise (3.2.4) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 3.2.6, < 5) + thread_safe (~> 0.1) + warden (~> 1.2.3) erubis (2.7.0) execjs (2.2.0) hike (1.2.3) @@ -53,6 +62,7 @@ GEM mime-types (1.25.1) minitest (5.3.4) multi_json (1.10.1) + orm_adapter (0.5.0) polyglot (0.3.5) rack (1.5.2) rack-test (0.6.2) @@ -84,6 +94,9 @@ GEM sdoc (0.4.0) json (~> 1.8) rdoc (~> 4.0, < 5.0) + simple_form (3.0.2) + actionpack (~> 4.0) + activemodel (~> 4.0) spring (1.1.3) sprockets (2.11.0) hike (~> 1.2) @@ -108,17 +121,22 @@ GEM uglifier (2.5.0) execjs (>= 0.3.0) json (>= 1.8.0) + warden (1.2.3) + rack (>= 1.0) PLATFORMS ruby DEPENDENCIES + bootstrap-sass coffee-rails (~> 4.0.0) + devise jbuilder (~> 2.0) jquery-rails rails (= 4.1.0) sass-rails (~> 4.0.3) sdoc (~> 0.4.0) + simple_form spring sqlite3 turbolinks diff --git a/app/assets/javascripts/admin/products.js.coffee b/app/assets/javascripts/admin/products.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/admin/products.js.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/admin/products.css.scss b/app/assets/stylesheets/admin/products.css.scss new file mode 100644 index 0000000..da8969d --- /dev/null +++ b/app/assets/stylesheets/admin/products.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the admin::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/application.css b/app/assets/stylesheets/application.css index a443db3..bcdb41c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -12,4 +12,5 @@ * *= require_tree . *= require_self + *= require bootstrap */ diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb new file mode 100644 index 0000000..9e42fbf --- /dev/null +++ b/app/controllers/admin/products_controller.rb @@ -0,0 +1,48 @@ +class Admin::ProductsController < ApplicationController + + before_action :authenticate_user! + before_action :admin_required + + def index + @product = Product.all + end + + def new + @product = Product.new + end + + def create + @product = Product.new(product_params) + + if @product.save + redirect_to admin_products_path + else + render :new + end + end + + def show + @product = Product.find(params[:id]) + end + + def edit + @product = Product.find(params[:id]) + end + + def update + @product = Product.find(params[:id]) + + if @product.update(product_params) + redirect_to admin_products_path + else + render :edit + end + end + + private + + def product_params + params.require(:product).permit(:title, :description, :quantity) + end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..6ac255b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,10 @@ 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 + + def admin_required + if !current_user.admin? + redirect_to root_path + end + end end diff --git a/app/helpers/admin/products_helper.rb b/app/helpers/admin/products_helper.rb new file mode 100644 index 0000000..977a242 --- /dev/null +++ b/app/helpers/admin/products_helper.rb @@ -0,0 +1,2 @@ +module Admin::ProductsHelper +end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 0000000..077a819 --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,2 @@ +class Product < ActiveRecord::Base +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..4e0d27f --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,9 @@ +class User < ActiveRecord::Base + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :trackable, :validatable + def admin? + is_admin + end +end diff --git a/app/views/admin/products/edit.html.erb b/app/views/admin/products/edit.html.erb new file mode 100644 index 0000000..8eec353 --- /dev/null +++ b/app/views/admin/products/edit.html.erb @@ -0,0 +1,6 @@ +<%= simple_form_for [:admin, @product] do |f| %> +<%= f.input :title, label: 'Title' %> +<%= f.input :description, label: 'Description' %> +<%= f.input :quantity, label: 'Quantity' %> +<%= f.button :submit %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb new file mode 100644 index 0000000..129b47b --- /dev/null +++ b/app/views/admin/products/index.html.erb @@ -0,0 +1,23 @@ +
| # | +Title | +Description | +Quantity | +Modify | +
|---|---|---|---|---|
| <%= p.id %> | +<%= p.title %> | +<%= p[:description] %> | +<%= p.quantity %> | +<%= link_to "Edit", edit_admin_product_path(p.id) %> | +