Skip to content

Commit

Permalink
rails_apps_composer: add pages
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielKehoe committed Feb 3, 2015
1 parent 6c7ec07 commit 1a4a22a
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,45 @@
class UsersController < ApplicationController
before_filter :authenticate_user!
before_filter :admin_only, :except => :show

def index
@users = User.all
end

def show
@user = User.find(params[:id])
unless current_user.admin?
unless @user == current_user
redirect_to :back, :alert => "Access denied."
end
end
end

def update
@user = User.find(params[:id])
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end

def destroy
user = User.find(params[:id])
user.destroy
redirect_to users_path, :notice => "User deleted."
end

private

def admin_only
unless current_user.admin?
redirect_to :back, :alert => "Access denied."
end
end

def secure_params
params.require(:user).permit(:role)
end

end
2 changes: 2 additions & 0 deletions app/controllers/visitors_controller.rb
@@ -0,0 +1,2 @@
class VisitorsController < ApplicationController
end
10 changes: 10 additions & 0 deletions app/views/layouts/_navigation_links.html.erb
@@ -1 +1,11 @@
<%# add navigation links to this file %>
<% if user_signed_in? %>
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
<li><%= link_to 'Sign in', new_user_session_path %></li>
<li><%= link_to 'Sign up', new_user_registration_path %></li>
<% end %>
<% if user_signed_in? %>
<li><%= link_to 'Users', users_path %></li>
<% end %>
12 changes: 12 additions & 0 deletions app/views/users/_user.html.erb
@@ -0,0 +1,12 @@
<td>
<%= link_to user.email, user %>
</td>
<td>
<%= form_for(user) do |f| %>
<%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>
<%= f.submit 'Change Role', :class => 'button-xs' %>
<% end %>
</td>
<td>
<%= link_to("Delete user", user_path(user), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'button-xs') unless user == current_user %>
</td>
16 changes: 16 additions & 0 deletions app/views/users/index.html.erb
@@ -0,0 +1,16 @@
<div class="container">
<div class="row">
<h3>Users</h3>
<div class="column">
<table class="table">
<tbody>
<% @users.each do |user| %>
<tr>
<%= render user %>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/users/show.html.erb
@@ -0,0 +1,3 @@
<h3>User</h3>
<p>Name: <%= @user.name if @user.name %></p>
<p>Email: <%= @user.email if @user.email %></p>
2 changes: 2 additions & 0 deletions app/views/visitors/index.html.erb
@@ -0,0 +1,2 @@
<h3>Welcome</h3>
<p><%= link_to 'Users:', users_path %> <%= User.count %> registered</p>
17 changes: 17 additions & 0 deletions config/initializers/devise_permitted_parameters.rb
@@ -0,0 +1,17 @@
module DevisePermittedParameters
extend ActiveSupport::Concern

included do
before_filter :configure_permitted_parameters
end

protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
devise_parameter_sanitizer.for(:account_update) << :name
end

end

DeviseController.send :include, DevisePermittedParameters
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
resources :users
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
16 changes: 16 additions & 0 deletions spec/features/visitors/home_page_spec.rb
@@ -0,0 +1,16 @@
# Feature: Home page
# As a visitor
# I want to visit a home page
# So I can learn more about the website
feature 'Home page' do

# Scenario: Visit the home page
# Given I am a visitor
# When I visit the home page
# Then I see "Welcome"
scenario 'visit the home page' do
visit root_path
expect(page).to have_content 'Welcome'
end

end
18 changes: 18 additions & 0 deletions spec/features/visitors/navigation_spec.rb
@@ -0,0 +1,18 @@
# Feature: Navigation links
# As a visitor
# I want to see navigation links
# So I can find home, sign in, or sign up
feature 'Navigation links', :devise do

# Scenario: View navigation links
# Given I am a visitor
# When I visit the home page
# Then I see "home," "sign in," and "sign up"
scenario 'view navigation links' do
visit root_path
expect(page).to have_content 'Home'
expect(page).to have_content 'Sign in'
expect(page).to have_content 'Sign up'
end

end

0 comments on commit 1a4a22a

Please sign in to comment.