Permalink
Browse files

Merge pull request #1 from amseledka/invite_and_ajax_calendar_fixes

Invite and ajax calendar fixes
  • Loading branch information...
2 parents 5ae2abf + e425a94 commit 96488113fd3f8ce1ead10940c1806b2cd625408d @amseledka committed Oct 27, 2011
@@ -4,4 +4,9 @@ def index
@calendar_records = @user.calendar_records.all
end
+ def create
+ end
+
+ def destroy
+ end
end
@@ -11,20 +11,16 @@ def index
def new
@user = User.new
+ session[:invite_code] = params.delete(:invite_code) #invite code is passed here, not in create
end
def create
@user = User.new(params[:user])
- @invite = Invite.find_redeemable(params[:invite_code])
- if @invite
- @user.save
- if @invite.redeemed!
- redirect_to(:root, :notice => 'Успешная регистрация.')
- else
- render(:action => "new", :notice => "Зарегестироваться можно только если вас пригласили")
- end
+ @user.invite = Invite.find_redeemable(session[:invite_code])
+ if @user.save
+ redirect_to(:root, :notice => 'Успешная регистрация.')
else
- render(:action => "new", :notice => "Зарегестироваться можно только если вас пригласили")
+ render(:action => "new", :notice => "Зарегистироваться можно только если вас пригласили")
end
end
@@ -44,4 +40,4 @@ def update
def calendar
end
-end
+end
View
@@ -5,25 +5,35 @@ class Invite < ActiveRecord::Base
validates_presence_of :email, :on => :save, :message => "can't be blank"
validates_uniqueness_of :email, :on => :save, :message => "is already registered"
- named_scope :unsent_invitations, :conditions => {:redeemed_at => nil, :invite_code => nil}
+ after_create :generate_code!
+
+ scope :unsent, where(:invite_code => nil)
+ scope :not_redeemed, where(:redeemed_at => nil)
def invited?
- !!self.invite_code && !!self.invited_at
- end
-
- def invite!
- self.invite_code = Digest::SHA1.hexdigest("--#{Time.now.utc.to_s}--#{self.email}--")
- self.invited_at = Time.now.utc
- self.save!
+ invite_code?
end
def self.find_redeemable(invite_code)
- self.find(:first, :conditions => {:redeemed_at => nil, :invite_code => invite_code})
+ not_redeemed.find_by_invite_code(invite_code)
end
- def redeemed!
- self.redeemed_at = Time.now.utc
+ def redeem!
+ self.redeemed_at = Time.zone.now #respect current time zone
self.save!
end
-
+
+ def redeemed?
+ redeemed_at?
+ end
+
+ def invited_at
+ created_at
+ end
+
+ protected
+ def generate_code!
+ self.invite_code = Digest::SHA1.hexdigest("--#{Time.now.utc.to_s}--#{self.email}--")
+ save!
+ end
end
View
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ belongs_to :invite
+
acts_as_authentic do |c|
c.login_field = :email
c.validate_login_field = false
@@ -14,5 +16,14 @@ class User < ActiveRecord::Base
validates :password, :presence => true, :confirmation => true, :length => { :within => 4..40 }
validates :password_confirmation, :presence => true
validates :email, :uniqueness => true, :presence => true, :on => :create
+ validates_presence_of :invite_id
+ validates_associated :invite
+
+ after_create :redeem_invite
+
+ protected
+ def redeem_invite
+ invite.redeem!
+ end
-end
+end
View
@@ -5,4 +5,3 @@
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'yaml'
-YAML::ENGINE.yamler= 'syck'
@@ -0,0 +1,9 @@
+class AddInviteIdToUsers < ActiveRecord::Migration
+ def self.up
+ add_column :users, :invite_id, :string
+ end
+
+ def self.down
+ remove_column :users, :invite_id
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110930155435) do
+ActiveRecord::Schema.define(:version => 20111026154405) do
create_table "calendar_records", :force => true do |t|
t.datetime "date"
@@ -82,6 +82,7 @@
t.string "city"
t.string "phone"
t.string "skype"
+ t.string "invite_id"
end
end
@@ -1,10 +1,8 @@
jQuery(function($) {
- $(document).ready(function() {
- $("form.aform").ajaxForm();
- $("table.calendar td").click(function() {
- $(this).toggleClass("free");
- $(this).toggleClass("busy");
- $(this).parent().submit();
- });
+ $("form.new_calendar_record").ajaxForm();
+ $("table.calendar td").click(function() {
+ $(this).toggleClass("free");
+ $(this).toggleClass("busy");
+ $(this).find("form.new_calendar_record:first").submit();
});
});

0 comments on commit 9648811

Please sign in to comment.