1
0
mirror of https://github.com/fspc/BikeShed-1.git synced 2025-02-28 16:53:23 -05:00

Merge pull request #15 from spacemunkay/denney-add-users-roles

Changing UserRole to UserRoleJoin
This commit is contained in:
jnm 2013-05-23 18:03:59 -07:00
commit 3c90c13c3b
18 changed files with 124 additions and 53 deletions

View File

@ -42,7 +42,10 @@ class AppTabPanel < Netzke::Basepack::TabPanel
wrappedComponent: :transactions_border, wrappedComponent: :transactions_border,
title: "Transactions"}, title: "Transactions"},
:logs, :logs,
:user_roles] { layout: :fit,
wrappedComponent: :user_role_joins,
title: "User Roles"}
]
end end
@@app_tab_panel_items.each do |item| @@app_tab_panel_items.each do |item|
@ -52,7 +55,6 @@ class AppTabPanel < Netzke::Basepack::TabPanel
self.class.component item[:wrappedComponent] self.class.component item[:wrappedComponent]
end end
end end
c.prevent_header = true c.prevent_header = true
c.tbar = [:sign_out, :check_out] c.tbar = [:sign_out, :check_out]
c.items = @@app_tab_panel_items c.items = @@app_tab_panel_items

View File

@ -37,7 +37,7 @@ class UserLogs < Netzke::Basepack::Grid
{ :name => :end_date, :hidden => true, :default_value => Time.now.to_formatted_s(:db) }, { :name => :end_date, :hidden => true, :default_value => Time.now.to_formatted_s(:db) },
{ :name => :hours, :getter => lambda { |rec| (rec.end_date - rec.start_date)/3600 }, :sorting_scope => :sort_by_duration}, { :name => :hours, :getter => lambda { |rec| (rec.end_date - rec.start_date)/3600 }, :sorting_scope => :sort_by_duration},
:description, :description,
{ :name => :user_action__action, :text => 'Action' }, { :name => :user_action__action, :text => 'Action', :default_value => ::ActsAsLoggable::UserAction.all.first.id },
:created_at, :created_at,
:updated_at, :updated_at,
{ :name => :logged_by, :getter => lambda{ |rec| { :name => :logged_by, :getter => lambda{ |rec|
@ -53,13 +53,12 @@ class UserLogs < Netzke::Basepack::Grid
bike_store = Bike.all.map { |b| [b.id, b.shop_id] } bike_store = Bike.all.map { |b| [b.id, b.shop_id] }
current_user ||= User.find_by_id(session[:selected_user_id]) || controller.current_user current_user ||= User.find_by_id(session[:selected_user_id]) || controller.current_user
bike_id = current_user.bike.nil? ? nil : current_user.bike.id bike_id = current_user.bike.nil? ? nil : current_user.bike.id
action_id = current_user.user_role.id
[ [
{ :name => :start_date}, { :name => :start_date},
{ :name => :end_date}, { :name => :end_date},
{ :name => :description}, { :name => :description},
#had to hack acts_as_loggable/log.rb to get this to work #had to hack acts_as_loggable/log.rb to get this to work
{ :name => :user_action__action, :field_label => 'Action', :value => action_id}, { :name => :user_action__action, :field_label => 'Action'},
{ :name => :for_bike, :checkboxName => :copy_log, :inputValue => true, :title => "Copy description to a Bike's History?", :xtype => 'fieldset', :checkboxToggle => true, :collapsed => true, :items => [ { :name => :for_bike, :checkboxName => :copy_log, :inputValue => true, :title => "Copy description to a Bike's History?", :xtype => 'fieldset', :checkboxToggle => true, :collapsed => true, :items => [
{:xtype => 'combo', :no_binding => true, :name => :copy_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store, :value => bike_id} {:xtype => 'combo', :no_binding => true, :name => :copy_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store, :value => bike_id}
] ]

View File

@ -0,0 +1,21 @@
class UserRoleJoins < Netzke::Basepack::Grid
def configure(c)
super
c.model = "UserRoleJoin"
c.header = false
c.title = "User Roles"
c.columns = [
{ :name => :user__first_name, :text => "First"},
{ :name => :user__last_name, :text => "Last"},
{ :name => :role__role, :text => "Role"},
:created_at,
:updated_at,
:ends ]
end
#override with nil to remove actions
def default_bbar
[ :apply, :add_in_form, :search ]
end
end

View File

@ -1,15 +0,0 @@
class UserRoles < Netzke::Basepack::Grid
def configure(c)
super
c.model = "UserRole"
c.title = "User Roles"
c.columns = [ :role, :created_at, :updated_at, :ends ]
end
#override with nil to remove actions
def default_bbar
[ :apply, :add_in_form, :search ]
end
end

View File

@ -9,7 +9,6 @@ class Users < Netzke::Basepack::Grid
:last_name, :last_name,
:nickname, :nickname,
:email, :email,
:user_role__role,
:bike__shop_id :bike__shop_id
] ]
end end

View File

@ -1,14 +1,16 @@
class Ability class Ability
include CanCan::Ability include CanCan::Ability
def initialize(current_user) def initialize(current_user)
@current_user = current_user @current_user = current_user
self.send(current_user.role.to_sym) current_user.roles.each do |role|
self.send(role.role.to_sym)
end
end end
def admin def admin
can :manage, :all can :manage, :all
end end
def staff def staff
can :manage, :all can :manage, :all
@ -21,7 +23,7 @@ class Ability
def user def user
can :read, :all can :read, :all
can :update, Bike, :id => @current_user.bike_id unless @current_user.bike.nil? can :manage, Bike, :id => @current_user.bike_id unless @current_user.bike.nil?
can :manage, ::ActsAsLoggable::Log, { :loggable_type => "Bike", :loggable_id => @current_user.bike_id } can :manage, ::ActsAsLoggable::Log, { :loggable_type => "Bike", :loggable_id => @current_user.bike_id }
can :manage, ::ActsAsLoggable::Log, { :loggable_type => "User", :loggable_id => @current_user.id } can :manage, ::ActsAsLoggable::Log, { :loggable_type => "User", :loggable_id => @current_user.id }
end end

16
app/models/role.rb Normal file
View File

@ -0,0 +1,16 @@
class Role < ActiveRecord::Base
attr_accessible :role
has_many :user_role_joins
has_many :users, through: :user_role_joins
validates_uniqueness_of :role
def to_s
self.role
end
def ==(other)
self.role == other.role
end
end

View File

@ -7,14 +7,16 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model # Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :nickname, :user_role_id, :bike_id, :first_name, :last_name, :nickname, :bike_id,
:user_profiles_attributes :user_profiles_attributes
has_many :transactions has_many :transactions
has_many :user_profiles has_many :user_profiles
accepts_nested_attributes_for :user_profiles, allow_destroy: false accepts_nested_attributes_for :user_profiles, allow_destroy: false
has_one :user_role has_many :user_role_joins, :conditions => ["ends IS NULL OR ends > ?", Time.now]
has_many :roles, through: :user_role_joins
belongs_to :bike belongs_to :bike
validates :first_name, :presence => true validates :first_name, :presence => true
@ -28,12 +30,11 @@ class User < ActiveRecord::Base
to_s to_s
end end
def role
user_role.role
end
def role?(role) def role?(role)
user_role.to_s == role.to_s if role.kind_of?(String) or role.kind_of?(Symbol)
role = Role.find_by_role(role.to_s)
end
roles.include?(role)
end end
### TODO methods below probably belong somewhere else ### TODO methods below probably belong somewhere else

View File

@ -1,11 +0,0 @@
class UserRole < ActiveRecord::Base
attr_accessible :role
belongs_to :user
self.per_page = 15
def to_s
self.role
end
end

View File

@ -0,0 +1,13 @@
class UserRoleJoin < ActiveRecord::Base
self.table_name = :user_role_joins
attr_accessible :role_id, :user_id, :ends
belongs_to :user
belongs_to :role
validate :role_id, presence: true, numericality: true
validate :user_id, presence: true, numericality: true
validates_uniqueness_of :user_id, :scope => :role_id
self.per_page = 15
end

View File

@ -0,0 +1,9 @@
class AlterUserRoles < ActiveRecord::Migration
def change
rename_table :user_roles, :user_role_joins
change_table :user_role_joins do |t|
t.rename :role, :role_id
t.change :role_id, :integer
end
end
end

View File

@ -0,0 +1,8 @@
class AddRoles < ActiveRecord::Migration
def change
create_table(:roles) do |t|
t.string :role
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
class AlterUser < ActiveRecord::Migration
def change
remove_column :users, :user_role_id
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130419010051) do ActiveRecord::Schema.define(:version => 20130424005701) do
create_table "bike_actions", :force => true do |t| create_table "bike_actions", :force => true do |t|
t.string "action", :limit => 128, :null => false t.string "action", :limit => 128, :null => false
@ -97,6 +97,12 @@ ActiveRecord::Schema.define(:version => 20130419010051) do
add_index "logs", ["loggable_id", "loggable_type", "context"], :name => "index_logs_on_loggable_id_and_loggable_type_and_context" add_index "logs", ["loggable_id", "loggable_type", "context"], :name => "index_logs_on_loggable_id_and_loggable_type_and_context"
create_table "roles", :force => true do |t|
t.string "role"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "task_lists", :force => true do |t| create_table "task_lists", :force => true do |t|
t.integer "item_id", :null => false t.integer "item_id", :null => false
t.string "item_type", :null => false t.string "item_type", :null => false
@ -148,8 +154,8 @@ ActiveRecord::Schema.define(:version => 20130419010051) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
create_table "user_roles", :force => true do |t| create_table "user_role_joins", :force => true do |t|
t.string "role" t.integer "role_id"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.datetime "ends" t.datetime "ends"
@ -167,7 +173,6 @@ ActiveRecord::Schema.define(:version => 20130419010051) do
t.datetime "last_sign_in_at" t.datetime "last_sign_in_at"
t.string "current_sign_in_ip" t.string "current_sign_in_ip"
t.string "last_sign_in_ip" t.string "last_sign_in_ip"
t.integer "user_role_id", :default => 1, :null => false
t.integer "bike_id" t.integer "bike_id"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false

View File

@ -0,0 +1,9 @@
user:
id: 1
role: user
staff:
id: 2
role: staff
admin:
id: 3
role: admin

View File

@ -21,7 +21,7 @@ end
if Rails.env.development? if Rails.env.development?
#create default admin user #create default admin user
if UserRole.all.empty? and User.all.empty? if User.all.empty?
FactoryGirl.create(:user) FactoryGirl.create(:user)
FactoryGirl.create(:staff) FactoryGirl.create(:staff)
FactoryGirl.create(:admin) FactoryGirl.create(:admin)

View File

@ -1,5 +1,5 @@
FactoryGirl.define do FactoryGirl.define do
factory :user_role do factory :role do
factory :role_staff do factory :role_staff do
role 'staff' role 'staff'
end end

View File

@ -6,21 +6,29 @@ FactoryGirl.define do
first_name 'Michael' first_name 'Michael'
last_name 'Scott' last_name 'Scott'
sequence(:bike_id) { |n| n } sequence(:bike_id) { |n| n }
association :user_role, factory: :role_user after_build do |r|
r.roles << (Role.find_by_role("user") || FactoryGirl.create(:role_user))
end
factory :staff do factory :staff do
first_name 'Staff' first_name 'Staff'
association :user_role, factory: :role_staff after_build do |r|
r.roles << (Role.find_by_role("staff") || FactoryGirl.create(:role_staff))
end
end end
factory :admin do factory :admin do
first_name 'Admin' first_name 'Admin'
association :user_role, factory: :role_admin after_build do |r|
r.roles << (Role.find_by_role("admin") || FactoryGirl.create(:role_admin))
end
end end
factory :bike_admin do factory :bike_admin do
first_name 'BikeAdmin' first_name 'BikeAdmin'
association :user_role, factory: :role_bike_admin after_build do |r|
r.roles << (Role.find_by_role("bike_admin") || FactoryGirl.create(:role_bike_admin))
end
end end
end end