diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index f710272..e0b8391 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -8,4 +8,11 @@ class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
end
+
+ def authenticate_admin!
+ unless user_signed_in? && current_user.admin?
+ redirect_to root_path, notice: "We're sorry, this resource is reserved" +
+ " exclusively for site admins."
+ end
+ end
end
diff --git a/app/controllers/demos_controller.rb b/app/controllers/demos_controller.rb
new file mode 100644
index 0000000..b452fbe
--- /dev/null
+++ b/app/controllers/demos_controller.rb
@@ -0,0 +1,36 @@
+class DemosController < ApplicationController
+ before_action :authenticate_admin!
+
+ def new
+ @demo = Demo.new
+ end
+
+ def create
+ @demo = Demo.new(demo_params)
+
+ if @demo.save
+ redirect_to root_path
+ flash[:notice] = "Video has been uploaded"
+ else
+ flash[:notice] = @demo.errors.full_messages
+ render :new
+ end
+ end
+
+ def show
+ @demos = Demo.all
+ if !@demos.empty?
+ @demo = Demo.where(name: "site demo")
+ end
+ end
+
+ def edit
+ @demo = Demo.find(params[:id])
+ end
+
+ private
+
+ def demo_params
+ params.require(:demo).permit(:name, :video, :description)
+ end
+end
diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb
index 89df520..afec827 100644
--- a/app/controllers/welcome_controller.rb
+++ b/app/controllers/welcome_controller.rb
@@ -7,6 +7,11 @@ def index
if user_signed_in?
@user = current_user
@recipes = @user.recipes.sample(5)
+ else
+ @demos = Demo.all
+ if !@demos.empty?
+ @demo = Demo.where(name: "site demo")
+ end
end
end
diff --git a/app/models/demo.rb b/app/models/demo.rb
new file mode 100644
index 0000000..a4e0f50
--- /dev/null
+++ b/app/models/demo.rb
@@ -0,0 +1,5 @@
+class Demo < ApplicationRecord
+ validates :name, presence: true
+ validates :description, presence: false
+ validates :video, presence: true
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index a4fc025..f22aa45 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -10,5 +10,9 @@ def profile
Profile.where(user: User.ids)
end
+ def admin?
+ role == "admin"
+ end
+
has_many :recipes
end
diff --git a/app/views/demos/edit.html.erb b/app/views/demos/edit.html.erb
new file mode 100644
index 0000000..75184b0
--- /dev/null
+++ b/app/views/demos/edit.html.erb
@@ -0,0 +1 @@
+<% render 'new', demo: @demo %>
diff --git a/app/views/demos/new.html.erb b/app/views/demos/new.html.erb
new file mode 100644
index 0000000..4009cf6
--- /dev/null
+++ b/app/views/demos/new.html.erb
@@ -0,0 +1,38 @@
+<%= form_for @demo do |f| %>
+
+
+ <% if @demo.errors.any? %>
+
<%= pluralize(@demo.errors.count, "error(s)") %> prohibited this demo from being saved:
+
+ <% @demo.errors.full_messages.each do |m| %>
+ - <%= m %>
+ <% end %>
+
+ <% end %>
+
+
+
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+
+
+
+ <%= f.label :video %>
+ <%= f.text_field :video %>
+
+
+
+
+
+ <%= f.label :description %>
+ <%= f.text_area :description %>
+
+
+
+
+
+ <%= f.submit '+'%>
+
+<% end %>
diff --git a/app/views/demos/show.html.erb b/app/views/demos/show.html.erb
new file mode 100644
index 0000000..1d04bec
--- /dev/null
+++ b/app/views/demos/show.html.erb
@@ -0,0 +1,5 @@
+<% if !@demos.empty? %>
+ <% @demo.video %>
+ <%= link_to 'Edit Site Demo', edit_demo_path(@demo) %> |
+ <% @demo.description %>
+<% end %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 8e70164..ddebdad 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -21,6 +21,9 @@
<% end %>
<%= link_to 'Your Recipes', user_recipes_path(current_user)%> |
<%= link_to 'Add Recipe', new_user_recipe_path(current_user)%> |
+ <% if current_user && current_user.admin? %>
+ <%= link_to 'Add Site Demo', new_demo_path %> |
+ <% end %>
diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb
index 3c6dc8e..00fe254 100644
--- a/app/views/welcome/index.html.erb
+++ b/app/views/welcome/index.html.erb
@@ -20,6 +20,12 @@
<% end %>
+ <% else %>
+ <% if !@demos.empty? %>
+
<% @demo.video %>
+ <%= link_to 'Edit Site Demo', edit_demo_path(@demo) %> |
+
<% @demo.description %>
+ <% end %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 1d3f730..6117790 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -31,6 +31,7 @@
resources :instructions
resources :ingredients
+ resources :demos
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
diff --git a/db/migrate/20170421130857_create_demos.rb b/db/migrate/20170421130857_create_demos.rb
new file mode 100644
index 0000000..368ec44
--- /dev/null
+++ b/db/migrate/20170421130857_create_demos.rb
@@ -0,0 +1,8 @@
+class CreateDemos < ActiveRecord::Migration[5.0]
+ def change
+ create_table :demos do |t|
+ t.string :video
+ t.string :description
+ end
+ end
+end
diff --git a/db/migrate/20170421131516_add_name_to_demos.rb b/db/migrate/20170421131516_add_name_to_demos.rb
new file mode 100644
index 0000000..596e06a
--- /dev/null
+++ b/db/migrate/20170421131516_add_name_to_demos.rb
@@ -0,0 +1,5 @@
+class AddNameToDemos < ActiveRecord::Migration[5.0]
+ def change
+ add_column :demos, :name, :string
+ end
+end
diff --git a/db/migrate/20170421141345_add_role_to_users.rb b/db/migrate/20170421141345_add_role_to_users.rb
new file mode 100644
index 0000000..37a268c
--- /dev/null
+++ b/db/migrate/20170421141345_add_role_to_users.rb
@@ -0,0 +1,5 @@
+class AddRoleToUsers < ActiveRecord::Migration[5.0]
+ def change
+ add_column :users, :role, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2155540..14203d9 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,11 +10,17 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170330001559) do
+ActiveRecord::Schema.define(version: 20170421141345) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ create_table "demos", force: :cascade do |t|
+ t.string "video"
+ t.string "description"
+ t.string "name"
+ end
+
create_table "ingredients", force: :cascade do |t|
t.integer "volume1"
t.string "volume2"
@@ -65,6 +71,7 @@
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.string "role"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
diff --git a/db/seeds.rb b/db/seeds.rb
index ba2edda..cc37ecd 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -13,7 +13,8 @@
User.create(first_name: "Briana",
last_name: "West",
email: "bmwest@bu.edu",
- password: "123456")
+ password: "123456",
+ role: "admin")
@description = []
@@ -60,7 +61,7 @@
"Nectarine Muffins",
"Oatmeal Raisin Cookie Muffins",
"Orange Yogurt Bread",
- "Peachy Banana Muffins",,
+ "Peachy Banana Muffins",
"Sour Cream, Cheddar, and Chive Biscuits",
"Sour Cream Cinnamon Streusel Muffins with Pecan Filling",
"Southwest Style Egg Muffins",