diff --git a/lib/jam_ruby/models/user.rb b/lib/jam_ruby/models/user.rb index fe4a60485..d4db360c8 100644 --- a/lib/jam_ruby/models/user.rb +++ b/lib/jam_ruby/models/user.rb @@ -3,7 +3,7 @@ module JamRuby include Tire::Model::Search include Tire::Model::Callbacks - attr_accessible :name, :email, :password, :password_confirmation + attr_accessible :first_name, :last_name, :name, :email, :password, :password_confirmation attr_accessor :updating_password self.primary_key = 'id' @@ -32,7 +32,7 @@ module JamRuby # band followings has_many :band_followings, :class_name => "JamRuby::BandFollower", :foreign_key => "follower_id" - has_many :inverse_band_followings, :through => :band_followings, :foreign_key => "band_id" + has_many :inverse_band_followings, :through => :band_followings, :class_name => "JamRuby::Band", :foreign_key => "band_id" # favorites (needs Recording model) @@ -59,6 +59,8 @@ module JamRuby after_save :limit_to_five_instruments + validates :first_name, presence: true, length: {maximum: 50} + validates :last_name, presence: true, length: {maximum: 50} validates :name, presence: true, length: {maximum: 50} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, @@ -88,6 +90,10 @@ module JamRuby updating_password || new_record? end + #def name + # return self.first_name + " " + self.last_name + #end + def friends?(user) return self.friends.exists?(user) end @@ -118,7 +124,17 @@ module JamRuby user = User.find(params[:id]) end - # name + # first name + unless params[:first_name].nil? + user.first_name = params[:first_name] + end + + # last name + unless params[:last_name].nil? + user.last_name = params[:last_name] + end + + # username unless params[:name].nil? user.name = params[:name] end @@ -200,7 +216,8 @@ module JamRuby def to_indexed_json { - :name => name, + :first_name => first_name, + :last_name => last_name, :photo_url => photo_url, :location => location, :musician => musician @@ -217,8 +234,9 @@ module JamRuby :properties => { :photo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false}, :location => { :type => :string }, - :name => { :type => :string, :boost => 100 }, - :is_musician => { :type => :boolean, :index => :not_analyzed, :include_in_all => false} + :first_name => { :type => :string, :boost => 100 }, + :last_name => { :type => :string, :boost => 100 }, + :musician => { :type => :boolean, :index => :not_analyzed, :include_in_all => false} } } } diff --git a/spec/jam_ruby/models/search_spec.rb b/spec/jam_ruby/models/search_spec.rb index 3733fc5f1..90a1545e7 100644 --- a/spec/jam_ruby/models/search_spec.rb +++ b/spec/jam_ruby/models/search_spec.rb @@ -11,9 +11,9 @@ describe Search do def create_peachy_data - @user = User.save(name: "Peach", email: "user@example.com", + @user = User.save(first_name: "Peach", last_name: "Pit", name: "peachy", email: "user@example.com", password: "foobar", password_confirmation: "foobar", musician: true) - @fan = User.save(name: "Peach Peach", email: "fan@example.com", + @fan = User.save(first_name: "Peach Peach", last_name: "Pit", name: "peachy", email: "fan@example.com", password: "foobar", password_confirmation: "foobar", musician: false) @band = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock") end diff --git a/spec/jam_ruby/models/tire_search_spec.rb b/spec/jam_ruby/models/tire_search_spec.rb index 237c87204..099bb17d6 100644 --- a/spec/jam_ruby/models/tire_search_spec.rb +++ b/spec/jam_ruby/models/tire_search_spec.rb @@ -20,7 +20,7 @@ describe "tire search" do end it "full search for single user" do - @user = User.save(name: "User One", email: "user@example.com", + @user = User.save(first_name: "User", last_name: "One", name: "test", email: "user@example.com", password: "foobar", password_confirmation: "foobar", musician: true) User.search_index.refresh @@ -49,7 +49,7 @@ describe "tire search" do end it "full search for a band & user" do - @user = User.save(name: "Peach", email: "user@example.com", + @user = User.save(first_name: "Peach", last_name: "Foo", name: "test", email: "user@example.com", password: "foobar", password_confirmation: "foobar", musician: true) @band = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock") User.search_index.refresh @@ -62,16 +62,16 @@ describe "tire search" do s.results.length.should == 2 result = s.results[0] - result.should be_a_kind_of User - result.id.should == @user.id - result = s.results[1] result.should be_a_kind_of Band result.id.should == @band.id + result = s.results[1] + result.should be_a_kind_of User + result.id.should == @user.id end it "pagination" do - @user = User.save(name: "Peach", email: "user@example.com", + @user = User.save(first_name: "Peach", last_name: "foo", name: "test", email: "user@example.com", password: "foobar", password_confirmation: "foobar", musician: true) @band = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock") User.search_index.refresh @@ -86,8 +86,8 @@ describe "tire search" do s.results.length.should == 1 result = s.results[0] - result.should be_a_kind_of User - result.id.should == @user.id + result.should be_a_kind_of Band + result.id.should == @band.id s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do query { string 'peach' } @@ -98,8 +98,8 @@ describe "tire search" do s.results.length.should == 1 result = s.results[0] - result.should be_a_kind_of Band - result.id.should == @band.id + result.should be_a_kind_of User + result.id.should == @user.id end it "should fail to search deleted index" do @@ -113,7 +113,7 @@ describe "tire search" do s.results.total.should == 0 - @user = User.save(name: "Peach", email: "user@example.com", + @user = User.save(first_name: "Peach", last_name: "Pit", name: "peachy", email: "user@example.com", password: "foobar", password_confirmation: "foobar", musician: true) User.search_index.refresh sleep 1 # https://jamkazam.atlassian.net/browse/VRFS-69 diff --git a/spec/jam_ruby/models/user_search_spec.rb b/spec/jam_ruby/models/user_search_spec.rb index 4fa012121..e7ec9ca8a 100644 --- a/spec/jam_ruby/models/user_search_spec.rb +++ b/spec/jam_ruby/models/user_search_spec.rb @@ -6,7 +6,7 @@ describe User do User.delete_search_index User.create_search_index - @user = User.save(name: "Example User", email: "user@example.com", + @user = User.save(first_name: "Example", last_name: "User", name: "peachy", email: "user@example.com", password: "foobar", password_confirmation: "foobar", musician: true) # you have to poke elasticsearch because it will batch requests internally for a second @@ -18,7 +18,8 @@ describe User do ws.results.length.should == 1 user_result = ws.results[0] user_result._type.should == "jam_ruby/user" - user_result.name.should == @user.name + user_result.first_name.should == @user.first_name + user_result.last_name.should == @user.last_name user_result.id.should == @user.id user_result.location.should == @user.location user_result.musician.should == true @@ -44,7 +45,8 @@ describe User do user_result = ws.results[0] user_result.id.should == @user.id - @user.name = "bonus-junk" + @user.first_name = "bonus-junk" + @user.last_name = "more-junk" @user.save User.search_index.refresh @@ -55,11 +57,11 @@ describe User do ws.results.length.should == 1 user_result = ws.results[0] user_result.id.should == @user.id - user_result.name.should == "bonus-junk" + user_result.first_name.should == "bonus-junk" end it "should tokenize correctly" do - @user2 = User.save(name: "peaches", email: "peach@example.com", + @user2 = User.save(first_name: "peaches", last_name: "test", name: "peachy", email: "peach@example.com", password: "foobar", password_confirmation: "foobar", musician: true) User.search_index.refresh ws = User.search("pea") diff --git a/spec/jam_ruby/models/user_spec.rb b/spec/jam_ruby/models/user_spec.rb index 8250064f8..97b651353 100644 --- a/spec/jam_ruby/models/user_spec.rb +++ b/spec/jam_ruby/models/user_spec.rb @@ -3,13 +3,14 @@ require 'spec_helper' describe User do before do - @user = User.new(name: "Example User", email: "user@example.com", + @user = User.new(first_name: "Example", last_name: "User", name: "test", email: "user@example.com", password: "foobar", password_confirmation: "foobar") end subject { @user } - it { should respond_to(:name) } + it { should respond_to(:first_name) } + it { should respond_to(:last_name) } it { should respond_to(:email) } it { should respond_to(:password_digest) } it { should respond_to(:password) } @@ -39,8 +40,13 @@ describe User do it { should be_admin } end - describe "when name is not present" do - before { @user.name = " " } + describe "when first name is not present" do + before { @user.first_name = " " } + it { should_not be_valid } + end + + describe "when last name is not present" do + before { @user.last_name = " " } it { should_not be_valid } end @@ -49,8 +55,13 @@ describe User do it { should_not be_valid } end - describe "when name is too long" do - before { @user.name = "a" * 51 } + describe "when first name is too long" do + before { @user.first_name = "a" * 51 } + it { should_not be_valid } + end + + describe "when last name is too long" do + before { @user.last_name = "a" * 51 } it { should_not be_valid } end