From e84f1a9a297491d30f70970f2847cd5e79379a1d Mon Sep 17 00:00:00 2001 From: David Wilkie Date: Fri, 30 Jan 2015 11:23:01 +0700 Subject: [PATCH] Add AASM for users. #108 --- app/models/user.rb | 41 +++++++++++++++++++--------------------- lib/tasks/users.rake | 2 +- spec/factories.rb | 2 +- spec/models/user_spec.rb | 4 ++-- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 9736191..22988ba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,6 +14,8 @@ def validate_each(record, attribute, value) include Chibi::Twilio::ApiHelpers include Chibi::Communicable::HasCommunicableResources + include AASM + has_communicable_resources :phone_calls, :messages, :replies MALE = "m" @@ -61,25 +63,31 @@ def validate_each(record, attribute, value) delegate :city, :country_code, :to => :location, :allow_nil => true - state_machine :initial => :online do - state :offline, :searching_for_friend, :unactivated - - after_transition :unactivated => :online, :do => :touch_activated_at + aasm :column => :state, :whiny_transitions => false do + state :online, :initial => true + state :offline + state :searching_for_friend + state :unactivated event :login do - transition([:offline, :unactivated] => :online) + transitions(:from => :offline, :to => :online) + transitions(:from => :unactivated, :to => :online, :after => :tourch_activated_at) end event :logout do - transition(any => :offline) + transitions(:to => :offline, :after => :deactivate_chats!) end event :search_for_friend do - transition(any => :searching_for_friend, :unless => lambda {|user| user.currently_chatting?}) + transitions(:to => :searching_for_friend) do + guard do + !currently_chatting? + end + end end event :cancel_searching_for_friend do - transition(:searching_for_friend => :online) + transitions(:from => :searching_for_friend, :to => :online) end end @@ -373,22 +381,11 @@ def reply_not_enough_credit! replies.build.not_enough_credit! end - def login! - fire_events(:login) - end - - def logout! + def deactivate_chats! if currently_chatting? partner = active_chat.partner(self) active_chat.deactivate!(:active_user => partner) end - - fire_events(:logout) - end - - def search_for_friend! - fire_events(:search_for_friend) - nil end def matches @@ -620,7 +617,7 @@ def self.from_registered_service_providers end def self.inactive_timestamp(options = {}) - (options[:inactivity_period] || 5.days).ago + options[:inactivity_period] || 5.days.ago end def self.not_contacted_recently(inactivity_timestamp) @@ -705,7 +702,7 @@ def torasup_number end def cancel_searching_for_friend_if_chatting - fire_events(:cancel_searching_for_friend) if currently_chatting? + cancel_searching_for_friend! if currently_chatting? nil end diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 1ca925c..6aabd9e 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -1,7 +1,7 @@ namespace :users do desc "Reminds users without recent interaction to use Chibi" task :remind => :environment do - Resque.enqueue(Reminderer, :limit => 300, :inactivity_period => 24.hours, :between => 6..24) + Resque.enqueue(Reminderer, :limit => 300, :inactivity_period => 24.hours.ago, :between => 6..24) end desc "Finds new friends for users who are searching" diff --git a/spec/factories.rb b/spec/factories.rb index 689c705..996a8ea 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -390,7 +390,7 @@ end trait :not_contacted_for_a_short_time do - updated_at { 3.days.ago } + updated_at { 4.days.ago } end trait :from_registered_service_provider do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9c39c9b..7f51266 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -637,9 +637,9 @@ def assert_not_reminded assert_reminded end - context "passing :inactivity_period => 3.days" do + context "passing :inactivity_period => 3.days.ago" do it "should remind users that have not been contacted in the last 3 days" do - do_remind(:inactivity_period => 3.days) + do_remind(:inactivity_period => 3.days.ago) assert_reminded assert_user_reminded(registered_sp_user_not_contacted_for_a_short_time) end