From af8bee608448259bf6d7aea1876805ad01b7092a Mon Sep 17 00:00:00 2001 From: Seth Call Date: Sun, 9 Dec 2012 14:56:35 -0600 Subject: [PATCH] * VRFS-149; bootstrap dev users --- lib/jam_ruby/models/user.rb | 61 +++++++++++++++++++++++++++++++ spec/jam_ruby/models/user_spec.rb | 23 ++++++++++++ 2 files changed, 84 insertions(+) diff --git a/lib/jam_ruby/models/user.rb b/lib/jam_ruby/models/user.rb index 6dae3823a..c3a6388f2 100644 --- a/lib/jam_ruby/models/user.rb +++ b/lib/jam_ruby/models/user.rb @@ -389,6 +389,67 @@ module JamRuby return user end + # this is intended to be development-mode or test-mode only; VRFS-149 + # it creates or updates one user per developer, so that we aren't in the business + # of constantly recreating users as we create new dev environments + + # We guard against this code running in production mode, + # because otherwise it's a bit of uncomfortable code + # to have sitting around + def self.create_dev_user(first_name, last_name, email, password, + city, state, country, instruments, photo_url) + + if Environment.mode == "production" + # short-circuit out + return + end + + user = User.find_or_create_by_email(email) + + User.transaction do + user.first_name = first_name + user.last_name = last_name + user.email = email + user.password = password + user.password_confirmation = password + user.admin = true + user.email_confirmed = true + user.musician = true + user.city = city + user.state = state + user.country = country + + if instruments.nil? + instruments = [{:instrument_id => "acoustic guitar", :proficiency_level => 3, :priority => 1}] + end + + unless user.new_record? + MusicianInstrument.delete_all(["user_id = ?", user.id]) + end + + instruments.each do |musician_instrument_param| + instrument = Instrument.find(musician_instrument_param[:instrument_id]) + musician_instrument = MusicianInstrument.new + musician_instrument.user = user + musician_instrument.instrument = instrument + musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level] + musician_instrument.priority = musician_instrument_param[:priority] + musician_instrument.save + user.musician_instruments << musician_instrument + end + + user.photo_url = photo_url + user.signup_token = nil + user.save + + if user.errors.any? + raise ActiveRecord::Rollback + end + end + + return user + end + # throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user def self.signup_confirm(signup_token) if signup_token.nil? || signup_token.empty? diff --git a/spec/jam_ruby/models/user_spec.rb b/spec/jam_ruby/models/user_spec.rb index bb20bb641..f3d88acd7 100644 --- a/spec/jam_ruby/models/user_spec.rb +++ b/spec/jam_ruby/models/user_spec.rb @@ -169,4 +169,27 @@ describe User do it { User.authenticate("", "").should be_nil } end end + + describe "create_dev_user" do + before { @dev_user = User.create_dev_user("Seth", "Call", "seth@jamkazam.com", "Jam123", "Austin", "Texas", "USA", nil, nil) } + + subject { @dev_user } + + describe "creates a valid record" do + it { should be_valid } + end + + describe "should not be a new record" do + it { should be_persisted } + end + + describe "updates record" do + before { @dev_user = User.create_dev_user("Seth", "Call2", "seth@jamkazam.com", "Jam123", "Austin", "Texas", "USA", nil, nil) } + + it { should be_valid } + + its(:last_name) { should == "Call2" } + + end + end end \ No newline at end of file