Skip to content

Commit

Permalink
Users must have a profile to be valid
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgrathwell committed Mar 5, 2015
1 parent 81adc37 commit c23a2e9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Profile < ActiveRecord::Base
PERMITTED_ATTRIBUTES = [:childcaring, :designing, :outreach, :linux, :macosx, :mentoring, :other, :user_id, :windows, :writing, :bio, :github_username]
PERMITTED_ATTRIBUTES = [:childcaring, :designing, :outreach, :linux, :macosx, :mentoring, :other, :windows, :writing, :bio, :github_username]

belongs_to :user, inverse_of: :profile

validates_presence_of :user_id
validates_presence_of :user
validates_uniqueness_of :user_id
end
13 changes: 3 additions & 10 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class User < ActiveRecord::Base
PERMITTED_ATTRIBUTES = [:first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :time_zone, :gender, :allow_event_email]

after_create :make_empty_profile
before_validation :build_profile, on: :create

devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable,
Expand All @@ -12,12 +12,12 @@ class User < ActiveRecord::Base
has_many :events, -> { where published: true }, through: :rsvps
has_many :chapter_leaderships

has_one :profile, dependent: :destroy, inverse_of: :user
has_one :profile, dependent: :destroy, inverse_of: :user, validate: true
has_and_belongs_to_many :chapters

accepts_nested_attributes_for :profile

validates_presence_of :first_name, :last_name
validates_presence_of :first_name, :last_name, :profile
validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.all.map(&:name), allow_blank: true

def self.from_omniauth(omniauth)
Expand Down Expand Up @@ -71,11 +71,4 @@ def event_attendances
def event_role(event)
event_attendances.fetch(event.id, {})[:role]
end

private

def make_empty_profile
self.build_profile
self.profile.save
end
end
2 changes: 1 addition & 1 deletion spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Profile do
it { should belong_to(:user) }
it { should validate_presence_of(:user_id) }
it { should validate_presence_of(:user) }
it { should validate_uniqueness_of(:user_id) }
end

0 comments on commit c23a2e9

Please sign in to comment.