Skip to content

Commit

Permalink
Merge pull request #425 from kurenn/master
Browse files Browse the repository at this point in the history
Adds invitations_counter cache for triggering the number of invitations ...
  • Loading branch information
scambra committed Jan 30, 2014
2 parents d3441d0 + 886f6cf commit f0b3a83
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/devise_invitable/model.rb
Expand Up @@ -30,9 +30,9 @@ module Invitable
included do
include ::DeviseInvitable::Inviter
if Devise.invited_by_class_name
belongs_to :invited_by, :class_name => Devise.invited_by_class_name
belongs_to :invited_by, :class_name => Devise.invited_by_class_name, :counter_cache => :invitations_count
else
belongs_to :invited_by, :polymorphic => true
belongs_to :invited_by, :polymorphic => true, :counter_cache => :invitations_count
end

include ActiveSupport::Callbacks
Expand Down
2 changes: 2 additions & 0 deletions lib/generators/active_record/templates/migration.rb
Expand Up @@ -7,6 +7,8 @@ def up
t.datetime :invitation_accepted_at
t.integer :invitation_limit
t.references :invited_by, :polymorphic => true
t.integer :invitations_count, default: 0
t.index :invitations_count
t.index :invitation_token, :unique => true # for invitable
t.index :invited_by_id
end
Expand Down
8 changes: 8 additions & 0 deletions test/models/invitable_test.rb
Expand Up @@ -12,6 +12,14 @@ def setup
end

test 'should not generate the raw invitation token after creating a record' do
current_user = new_user
2.times do |index|
User.invite!({:email => "valid#{index}@email.com"}, current_user)
end
assert_equal current_user.reload.invitations_count, 2
end

test 'should update the invitations count counter cache' do
assert_nil new_user.raw_invitation_token
end

Expand Down
5 changes: 5 additions & 0 deletions test/rails_app/db/migrate/20100401102949_create_tables.rb
Expand Up @@ -27,15 +27,20 @@ def change
t.integer :invitation_limit
t.integer :invited_by_id
t.string :invited_by_type
t.integer :invitations_count, :default => 0

t.timestamps
end
add_index :users, :invitation_token, :unique => true
add_index :users, :invitations_count

create_table :admins do |t|
## Database authenticatable
t.string :email, :null => true, :default => ""
t.string :encrypted_password, :null => true, :default => ""

t.integer :invitations_count, :default => 0
end
add_index :admins, :invitations_count
end
end

0 comments on commit f0b3a83

Please sign in to comment.