Skip to content

Commit

Permalink
Fix organization responses creation and users count
Browse files Browse the repository at this point in the history
  * Fix bug when user organization id is changed is should create
    responses for the new organization if it does not have
  * Fix bug with organization users count update
  • Loading branch information
Dalibor Nasevic and Sachin Ranchod authored and dalibor committed Oct 18, 2012
1 parent b0a31c9 commit 58c98d7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
3 changes: 1 addition & 2 deletions app/models/organization.rb
Expand Up @@ -125,8 +125,7 @@ def current_user_logged_in
# and on new Request creation
def create_data_responses!
DataRequest.all.each do |data_request|
dr = self.responses.find(:first,
:conditions => {:data_request_id => data_request.id})
dr = self.responses.where({:data_request_id => data_request.id}).first
unless dr
dr = self.responses.new
dr.data_request = data_request
Expand Down
15 changes: 9 additions & 6 deletions app/models/user.rb
Expand Up @@ -25,7 +25,10 @@ class User < ActiveRecord::Base
validates_presence_of :full_name, :organization_id

### Callbacks
after_create :create_organization_responses
after_save :create_organization_responses,
if: ->(m) { m.organization_id_changed? }
before_save :update_organization_users_counter_cache,
if: ->(m) { !m.new_record? && m.organization_id_changed? }

### Delegates
delegate :responses, :to => :organization # instead of deprecated data_response
Expand Down Expand Up @@ -58,11 +61,6 @@ def name
full_name.present? ? full_name : email
end

# assign organization association so that counter cache is updated
def organization_id=(organization_id)
self.organization = Organization.find_by_id(organization_id) if organization_id.present?
end

def generate_token
Digest::SHA1.hexdigest("#{self.email}#{Time.now}")[24..38]
end
Expand Down Expand Up @@ -108,6 +106,11 @@ def create_organization_responses
organization.create_data_responses!
end

def update_organization_users_counter_cache
Organization.decrement_counter(:users_count, self.organization_id_was)
Organization.increment_counter(:users_count, self.organization_id)
end

def only_password_errors?
errors.size == errors[:password].size + errors[:password_confirmation].size
end
Expand Down
2 changes: 2 additions & 0 deletions spec/models/organization_spec.rb
Expand Up @@ -292,6 +292,8 @@
o1 = FactoryGirl.create(:organization)
o2 = FactoryGirl.create(:organization)
reporter = FactoryGirl.create(:reporter, organization: o1)
reporter.reload # reload to get real organization association

reporter.organization.should == o1
o1.reload.users_count.should == 1
o2.reload.users_count.should == 0
Expand Down
48 changes: 43 additions & 5 deletions spec/models/user_spec.rb
Expand Up @@ -49,11 +49,49 @@
end
end

it "notifies organization it needs to create responses" do
u = FactoryGirl.build :reporter
org = u.organization
u.should_receive(:create_organization_responses).once
u.save
describe "Callbacks" do
it "creates responses when user is created" do
u = FactoryGirl.build :reporter
u.should_receive(:create_organization_responses).once
u.save!
end

it "does not create responses when user organization_id is not changed" do
u = FactoryGirl.create :reporter

u.full_name = 'another name'
u.should_not_receive(:create_organization_responses)
u.save!
end

it "created responses when user organization_id is changed" do
u = FactoryGirl.create :reporter
u.reload

org2 = FactoryGirl.create(:organization)
u.organization_id = org2.id
u.should_receive(:create_organization_responses).once
u.save!
end
end

describe "Counter caches" do
it "updates organization users count" do
org1 = FactoryGirl.create(:organization)
org2 = FactoryGirl.create(:organization)
user1 = FactoryGirl.create(:reporter, organization: org1)
user2 = FactoryGirl.create(:reporter, organization: org2)

org1.reload; org2.reload; user1.reload; user2.reload
org1.users_count.should == 1

user2.organization_id = org1.id
user2.save!

org1.reload; org2.reload
org1.users_count.should == 2
org2.users_count.should == 0
end
end

describe "save and invite" do
Expand Down

0 comments on commit 58c98d7

Please sign in to comment.