Browse Source

Changing UserRole to UserRoleJoin

Beware, this breaks the view for some reason
eperez-timeinput
Jason Denney 11 years ago
parent
commit
91bb2ea43e
  1. 3
      app/components/app_tab_panel.rb
  2. 2
      app/components/user_logs.rb
  3. 4
      app/components/user_roles.rb
  4. 1
      app/components/users.rb
  5. 8
      app/models/ability.rb
  6. 16
      app/models/role.rb
  7. 15
      app/models/user.rb
  8. 11
      app/models/user_role.rb
  9. 13
      app/models/user_role_join.rb
  10. 9
      db/migrate/20130423231937_alter_user_roles.rb
  11. 8
      db/migrate/20130423233228_add_roles.rb
  12. 5
      db/migrate/20130424005701_alter_user.rb
  13. 13
      db/schema.rb
  14. 9
      db/seed/fixtures/roles.yml
  15. 2
      db/seeds.rb
  16. 2
      spec/factories/roles.rb
  17. 16
      spec/factories/users.rb

3
app/components/app_tab_panel.rb

@ -37,7 +37,8 @@ class AppTabPanel < Netzke::Basepack::TabPanel
wrappedComponent: :transactions_border,
title: "Transactions"},
:logs,
:user_roles]
:user_roles
]
end
@@app_tab_panel_items.each do |item|

2
app/components/user_logs.rb

@ -53,7 +53,7 @@ class UserLogs < Netzke::Basepack::Grid
bike_store = Bike.all.map { |b| [b.id, b.shop_id] }
current_user ||= User.find_by_id(session[:selected_user_id]) || controller.current_user
bike_id = current_user.bike.nil? ? nil : current_user.bike.id
action_id = current_user.user_role.id
action_id = ::ActsAsLoggable::UserAction.all.first.id
[
{ :name => :start_date},
{ :name => :end_date},

4
app/components/user_roles.rb

@ -2,9 +2,9 @@ class UserRoles < Netzke::Basepack::Grid
def configure(c)
super
c.model = "UserRole"
c.model = "UserRoleJoin"
c.title = "User Roles"
c.columns = [ :role, :created_at, :updated_at, :ends ]
#c.columns = [ :user__first_name, :role__role, :created_at, :updated_at, :ends ]
end
#override with nil to remove actions

1
app/components/users.rb

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

8
app/models/ability.rb

@ -1,14 +1,14 @@
class Ability
include CanCan::Ability
def initialize(current_user)
@current_user = current_user
self.send(current_user.role.to_sym)
self.send(current_user.role.role.to_sym)
end
def admin
can :manage, :all
end
end
def staff
can :manage, :all
@ -21,7 +21,7 @@ class Ability
def user
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 => "User", :loggable_id => @current_user.id }
end

16
app/models/role.rb

@ -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

15
app/models/user.rb

@ -7,14 +7,16 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
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
has_many :transactions
has_many :user_profiles
accepts_nested_attributes_for :user_profiles, allow_destroy: false
has_one :user_role
has_many :user_role_joins
has_many :roles, through: :user_role_joins
belongs_to :bike
validates :first_name, :presence => true
@ -28,12 +30,11 @@ class User < ActiveRecord::Base
to_s
end
def role
user_role.role
end
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
def total_hours

11
app/models/user_role.rb

@ -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

13
app/models/user_role_join.rb

@ -0,0 +1,13 @@
class UserRoleJoin < ActiveRecord::Base
#set_table_name :user_role_joins
attr_accessible :role_id, :user_id
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

9
db/migrate/20130423231937_alter_user_roles.rb

@ -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.remove :id
end
end
end

8
db/migrate/20130423233228_add_roles.rb

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

5
db/migrate/20130424005701_alter_user.rb

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

13
db/schema.rb

@ -11,7 +11,7 @@
#
# 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|
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"
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|
t.integer "item_id", :null => false
t.string "item_type", :null => false
@ -148,8 +154,8 @@ ActiveRecord::Schema.define(:version => 20130419010051) do
t.datetime "updated_at", :null => false
end
create_table "user_roles", :force => true do |t|
t.string "role"
create_table "user_role_joins", :id => false, :force => true do |t|
t.string "role_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "ends"
@ -167,7 +173,6 @@ ActiveRecord::Schema.define(:version => 20130419010051) do
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "user_role_id", :default => 1, :null => false
t.integer "bike_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false

9
db/seed/fixtures/roles.yml

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

2
db/seeds.rb

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

2
spec/factories/user_roles.rb → spec/factories/roles.rb

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

16
spec/factories/users.rb

@ -6,21 +6,29 @@ FactoryGirl.define do
first_name 'Michael'
last_name 'Scott'
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
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
factory :admin do
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
factory :bike_admin do
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

Loading…
Cancel
Save