move :preference to principal

This allows for eager loading preference. As the UserPreferences are pretty specific to users we don't want groups to actually receive a UserPreference, though. The methods added by has_one are therefore undefined on the Group model.
pull/3547/head
Jens Ulferts 9 years ago
parent 0579f4906b
commit 0b2f482797
  1. 12
      app/models/group.rb
  2. 4
      app/models/principal.rb
  3. 1
      app/models/user.rb
  4. 13
      spec/models/group_spec.rb

@ -44,6 +44,18 @@ class Group < Principal
validate :uniqueness_of_groupname
validates_length_of :groupname, maximum: 30
# HACK: We want to have the :preference association on the Principal to allow
# for eager loading preferences.
# However, the preferences are currently very user specific. We therefore
# remove the methods added by
# has_one :preference
# to avoid accidental assignment and usage of preferences on groups.
undef_method :preference,
:preference=,
:build_preference,
:create_preference,
:create_preference!
def to_s
lastname.to_s
end

@ -42,6 +42,10 @@ class Principal < ActiveRecord::Base
self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
has_one :preference,
dependent: :destroy,
class_name: 'UserPreference',
foreign_key: 'user_id'
has_many :members, foreign_key: 'user_id', dependent: :destroy
has_many :memberships, -> {
includes(:project, :roles)

@ -93,7 +93,6 @@ class User < Principal
}, class_name: 'UserPassword',
dependent: :destroy,
inverse_of: :user
has_one :preference, dependent: :destroy, class_name: 'UserPreference'
has_one :rss_token, -> {
where("action='feeds'")
}, dependent: :destroy, class_name: 'Token'

@ -83,4 +83,17 @@ describe Group, type: :model do
end
end
end
describe 'preference' do
%w{preference
preference=
build_preference
create_preference
create_preference!}.each do |method|
it "should not respond to #{method}" do
expect(group).to_not respond_to method
end
end
end
end

Loading…
Cancel
Save