From 91bb2ea43e4abfa365544bde99eb683b0d527ff4 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Tue, 23 Apr 2013 23:21:17 -0400 Subject: [PATCH 1/9] Changing UserRole to UserRoleJoin Beware, this breaks the view for some reason --- app/components/app_tab_panel.rb | 3 ++- app/components/user_logs.rb | 2 +- app/components/user_roles.rb | 4 ++-- app/components/users.rb | 1 - app/models/ability.rb | 8 ++++---- app/models/role.rb | 16 ++++++++++++++++ app/models/user.rb | 15 ++++++++------- app/models/user_role.rb | 11 ----------- app/models/user_role_join.rb | 13 +++++++++++++ db/migrate/20130423231937_alter_user_roles.rb | 9 +++++++++ db/migrate/20130423233228_add_roles.rb | 8 ++++++++ db/migrate/20130424005701_alter_user.rb | 5 +++++ db/schema.rb | 13 +++++++++---- db/seed/fixtures/roles.yml | 9 +++++++++ db/seeds.rb | 2 +- spec/factories/{user_roles.rb => roles.rb} | 2 +- spec/factories/users.rb | 16 ++++++++++++---- 17 files changed, 100 insertions(+), 37 deletions(-) create mode 100644 app/models/role.rb delete mode 100644 app/models/user_role.rb create mode 100644 app/models/user_role_join.rb create mode 100644 db/migrate/20130423231937_alter_user_roles.rb create mode 100644 db/migrate/20130423233228_add_roles.rb create mode 100644 db/migrate/20130424005701_alter_user.rb create mode 100644 db/seed/fixtures/roles.yml rename spec/factories/{user_roles.rb => roles.rb} (91%) diff --git a/app/components/app_tab_panel.rb b/app/components/app_tab_panel.rb index faf4bc8..60fbb4f 100644 --- a/app/components/app_tab_panel.rb +++ b/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| diff --git a/app/components/user_logs.rb b/app/components/user_logs.rb index 1300845..c7780b2 100644 --- a/app/components/user_logs.rb +++ b/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}, diff --git a/app/components/user_roles.rb b/app/components/user_roles.rb index 4de2383..bd81705 100644 --- a/app/components/user_roles.rb +++ b/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 diff --git a/app/components/users.rb b/app/components/users.rb index b844e4a..846b5fa 100644 --- a/app/components/users.rb +++ b/app/components/users.rb @@ -9,7 +9,6 @@ class Users < Netzke::Basepack::Grid :last_name, :nickname, :email, - :user_role__role, :bike__shop_id ] end diff --git a/app/models/ability.rb b/app/models/ability.rb index 4fde40d..01293f0 100644 --- a/app/models/ability.rb +++ b/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 diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 0000000..e7a5b90 --- /dev/null +++ b/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 diff --git a/app/models/user.rb b/app/models/user.rb index 9b67e65..9de57d2 100644 --- a/app/models/user.rb +++ b/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 diff --git a/app/models/user_role.rb b/app/models/user_role.rb deleted file mode 100644 index 5a9452f..0000000 --- a/app/models/user_role.rb +++ /dev/null @@ -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 diff --git a/app/models/user_role_join.rb b/app/models/user_role_join.rb new file mode 100644 index 0000000..f15c109 --- /dev/null +++ b/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 diff --git a/db/migrate/20130423231937_alter_user_roles.rb b/db/migrate/20130423231937_alter_user_roles.rb new file mode 100644 index 0000000..bc046c1 --- /dev/null +++ b/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 diff --git a/db/migrate/20130423233228_add_roles.rb b/db/migrate/20130423233228_add_roles.rb new file mode 100644 index 0000000..f82f955 --- /dev/null +++ b/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 diff --git a/db/migrate/20130424005701_alter_user.rb b/db/migrate/20130424005701_alter_user.rb new file mode 100644 index 0000000..510d717 --- /dev/null +++ b/db/migrate/20130424005701_alter_user.rb @@ -0,0 +1,5 @@ +class AlterUser < ActiveRecord::Migration + def change + remove_column :users, :user_role_id + end +end diff --git a/db/schema.rb b/db/schema.rb index fcb1f7c..6376f3b 100644 --- a/db/schema.rb +++ b/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 diff --git a/db/seed/fixtures/roles.yml b/db/seed/fixtures/roles.yml new file mode 100644 index 0000000..46f26fc --- /dev/null +++ b/db/seed/fixtures/roles.yml @@ -0,0 +1,9 @@ +user: + id: 1 + role: user +staff: + id: 2 + role: staff +admin: + id: 3 + role: admin diff --git a/db/seeds.rb b/db/seeds.rb index 6d1f3c2..89a9000 100644 --- a/db/seeds.rb +++ b/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) diff --git a/spec/factories/user_roles.rb b/spec/factories/roles.rb similarity index 91% rename from spec/factories/user_roles.rb rename to spec/factories/roles.rb index ac4d2f3..97ae258 100644 --- a/spec/factories/user_roles.rb +++ b/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 diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 53c85a8..507e253 100644 --- a/spec/factories/users.rb +++ b/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 From a083ed715b3fec09f758f9ee80b9ec408d73f693 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Wed, 24 Apr 2013 07:56:02 -0400 Subject: [PATCH 2/9] Renamed UserRoles to UserRole Joins, Needed ID column in user_role_joins table --- app/components/app_tab_panel.rb | 3 +-- app/components/{user_roles.rb => user_role_joins.rb} | 6 +++--- app/models/user_role_join.rb | 2 +- db/migrate/20130423231937_alter_user_roles.rb | 1 - db/schema.rb | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) rename app/components/{user_roles.rb => user_role_joins.rb} (54%) diff --git a/app/components/app_tab_panel.rb b/app/components/app_tab_panel.rb index 60fbb4f..6a0daed 100644 --- a/app/components/app_tab_panel.rb +++ b/app/components/app_tab_panel.rb @@ -37,7 +37,7 @@ class AppTabPanel < Netzke::Basepack::TabPanel wrappedComponent: :transactions_border, title: "Transactions"}, :logs, - :user_roles + :user_role_joins ] end @@ -48,7 +48,6 @@ class AppTabPanel < Netzke::Basepack::TabPanel self.class.component item[:wrappedComponent] end end - c.prevent_header = true c.tbar = [:sign_out] c.items = @@app_tab_panel_items diff --git a/app/components/user_roles.rb b/app/components/user_role_joins.rb similarity index 54% rename from app/components/user_roles.rb rename to app/components/user_role_joins.rb index bd81705..40442a1 100644 --- a/app/components/user_roles.rb +++ b/app/components/user_role_joins.rb @@ -1,10 +1,10 @@ -class UserRoles < Netzke::Basepack::Grid - +class UserRoleJoins < Netzke::Basepack::Grid def configure(c) super c.model = "UserRoleJoin" c.title = "User Roles" - #c.columns = [ :user__first_name, :role__role, :created_at, :updated_at, :ends ] + c.columns = [ :user__first_name, :role__role, :created_at, :updated_at, :ends ] + #c.columns = [ :user__first_name] end #override with nil to remove actions diff --git a/app/models/user_role_join.rb b/app/models/user_role_join.rb index f15c109..64b961a 100644 --- a/app/models/user_role_join.rb +++ b/app/models/user_role_join.rb @@ -1,5 +1,5 @@ class UserRoleJoin < ActiveRecord::Base - #set_table_name :user_role_joins + set_table_name :user_role_joins attr_accessible :role_id, :user_id belongs_to :user diff --git a/db/migrate/20130423231937_alter_user_roles.rb b/db/migrate/20130423231937_alter_user_roles.rb index bc046c1..43f4b82 100644 --- a/db/migrate/20130423231937_alter_user_roles.rb +++ b/db/migrate/20130423231937_alter_user_roles.rb @@ -3,7 +3,6 @@ class AlterUserRoles < ActiveRecord::Migration 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 diff --git a/db/schema.rb b/db/schema.rb index 6376f3b..c6d3273 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -154,7 +154,7 @@ ActiveRecord::Schema.define(:version => 20130424005701) do t.datetime "updated_at", :null => false end - create_table "user_role_joins", :id => false, :force => true do |t| + create_table "user_role_joins", :force => true do |t| t.string "role_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false From 6f6902dd829979380e5fcfc80fa7b7962d20aed9 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Thu, 25 Apr 2013 21:25:49 -0400 Subject: [PATCH 3/9] Update Ability to get user roles, not role --- app/models/ability.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 01293f0..7bf89a3 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -3,7 +3,9 @@ class Ability def initialize(current_user) @current_user = current_user - self.send(current_user.role.role.to_sym) + current_user.roles.each do |role| + self.send(role.role.to_sym) + end end def admin From a614b285589d14457a2e75567900adcffbb0d193 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Thu, 25 Apr 2013 21:38:55 -0400 Subject: [PATCH 4/9] Make it possible to limit roles by date --- app/models/user.rb | 2 +- app/models/user_role_join.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 9de57d2..17be074 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,7 +14,7 @@ class User < ActiveRecord::Base has_many :user_profiles accepts_nested_attributes_for :user_profiles, allow_destroy: false - has_many :user_role_joins + has_many :user_role_joins, :conditions => ["ends IS NULL OR ends > ?", Time.now] has_many :roles, through: :user_role_joins belongs_to :bike diff --git a/app/models/user_role_join.rb b/app/models/user_role_join.rb index 64b961a..f55b93c 100644 --- a/app/models/user_role_join.rb +++ b/app/models/user_role_join.rb @@ -1,6 +1,6 @@ class UserRoleJoin < ActiveRecord::Base set_table_name :user_role_joins - attr_accessible :role_id, :user_id + attr_accessible :role_id, :user_id, :ends belongs_to :user belongs_to :role From 8f988b2cf1644183a6d3113553679f4b5d97918d Mon Sep 17 00:00:00 2001 From: "John N. Milner" Date: Fri, 10 May 2013 03:18:36 -0400 Subject: [PATCH 5/9] Use `self.table_name = ` instead of set_table_name to avoid deprecation warning --- app/models/user_role_join.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user_role_join.rb b/app/models/user_role_join.rb index f55b93c..b1d285d 100644 --- a/app/models/user_role_join.rb +++ b/app/models/user_role_join.rb @@ -1,5 +1,5 @@ class UserRoleJoin < ActiveRecord::Base - set_table_name :user_role_joins + self.table_name = :user_role_joins attr_accessible :role_id, :user_id, :ends belongs_to :user From f34def2c09a669c8e260d2484a08478b62bf3424 Mon Sep 17 00:00:00 2001 From: darmock Date: Fri, 10 May 2013 03:30:54 -0400 Subject: [PATCH 6/9] Store role_id as an integer instead of a string --- db/migrate/20130423231937_alter_user_roles.rb | 1 + db/schema.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/db/migrate/20130423231937_alter_user_roles.rb b/db/migrate/20130423231937_alter_user_roles.rb index 43f4b82..329d470 100644 --- a/db/migrate/20130423231937_alter_user_roles.rb +++ b/db/migrate/20130423231937_alter_user_roles.rb @@ -3,6 +3,7 @@ class AlterUserRoles < ActiveRecord::Migration 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 diff --git a/db/schema.rb b/db/schema.rb index c6d3273..529fac7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -155,7 +155,7 @@ ActiveRecord::Schema.define(:version => 20130424005701) do end create_table "user_role_joins", :force => true do |t| - t.string "role_id" + t.integer "role_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.datetime "ends" From bc1eeea16e2e700599bcf397e7ccd3f8c8e19834 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Thu, 16 May 2013 22:41:00 -0400 Subject: [PATCH 7/9] Make the title of user roles tab --- app/components/app_tab_panel.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/components/app_tab_panel.rb b/app/components/app_tab_panel.rb index 6a0daed..98d6f18 100644 --- a/app/components/app_tab_panel.rb +++ b/app/components/app_tab_panel.rb @@ -37,7 +37,9 @@ class AppTabPanel < Netzke::Basepack::TabPanel wrappedComponent: :transactions_border, title: "Transactions"}, :logs, - :user_role_joins + { layout: :fit, + wrappedComponent: :user_role_joins, + title: "User Roles"} ] end From 81217f1ea80d46e0dec9c070894d9548a496e8cf Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Thu, 16 May 2013 22:41:31 -0400 Subject: [PATCH 8/9] Give better names to columns, add last name col --- app/components/user_role_joins.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/components/user_role_joins.rb b/app/components/user_role_joins.rb index 40442a1..ba61e66 100644 --- a/app/components/user_role_joins.rb +++ b/app/components/user_role_joins.rb @@ -2,9 +2,15 @@ class UserRoleJoins < Netzke::Basepack::Grid def configure(c) super c.model = "UserRoleJoin" + c.header = false c.title = "User Roles" - c.columns = [ :user__first_name, :role__role, :created_at, :updated_at, :ends ] - #c.columns = [ :user__first_name] + 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 From 65c8cf8cf10d372424e076547ff45a5197f48b0b Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Thu, 16 May 2013 22:44:27 -0400 Subject: [PATCH 9/9] Fix setting default log action --- app/components/user_logs.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/components/user_logs.rb b/app/components/user_logs.rb index c7780b2..63dbf8e 100644 --- a/app/components/user_logs.rb +++ b/app/components/user_logs.rb @@ -37,7 +37,7 @@ class UserLogs < Netzke::Basepack::Grid { :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}, :description, - { :name => :user_action__action, :text => 'Action' }, + { :name => :user_action__action, :text => 'Action', :default_value => ::ActsAsLoggable::UserAction.all.first.id }, :created_at, :updated_at, { :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] } 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 = ::ActsAsLoggable::UserAction.all.first.id [ { :name => :start_date}, { :name => :end_date}, { :name => :description}, #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 => [ {:xtype => 'combo', :no_binding => true, :name => :copy_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store, :value => bike_id} ]