Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
rake issue
Browse files Browse the repository at this point in the history
  • Loading branch information
i5okie committed Sep 9, 2013
1 parent be37ab2 commit d1bffa9
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 36 deletions.
1 change: 1 addition & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class HomeController < ApplicationController
def index
@users = User.all
end
end
42 changes: 42 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class UsersController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource :only => :index

def resource_params
unless params[resource_name].blank?
params.require(resource_name).permit(:name, :email, :password, :password_confirmation, :remember_me)
params.require(resource_name).permit(:role_ids, :as => :admin)
end
end

# def index
# authorize! :index, @user, :message => 'Not authorized as an administrator.'
# @users = User.all
# end

def show
@user = User.find(params[:id])
end

def update
authorize! :update, @user, :message => 'Not authorized as an administrator.'
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => :admin)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end

def destroy
authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
user = User.find(params[:id])
unless user == current_user
user.destroy
redirect_to users_path, :notice => "User deleted."
else
redirect_to users_path, :notice => "Can't delete yourself."
end
end

end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
25 changes: 1 addition & 24 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,12 @@ class Ability
include CanCan::Ability

def initialize(user)
# Define abilities for the passed in user here. For example:
#
# user ||= User.new # guest user (not logged in)
# if user.admin?
# can :manage, :all
# else
# can :read, :all
# end
#
# The first argument to `can` is the action you are giving the user
# permission to do.
# If you pass :manage it will apply to every action. Other common actions
# here are :read, :create, :update and :destroy.
#
# The second argument is the resource the user can perform the action on.
# If you pass :all it will apply to every resource. Otherwise pass a Ruby
# class of the resource.
#
# The third argument is an optional hash of conditions to further filter the
# objects.
# For example, here the user can only update published articles.
#
# can :update, Article, :published => true
#
# See the wiki for details:
# https://github.com/ryanb/cancan/wiki/Defining-Abilities
user ||= User.new # guest user (not logged in)
if user.has_role? :Admin
can :manage, :all
end
end

end
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
end

#attr_accessible :role_ids, :as => :admin
#attr_accessible :name, :email, :password, :password_confirmation, :remember_me
end
6 changes: 4 additions & 2 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<h3>Home</h3>
<% @users.each do |user| %>
<p>User: <%=link_to user.name, user %></p>
<% end %>
15 changes: 15 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div id="role-options-<%= user.id %>" class="modal" style="display: none;">
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<div class="modal-header">
<a class="close" data-dismiss="modal">&#215;</a>
<h3>Change Role</h3>
</div>
<div class="modal-body">
<%= f.input :role_ids, :collection => Role.all, :as => :radio_buttons, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids.first %>
</div>
<div class="modal-footer">
<%= f.submit "Change Role", :class => "btn" %>
<a class="btn" data-dismiss="modal" href="#">Close</a>
</div>
<% end %>
</div>
30 changes: 30 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h3>Users</h3>
<div class="span8">
<table class="table table-condensed">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Registered</th>
<th>Role</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= user.email %></td>
<td><%= user.created_at.to_date %></td>
<td><%= user.roles.first.name.titleize unless user.roles.first.nil? %></td>
<td>
<a data-toggle="modal" href="#role-options-<%= user.id %>" class="btn btn-mini" type="button">Change role</a>
<%= render user %>
</td>
<td><%= link_to("Delete user", user_path(user), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'btn btn-mini') unless user == current_user %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
3 changes: 3 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h3>User</h3>
<p>User: <%= @user.name %></p>
<p>Email: <%= @user.email if @user.email %></p>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
end

resources :items, only: :index
resources :users

root :to => 'home#index'

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20130904185424_create_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def change
t.timestamps
end
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20130904211337_add_attachment_photo_to_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def self.up
def self.down
drop_attached_file :items, :photo
end
end
end
16 changes: 9 additions & 7 deletions db/migrate/20130906215325_devise_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def change
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""

t.string :name

## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
Expand All @@ -20,15 +22,15 @@ def change
t.string :last_sign_in_ip

## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable

## Lockable
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at


t.timestamps
Expand Down
12 changes: 12 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
puts 'ROLES'
YAML.load(ENV['ROLES']).each do |role|
Role.find_or_create_by_name({ :name => role }, :without_protection => true)
puts 'role: ' << role
end
puts 'DEFAULT USERS'
user = User.find_or_create_by_email :name => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, :password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup
puts 'user: ' << user.name
user.add_role :admin
user2 = User.find_or_create_by_email :name => 'Second User', :email => '[email protected]', :password => 'changeme', :password_confirmation => 'changeme'
puts 'user: ' << user2.name
user2.add_role :VIP
15 changes: 15 additions & 0 deletions spec/helpers/users_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the UsersHelper. For example:
#
# describe UsersHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
describe UsersHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/users/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "users/index.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/users/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "users/show.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit d1bffa9

Please sign in to comment.