rails_apps_composer: add pages
This commit is contained in:
parent
1c11630c68
commit
c48f1141e6
38
app/controllers/users_controller.rb
Normal file
38
app/controllers/users_controller.rb
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
class UsersController < ApplicationController
|
||||||
|
before_filter :authenticate_user!
|
||||||
|
after_action :verify_authorized
|
||||||
|
|
||||||
|
def index
|
||||||
|
@users = User.all
|
||||||
|
authorize User
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
authorize @user
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
authorize @user
|
||||||
|
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])
|
||||||
|
authorize user
|
||||||
|
user.destroy
|
||||||
|
redirect_to users_path, :notice => "User deleted."
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def secure_params
|
||||||
|
params.require(:user).permit(:role)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
2
app/controllers/visitors_controller.rb
Normal file
2
app/controllers/visitors_controller.rb
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
class VisitorsController < ApplicationController
|
||||||
|
end
|
||||||
26
app/policies/user_policy.rb
Normal file
26
app/policies/user_policy.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
class UserPolicy
|
||||||
|
attr_reader :current_user, :model
|
||||||
|
|
||||||
|
def initialize(current_user, model)
|
||||||
|
@current_user = current_user
|
||||||
|
@user = model
|
||||||
|
end
|
||||||
|
|
||||||
|
def index?
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
@current_user.admin? or @current_user == @user
|
||||||
|
end
|
||||||
|
|
||||||
|
def update?
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
return false if @current_user == @user
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
11
app/views/layouts/_navigation_links.html.erb
Normal file
11
app/views/layouts/_navigation_links.html.erb
Normal file
@ -0,0 +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 %>
|
||||||
7
app/views/pages/about.html.erb
Normal file
7
app/views/pages/about.html.erb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<% content_for :title do %>About<% end %>
|
||||||
|
<h3>About the Website</h3>
|
||||||
|
<p>
|
||||||
|
This web application was created with
|
||||||
|
<%= link_to('Rails Composer', 'http://railsapps.github.io/rails-composer/') %>
|
||||||
|
from the <%= link_to('RailsApps project', 'http://railsapps.github.io/') %>.
|
||||||
|
</p>
|
||||||
12
app/views/users/_user.html.erb
Normal file
12
app/views/users/_user.html.erb
Normal file
@ -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
app/views/users/index.html.erb
Normal file
16
app/views/users/index.html.erb
Normal file
@ -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
app/views/users/show.html.erb
Normal file
3
app/views/users/show.html.erb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<h3>User</h3>
|
||||||
|
<p>Name: <%= @user.name if @user.name %></p>
|
||||||
|
<p>Email: <%= @user.email if @user.email %></p>
|
||||||
2
app/views/visitors/index.html.erb
Normal file
2
app/views/visitors/index.html.erb
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<h3>Welcome</h3>
|
||||||
|
<p><%= link_to 'Users:', users_path %> <%= User.count %> registered</p>
|
||||||
17
config/initializers/devise_permitted_parameters.rb
Normal file
17
config/initializers/devise_permitted_parameters.rb
Normal file
@ -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
|
||||||
22
config/initializers/pundit.rb
Normal file
22
config/initializers/pundit.rb
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# config/initializers/pundit.rb
|
||||||
|
# Extends the ApplicationController to add Pundit for authorization.
|
||||||
|
# Modify this file to change the behavior of a 'not authorized' error.
|
||||||
|
# Be sure to restart your server when you modify this file.
|
||||||
|
module PunditHelper
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
include Pundit
|
||||||
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def user_not_authorized
|
||||||
|
flash[:alert] = "Access denied."
|
||||||
|
redirect_to (request.referrer || root_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
ApplicationController.send :include, PunditHelper
|
||||||
25
config/initializers/upmin.rb
Normal file
25
config/initializers/upmin.rb
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# config/initializers/upmin.rb
|
||||||
|
# Extends the Upmin ApplicationController to limit access to users with an admin role.
|
||||||
|
# Depends on Devise for authentication plus role-based authorization.
|
||||||
|
# Be sure to restart your server when you modify this file.
|
||||||
|
module AdminOnly
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
before_filter :authenticate_user!
|
||||||
|
before_filter :admin_only
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def admin_only
|
||||||
|
unless current_user.admin?
|
||||||
|
redirect_to :back, :alert => "Access denied."
|
||||||
|
end
|
||||||
|
rescue ActionController::RedirectBackError
|
||||||
|
redirect_to '/', :alert => "Access denied."
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
Upmin::ApplicationController.send :include, AdminOnly
|
||||||
@ -1,5 +1,8 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
|
mount Upmin::Engine => '/admin'
|
||||||
|
root to: 'visitors#index'
|
||||||
devise_for :users
|
devise_for :users
|
||||||
|
resources :users
|
||||||
# The priority is based upon order of creation: first created -> highest priority.
|
# The priority is based upon order of creation: first created -> highest priority.
|
||||||
# See how all your routes lay out with "rake routes".
|
# See how all your routes lay out with "rake routes".
|
||||||
|
|
||||||
|
|||||||
16
spec/features/visitors/about_page_spec.rb
Normal file
16
spec/features/visitors/about_page_spec.rb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Feature: 'About' page
|
||||||
|
# As a visitor
|
||||||
|
# I want to visit an 'about' page
|
||||||
|
# So I can learn more about the website
|
||||||
|
feature 'About page' do
|
||||||
|
|
||||||
|
# Scenario: Visit the 'about' page
|
||||||
|
# Given I am a visitor
|
||||||
|
# When I visit the 'about' page
|
||||||
|
# Then I see "About the Website"
|
||||||
|
scenario 'Visit the about page' do
|
||||||
|
visit 'pages/about'
|
||||||
|
expect(page).to have_content 'About the Website'
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
16
spec/features/visitors/home_page_spec.rb
Normal file
16
spec/features/visitors/home_page_spec.rb
Normal file
@ -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
spec/features/visitors/navigation_spec.rb
Normal file
18
spec/features/visitors/navigation_spec.rb
Normal file
@ -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
|
||||||
Loading…
x
Reference in New Issue
Block a user