Skip to content

Commit

Permalink
Add AASM for users. #108
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed Jan 30, 2015
1 parent a7680ea commit e84f1a9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
41 changes: 19 additions & 22 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/users.rake
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e84f1a9

Please sign in to comment.