diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index e8785e81c..c399f4e24 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -78,5 +78,15 @@ format.html end end + + def new_musicians(user, new_nearby, host='www.jamkazam.com') + @user, @new_nearby, @host = user, new_nearby, host + sendgrid_unique_args :type => "new_musicians" + mail(:to => user.email, :subject => "JamKazam New Musicians in Your Area") do |format| + format.text + format.html + end + end + end end diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_musicians.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_musicians.html.erb new file mode 100644 index 000000000..a6abfd81a --- /dev/null +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_musicians.html.erb @@ -0,0 +1,22 @@ +<% provide(:title, 'New JamKazam Musicians in your Area') %> + +<% link_style = "background-color:#ED3618; margin:0px 8px 0px 8px; border: solid 1px #F27861; outline: solid 2px #ED3618; padding:3px 10px; font-family:raleway; font-size:12px; font-weight:300; cursor:pointer; color:#FC9; text-decoration:none;" %> +

+ +<% @new_nearby.each do |user| %> + + + + + + +<% end %> +
<%= user.name %>
+ <%= user.location %>

+ <% user.instruments.each do |inst| %> + + <% end %> +
<%= user.biography %>

+Profile   +
+

diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_musicians.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_musicians.text.erb new file mode 100644 index 000000000..8260d4bd7 --- /dev/null +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_musicians.text.erb @@ -0,0 +1,9 @@ +<% provide(:title, 'New JamKazam Musicians in your Area') %> + +<% @new_nearby.each do |user| %> +<%= user.name %> (http://<%= @host %>/#/profile/<%= user.id %>) + <%= user.location %> + <% user.instruments.collect { |inst| inst.description }.join(', ') %> + <%= user.biography %> + +<% end %> diff --git a/ruby/lib/jam_ruby/models/instrument.rb b/ruby/lib/jam_ruby/models/instrument.rb index 6b5936e87..2764a930c 100644 --- a/ruby/lib/jam_ruby/models/instrument.rb +++ b/ruby/lib/jam_ruby/models/instrument.rb @@ -1,6 +1,35 @@ module JamRuby class Instrument < ActiveRecord::Base + MAP_ICON_NAME = { + "accordion" => "accordion", + "acoustic guitar" => "acoustic", + "banjo" => "banjo", + "bass guitar" => "bass", + "cello" => "cello", + "clarinet" => "clarinet", + "computer" => "computer", + "default" => "default", + "drums" => "drums", + "electric guitar" => "guitar", + "euphonium" => "euphonium", + "flute" => "flute", + "french horn" => "frenchhorn", + "harmonica" => "harmonica", + "keyboard" => "keyboard", + "mandolin" => "mandolin", + "oboe" => "oboe", + "other" => "other", + "saxophone" => "saxophone", + "trombone" => "trombone", + "trumpet" => "trumpet", + "tuba" => "tuba", + "ukulele" => "ukelele", + "viola" => "viola", + "violin" => "violin", + "voice" => "vocals" + } + self.primary_key = 'id' # users @@ -16,5 +45,9 @@ module JamRuby return Instrument.where('instruments.popularity > 0').order('instruments.popularity DESC, instruments.description ASC') end + def icon_name + MAP_ICON_NAME[self.id] + end + end end diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index cdfcb8d53..1372f495e 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -250,15 +250,17 @@ module JamRuby false end - def self.new_musicians(since_date=Time.now - 1.week, max_count=100, radius=M_MILES_DEFAULT) - return unless block_given? - User.where(['lat IS NOT NULL AND lng IS NOT NULL']).find_each do |usr| - rel = User.musicians - .where(['created_at >= ? AND users.id != ?', since_date, usr.id]) - .within(radius, :origin => [usr.lat, usr.lng]) - .order('created_at DESC') - .limit(max_count) - yield(rel) if 0 < rel.count + def self.new_musicians(usr, since_date=Time.now - 1.week, max_count=50, radius=M_MILES_DEFAULT) + rel = User.musicians + .where(['created_at >= ? AND users.id != ?', since_date, usr.id]) + .within(radius, :origin => [usr.lat, usr.lng]) + .order('created_at DESC') + .limit(max_count) + objs = rel.all.to_a + if block_given? + yield(objs) if 0 < objs.count + else + return objs end end diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index a07554d81..8fbdb7ed9 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -139,7 +139,8 @@ module JamRuby validate :update_email_case_insensitive_uniqueness, :if => :updating_email scope :musicians, where(:musician => true) - scope :musicians_geocoded, where(['musician = ? AND lat IS NOT NULL AND lng IS NOT NULL',true]) + scope :geocoded_users, where(['lat IS NOT NULL AND lng IS NOT NULL']) + scope :musicians_geocoded, musicians.geocoded_users def user_progression_fields @user_progression_fields ||= Set.new ["first_downloaded_client_at", "first_ran_client_at", "first_music_session_at", "first_real_music_session_at", "first_good_music_session_at", "first_certified_gear_at", "first_invited_at", "first_friended_at", "first_social_promoted_at" ] @@ -988,6 +989,14 @@ module JamRuby .order('follows.created_at DESC') .limit(3) end + + def self.deliver_new_musician_notifications(since_date=Time.now-1.week) + self.geocoded_users.find_each do |usr| + Search.new_musicians(usr, since_date) do |new_nearby| + UserMailer.new_musicians(usr, new_nearby).deliver + end + end + end # devise compatibility diff --git a/ruby/spec/jam_ruby/models/musician_search_spec.rb b/ruby/spec/jam_ruby/models/musician_search_spec.rb index e86bd5db4..6eda4ae28 100644 --- a/ruby/spec/jam_ruby/models/musician_search_spec.rb +++ b/ruby/spec/jam_ruby/models/musician_search_spec.rb @@ -204,15 +204,31 @@ describe 'Musician search' do end context 'new users' do + it "find nearby" do - # create new user to ensure its excluded from results - usr = FactoryGirl.create(:user, {city: "Austin", state: "TX", country: "US"}) - Search.new_musicians do |usrs| - # the newly created user is not nearby the existing users (which are in Apex, NC) - # and that user is not included in query - expect(usrs.count).to eq(User.musicians.count - 1) + # create new user outside 500 from Apex to ensure its excluded from results + FactoryGirl.create(:user, {city: "Austin", state: "TX", country: "US"}) + User.geocoded_users.find_each do |usr| + Search.new_musicians(usr) do |new_usrs| + # the newly created user is not nearby the existing users (which are in Apex, NC) + # and that user is not included in query + expect(new_usrs.count).to eq(User.musicians.count - 1) + end end end + + it "sends new musician email" do + # create new user outside 500 from Apex to ensure its excluded from results + FactoryGirl.create(:user, {city: "Austin", state: "TX", country: "US"}) + User.geocoded_users.find_each do |usr| + Search.new_musicians(usr) do |new_usrs| + # the newly created user is not nearby the existing users (which are in Apex, NC) + # and that user is not included in query + expect(new_usrs.count).to eq(User.musicians.count - 1) + end + end + end + end end diff --git a/ruby/spec/mailers/user_mailer_spec.rb b/ruby/spec/mailers/user_mailer_spec.rb index 77f48e0c4..30ecc52af 100644 --- a/ruby/spec/mailers/user_mailer_spec.rb +++ b/ruby/spec/mailers/user_mailer_spec.rb @@ -137,4 +137,23 @@ describe UserMailer do end + describe "sends new musicians email" do + + let(:mail) { UserMailer.deliveries[0] } + + before(:each) do + UserMailer.new_musicians(user, User.musicians).deliver + end + + it { UserMailer.deliveries.length.should == 1 } + + it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER } + it { mail['to'].to_s.should == user.email } + it { mail.multipart?.should == true } # because we send plain + html + + # verify that the messages are correctly configured + it { mail.html_part.body.include?("New JamKazam Musicians in your Area").should be_true } + it { mail.text_part.body.include?("New JamKazam Musicians in your Area").should be_true } + end + end diff --git a/web/lib/tasks/users.rake b/web/lib/tasks/users.rake new file mode 100644 index 000000000..4ad3d895f --- /dev/null +++ b/web/lib/tasks/users.rake @@ -0,0 +1,7 @@ +namespace :users do + desc "Import a maxmind database; run like this: rake db:import_maxmind_geo file=" + task new_musician_email: :environment do + User.deliver_new_musician_notifications(Time.now-12.months) + end + +end