Skip to content

Commit

Permalink
Adding menu nav, adding permission enforcers to some controllers, add…
Browse files Browse the repository at this point in the history
…ing details to User model
  • Loading branch information
hornairs committed Apr 3, 2010
1 parent 927513a commit f0ff732
Show file tree
Hide file tree
Showing 24 changed files with 537 additions and 13 deletions.
11 changes: 8 additions & 3 deletions app/controllers/businesses_controller.rb
Expand Up @@ -16,6 +16,7 @@ def index
# Route: GET /businesses/1
def show
@business = Business.find(params[:id])
enforce_show_permission(@business)

respond_to do |format|
format.html # show.html.erb
Expand All @@ -27,6 +28,7 @@ def show
def new
@business = Business.new
@business.address ||= Address.new
enforce_create_permission(@business)

respond_to do |format|
format.html # new.html.erb
Expand All @@ -37,12 +39,15 @@ def new
# Route: GET /businesses/1/edit
def edit
@business = Business.find(params[:id])
enforce_update_permission(@business)

end

# Accepts POST data from the {BusinessesController#new} form to validate and create a new +Business+ record
# Route: POST /businesses
def create
@business = Business.new(params[:business])
enforce_create_permission(@business)

respond_to do |format|
if @business.save
Expand All @@ -60,8 +65,7 @@ def create
def update

@business = Business.find(params[:id])
puts @business
puts params
enforce_create_permission(@business)

respond_to do |format|
if @business.update_attributes(params[:business])
Expand All @@ -77,8 +81,9 @@ def update
# Route: DELETE /businesses/1
def destroy
@business = Business.find(params[:id])
enforce_destroy_permission(@business)
@business.destroy

respond_to do |format|
format.html { redirect_to(businesses_url) }
end
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/contacts_controller.rb
Expand Up @@ -11,6 +11,7 @@ def index
# GET /contacts/1
def show
@contact = Contact.find(params[:id])
enforce_view_permission(@contact)

respond_to do |format|
format.html # show.html.erb
Expand All @@ -21,6 +22,7 @@ def show
def new
@contact = Contact.new
@contact.address = Address.new
enforce_create_permission(@contact)

respond_to do |format|
format.html # new.html.erb
Expand All @@ -30,11 +32,14 @@ def new
# GET /contacts/1/edit
def edit
@contact = Contact.find(params[:id])
enforce_update_permission(@contact)

end

# POST /contacts
def create
@contact = Contact.new(params[:contact])
enforce_create_permission(@contact)

respond_to do |format|
if @contact.save
Expand All @@ -49,6 +54,7 @@ def create
# PUT /contacts/1
def update
@contact = Contact.find(params[:id])
enforce_update_permission(@contact)

respond_to do |format|
if @contact.update_attributes(params[:contact])
Expand All @@ -63,6 +69,8 @@ def update
# DELETE /contacts/1
def destroy
@contact = Contact.find(params[:id])
enforce_destroy_permission(@contact)

@contact.destroy

respond_to do |format|
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/users_controller.rb
@@ -1,19 +1,22 @@
# The +UsersController+ is responsible for registering (creating) new {User} records and managing them.
# @author Harry Brundage
class UsersController < ApplicationController
include Canable::Enforcers
# Make sure the user manging new users is logged in
before_filter :require_user

# Renders the registration form for a new user
# Route: GET /users/new
def new
@user = User.new
enforce_create_permission(@user)
end

# Accepts HTML POST data from the {UsersController#new} action to create a new +User+ record.
# Route: POST /users
def create
@user = User.new(params[:user])
enforce_create_permission(@user)
if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
Expand All @@ -26,19 +29,23 @@ def create
# Route: GET /users/1
def show
@user = @current_user
enforce_view_permission(@user)

end

# Renders the form to edit a particular user
# Route: GET /users/1/edit
def edit
@user = @current_user
enforce_update_permission(@user)
end

# Accepts data from the {UsersController#edit} form to update the attributes
# of an existing +User+ record
# Route: PUT /users/1
def update
@user = @current_user # makes our views "cleaner" and more consistent
enforce_update_permission(@user)
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
Expand Down
6 changes: 6 additions & 0 deletions app/models/user.rb
Expand Up @@ -12,4 +12,10 @@ class User < ActiveRecord::Base
acts_as_authentic do |c|
# for available options see documentation in: Authlogic::ActsAsAuthentic
end

validates_presence_of :active, :first_name, :last_name, :hourly_rate, :telephone, :role

def after_initialize
self.role ||= :employee
end
end
40 changes: 33 additions & 7 deletions app/views/layouts/application.html.erb
Expand Up @@ -22,13 +22,39 @@
</div>

<div class="sidebar span-4">
<p><% if !current_user %>
<%= link_to "Log In", new_user_session_path %>
<% else %>
<%= link_to "My Account", account_path %> |
<%= link_to "Register a new user", new_account_path %> |
<%= link_to "Logout", user_session_path, :method => :delete, :confirm => "Are you sure you want to logout?" %>
<% end %></p>
<% if !current_user %>
<%= link_to "Log In", new_user_session_path %>
<% else %>
<%= link_to "My Account", account_path %> |
<%= link_to "Logout", user_session_path, :method => :delete, :confirm => "Are you sure you want to logout?" %>
<br/>
<% semantic_menu :class => "menu" do |root|
root.add "Dashboard", root_path

root.add "Time Tracking", hour_reports_path do |hours|
hours.add "New Hour Report", new_hour_report_path if can_create?(HourReport.new)
end

root.add "Expenses", expenses_path do |expenses|
expenses.add "New Expense", new_expense_path if can_create?(Expense.new)
end

root.add "Projects", "#"

root.add "Invoices", "#"

root.add "Contacts", contacts_path do |contacts|
contacts.add "Add Contact", new_contact_path if can_create?(Contact.new)
contacts.add "List Businesses", businesses_path
contacts.add "Add Business", new_business_path if can_create?(Business.new)
end

root.add "Users", users_path do |users|
users.add "Add User", new_account_path if can_create?(User.new)
end

end %>
<% end %>
</div>

<!--- #content start -->
Expand Down
21 changes: 21 additions & 0 deletions app/views/users/_form.erb
@@ -1,6 +1,27 @@
<%= form.label :first_name %><br />
<%= form.text_field :first_name %><br />
<br />
<%= form.label :last_name %><br />
<%= form.text_field :last_name %><br />
<br />
<%= form.label :telephone %><br />
<%= form.text_field :telephone %><br />
<br />
<%= form.label :employee_number %><br />
<%= form.text_field :employee_number %><br />
<br />
<%= form.label :email %><br />
<%= form.text_field :email %><br />
<br />
<%= form.label :role %><br />
<%= form.select "role" , ['employee', 'manager', 'admin'] %><br />
<br />
<%= form.label :active %><br />
<%= form.check_box :active %><br />
<br />
<%= form.label :hourly_rate %><br />
<%= form.text_field :hourly_rate %><br />
<br />
<%= form.label :password, form.object.new_record? ? nil : "Change password" %><br />
<%= form.password_field :password %><br />
<br />
Expand Down
19 changes: 19 additions & 0 deletions db/migrate/20100403024826_add_details_to_user.rb
@@ -0,0 +1,19 @@
class AddDetailsToUser < ActiveRecord::Migration
def self.up
add_column :users, :active, :boolean
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :hourly_rate, :decimal
add_column :users, :telephone, :string
add_column :users, :employee_number, :string
end

def self.down
remove_column :users, :employee_number
remove_column :users, :telephone
remove_column :users, :hourly_rate
remove_column :users, :last_name
remove_column :users, :first_name
remove_column :users, :active
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100331201519) do
ActiveRecord::Schema.define(:version => 20100403024826) do

create_table "addresses", :force => true do |t|
t.string "street1"
Expand Down Expand Up @@ -79,6 +79,12 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
t.boolean "active"
t.string "first_name"
t.string "last_name"
t.decimal "hourly_rate"
t.string "telephone"
t.string "employee_number"
end

end
21 changes: 19 additions & 2 deletions db/seeds.rb
Expand Up @@ -6,5 +6,22 @@
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Major.create(:name => 'Daley', :city => cities.first)

User.create(:email=>"admin@example.com", :password => "apple123", :password_confirmation => "apple123", :role => "admin")
User.create(:email=>"joe@example.com", :password => "apple123", :password_confirmation => "apple123", :role => "employee")
User.create!(:email=>"admin@example.com",
:active=> true,
:first_name=>"Admin",
:last_name=>"Istrator",
:hourly_rate => 10,
:telephone => "5555555555",
:password => "apple123",
:password_confirmation => "apple123",
:role => "admin")

User.create!(:email=>"joe@example.com",
:active=> true,
:first_name=>"Joe",
:last_name=>"Employee",
:hourly_rate => 10,
:telephone => "5555555555",
:password => "apple123",
:password_confirmation => "apple123",
:role => "employee")
5 changes: 5 additions & 0 deletions test/factories/users.rb
@@ -1,7 +1,12 @@
Factory.define :user do |u|
u.active true
u.email "employee@email.com"
u.password "apple123"
u.password_confirmation "apple123"
u.first_name "Joe"
u.last_name "Blow"
u.telephone "5555555555"
u.hourly_rate 10
end

# needed for the example with all three types of users trying to create a new user
Expand Down
20 changes: 20 additions & 0 deletions vendor/plugins/semantic_navigation/MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 Daniel Haran

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 changes: 54 additions & 0 deletions vendor/plugins/semantic_navigation/README
@@ -0,0 +1,54 @@
= SemanticMenu

A plugin to make large menus easier to write.

Supports arbitrarily deep nesting; parents will be marked as 'active' if any of its children are active.

== Example

<%= semantic_menu do |root|
root.add "overview", root_path
root.add "comments", comments_path
end %>

Assuming you are on /comments, the output would be:

<ul class="menu">
<li>
<a href="/">overview</a>
</li>
<li class="active">
<a href="/comments">comments</a>
</li>
</ul>

add and semantic_menu both take an optional parameter hash, and you can nest the menu as deeply as you want:

<%= semantic_menu :class => 'top_level_nav' do |root|
root.add "overview", "root_path"
root.add "comments", "comments_path", :class => 'button' do |comments|
comments.add "My Comments", "my_comments_path"
comments.add "Recent", "recent_comments_path"
end
end %>

This would look like this:

<ul class="top_level_nav">
<li>
<a href="root_path">overview</a>
</li>
<li class="active">
<a href="comments_path" class="button">comments</a>
<ul class="menu_level_1">
<li class="active">
<a href="my_comments_path">My Comments</a>
</li>
<li>
<a href="recent_comments_path">Recent</a>
</li>
</ul>
</li>
</ul>

Copyright (c) 2008 Daniel Haran, released under the MIT license

0 comments on commit f0ff732

Please sign in to comment.