Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Set invitation sender

* Add migration
* Add assocaition
* Update tests
  • Loading branch information...
commit 4169bd83fae262b6b938016db58030354f1ab52d 1 parent 35e80c2
@masonforest masonforest authored
View
4 app/controllers/invitations_controller.rb
@@ -5,7 +5,9 @@ class InvitationsController < ApplicationController
layout 'events'
def create
- @invitation = Invitation.new(params[:invitation])
+ @invitation = Invitation.new(params[:invitation].
+ merge(sender_id: current_user.id, sender_type: current_user.class.name))
+
@event = @invitation.event
if !@invitation.save
View
15 app/models/invitation.rb
@@ -1,10 +1,11 @@
class Invitation < ActiveRecord::Base
attr_accessor :skip_notification, :invitee_params
attr_accessible :event, :invitee, :invitee_attributes, :event_id,
- :skip_notification
+ :skip_notification, :sender_id, :sender_type
belongs_to :event
belongs_to :invitee, polymorphic: true
+ belongs_to :sender, polymorphic: true
accepts_nested_attributes_for :invitee
@@ -26,15 +27,15 @@ def self.invite_without_notification(event, invitee)
)
end
+ def access_token
+ event_creator.access_token
+ end
+
def build_invitee(params, options={})
self.invitee_params = params
self.invitee = find_or_create_invitee
end
- def sender
- event.owner
- end
-
def deliver_invitation
invitee.deliver_email_or_private_message(:invitation, sender, self)
end
@@ -51,10 +52,6 @@ def deliver_reminders_from(_)
private
- def access_token
- event_creator.access_token
- end
-
def create_guest
Guest.create_without_name_validation(name_or_email_param)
end
View
25 db/migrate/20121015210452_add_sender_to_invitation.rb
@@ -0,0 +1,25 @@
+class AddSenderToInvitation < ActiveRecord::Migration
+ def up
+ add_column :invitations, :sender_id, :integer
+ add_column :invitations, :sender_type, :string
+
+ select_all('SELECT id, event_id FROM invitations;').each do |row|
+ event = select_one("SELECT user_id FROM events where id=#{row['event_id']};")
+ update("UPDATE invitations SET sender_id = #{event['user_id']} where id=#{row['id']};")
+ update("UPDATE invitations SET sender_type = 'User' where id=#{row['id']};")
+ end
+
+ change_column_null :invitations, :sender_id, true
+ change_column_null :invitations, :sender_type, true
+
+ add_index :invitations, :sender_id
+ add_index :invitations, :sender_type
+ end
+
+ def down
+ remove_index :invitations, :sender_id
+ remove_index :invitations, :sender_type
+ remove_column :invitations, :sender_id
+ remove_column :invitations, :sender_type
+ end
+end
View
6 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121003163630) do
+ActiveRecord::Schema.define(:version => 20121015210452) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -71,10 +71,14 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "invitee_type", :null => false
+ t.integer "sender_id"
+ t.string "sender_type"
end
add_index "invitations", ["event_id"], :name => "index_invitations_on_event_id"
add_index "invitations", ["invitee_id"], :name => "index_invitations_on_invitee_id"
+ add_index "invitations", ["sender_id"], :name => "index_invitations_on_sender_id"
+ add_index "invitations", ["sender_type"], :name => "index_invitations_on_sender_type"
create_table "reminders", :force => true do |t|
t.integer "sender_id", :null => false
View
2  spec/factories.rb
@@ -70,7 +70,9 @@
factory :invitation do
event
association :invitee, factory: :user
+ association :sender, factory: :user
invitee_type 'User'
+ sender_type 'User'
factory :invitation_with_user do
association :invitee, factory: :user
View
2  spec/models/event_spec.rb
@@ -196,7 +196,9 @@
sender = create(:user)
guest = create(:guest)
user = create(:user)
+ sender = build_stubbed(:user)
event = create_event_with_invitees(guest, user)
+ Invitation.any_instance.stubs(sender: sender)
event.deliver_reminders_from(sender)
View
11 spec/models/invitation_spec.rb
@@ -3,6 +3,7 @@
describe Invitation do
it { should belong_to(:event) }
it { should belong_to(:invitee) }
+ it { should belong_to(:sender) }
it { should accept_nested_attributes_for :invitee }
@@ -133,7 +134,7 @@
it 'creates a User if it finds an existing Yammer user' do
invitation = create(:invitation)
- access_token = invitation.sender.access_token
+ access_token = invitation.access_token
yammer_staging = false
invitee_email = 'ralph@example.com'
invitee_user_id = 1488374236
@@ -192,14 +193,6 @@
end
end
-describe Invitation, '#sender' do
- it 'tell you who created the invitations event' do
- invitation = build_stubbed(:invitation_with_user)
-
- invitation.sender.should == invitation.event.owner
- end
-end
-
describe Invitation, '#deliver_reminders_from' do
it 'sends a reminder to the invitee' do
invitation = create(:invitation)
Please sign in to comment.
Something went wrong with that request. Please try again.