1

これらのフィールドを一意にするように求めていますが、同じ名前と電子メールでユーザーを作成できます。

データベースを見ると、問題となるエントリが削除され、新しいエントリが作成されています。

モデル:

class User
  include Mongoid::Document
  extend Rolify
  rolify
  include Mongoid::Timestamps

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  ## Database authenticatable
  field :email,              :type => String, :default => ""
  field :encrypted_password, :type => String, :default => ""

  validates_presence_of :email
#  validates_presence_of :encrypted_password


  # override Devise method
  # no password is required when the account is created; validate password when the user sets one
  validates_confirmation_of :password
  def password_required?
    if !persisted? 
      !(password != "")
    else
      !password.nil? || !password_confirmation.nil?
    end
  end

  # override Devise method
  def confirmation_required?
    false
  end

  # override Devise method
  def active_for_authentication?
    confirmed? || confirmation_period_valid?
  end




  ## Recoverable
  field :reset_password_token,   :type => String
  field :reset_password_sent_at, :type => Time

  ## Rememberable
  field :remember_created_at, :type => Time

  ## Trackable
  field :sign_in_count,      :type => Integer, :default => 0
  field :current_sign_in_at, :type => Time
  field :last_sign_in_at,    :type => Time
  field :current_sign_in_ip, :type => String
  field :last_sign_in_ip,    :type => String

  # Confirmable
  field :confirmation_token,   :type => String
  field :confirmed_at,         :type => Time
  field :confirmation_sent_at, :type => Time
  field :unconfirmed_email,    :type => String # Only if using reconfirmable

  ## Lockable
  # field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts
  # field :unlock_token,    :type => String # Only if unlock strategy is :email or :both
  # field :locked_at,       :type => Time

  #invitable
  field :invitation_token, :type => String
  field :invitation_sent_at, :type => Time
  field :invitation_accepted_at, :type => Time
  field :invitation_limit, :type => Integer
  field :invited_by_id, :type => String
  field :invited_by_type, :type => String

  ## Token authenticatable
  # field :authentication_token, :type => String
  # run 'rake db:mongoid:create_indexes' to create indexes
  index({ email: 1 }, { unique: true, background: true })
  field :name, :type => String
  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false

  attr_accessible :role_ids, :as => :admin
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :created_at, :updated_at


  # add a welcome email sender

  after_create :send_welcome_email


  def attempt_set_password(params)
    p = {}
    p[:password] = params[:password]
    p[:password_confirmation] = params[:password_confirmation]
    update_attributes(p)
  end

  # new function to determine whether a password has been set
  def has_no_password?
    self.encrypted_password.blank?
  end

  # new function to provide access to protected method pending_any_confirmation
  def only_if_unconfirmed
    pending_any_confirmation {yield}
  end

  private

  def send_welcome_email
    unless self.email.include?('@example.com') && Rails.env != 'test'
      UserMailer.welcome_email(self).deliver
      UserMailer.newusernotify(self).deliver
    end
  end


end

サーバー ログ: (「Bob」という名前のユーザーが既に存在し、test@test.com に電子メールを送信します。「Sam」という名前で test@test.com という名前の新しいユーザーを作成するように要求した後、次のようなメッセージが表示されます。

Started POST "/users" for 127.0.0.1 at 2012-11-14 14:06:54 -0500
Processing by RegistrationsController#create as */*
  Parameters: {"user"=>{"email"=>"test@test.com", "name"=>"Sam"}}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} (6.2630ms)
  MOPED: 127.0.0.1:27017 QUERY        database=app_development collection=users selector={"$query"=>{"email"=>"test@test.com", "encrypted_password"=>""}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 fields=nil (5.0299ms)
  MOPED: 127.0.0.1:27017 DELETE       database=app_development collection=users selector={"_id"=>"50a3c974b8e764fc0d000005"} flags=[:remove_first] (0.1829ms)
before build resource
after build resource
  MOPED: 127.0.0.1:27017 QUERY        database=app_development collection=users selector={"email"=>"test@test.com"} flags=[:slave_ok] limit=1 skip=0 fields={:_id=>1} (1.9441ms)
  MOPED: 127.0.0.1:27017 QUERY        database=app_development collection=users selector={"name"=>/\ASam$/i} flags=[:slave_ok] limit=1 skip=0 fields={:_id=>1} (0.8898ms)
  MOPED: 127.0.0.1:27017 QUERY        database=app_development collection=users selector={"email"=>/\Atest@test\.com$/i} flags=[:slave_ok] limit=1 skip=0 fields={:_id=>1} (0.3989ms)
  MOPED: 127.0.0.1:27017 INSERT       database=app_development collection=users documents=[{"_id"=>"50a3ebceb8e764fc0d000006", "role_ids"=>[], "email"=>"test@test.com", "encrypted_password"=>"", "sign_in_count"=>0, "name"=>"Sam", "updated_at"=>2012-11-14 19:06:54 UTC, "created_at"=>2012-11-14 19:06:54 UTC}] flags=[] (1.3258ms)
  Rendered user_mailer/welcome_email.html.erb (0.0ms)
  Rendered user_mailer/welcome_email.text.erb (0.0ms)

また、モデルの rspec テスト (下記) に合格することにも注意してください。ユーザーを削除してから新しいユーザーを挿入するのはなぜですか?

require 'spec_helper'

describe User do

  before(:each) do
    @attr = { 
      :name => "Example User",
      :email => "user@example.com",
      :password => "foobar",
      :password_confirmation => "foobar"
    }
  end

  it "should create a new instance given a valid attribute" do
    User.create!(@attr)
  end

  it "should require an email address" do
    no_email_user = User.new(@attr.merge(:email => ""))
    no_email_user.should_not be_valid
  end

  it "should accept valid email addresses" do
    addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
    addresses.each do |address|
      valid_email_user = User.new(@attr.merge(:email => address))
      valid_email_user.should be_valid
    end
  end

  it "should reject invalid email addresses" do
    addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
    addresses.each do |address|
      invalid_email_user = User.new(@attr.merge(:email => address))
      invalid_email_user.should_not be_valid
    end
  end

  it "should reject duplicate email addresses" do
    User.create!(@attr)
    user_with_duplicate_email = User.new(@attr)
    user_with_duplicate_email.should_not be_valid
  end

  it "should reject email addresses identical up to case" do
    upcased_email = @attr[:email].upcase
    User.create!(@attr.merge(:email => upcased_email))
    user_with_duplicate_email = User.new(@attr)
    user_with_duplicate_email.should_not be_valid
  end

  describe "passwords" do

    before(:each) do
      @user = User.new(@attr)
    end

    it "should have a password attribute" do
      @user.should respond_to(:password)
    end

    it "should have a password confirmation attribute" do
      @user.should respond_to(:password_confirmation)
    end
  end

  describe "password validations" do

    it "should require a password" do
      User.new(@attr.merge(:password => "", :password_confirmation => "")).
        should_not be_valid
    end

    it "should require a matching password confirmation" do
      User.new(@attr.merge(:password_confirmation => "invalid")).
        should_not be_valid
    end

    it "should reject short passwords" do
      short = "a" * 5
      hash = @attr.merge(:password => short, :password_confirmation => short)
      User.new(hash).should_not be_valid
    end

  end

  describe "password encryption" do

    before(:each) do
      @user = User.create!(@attr)
    end

    it "should have an encrypted password attribute" do
      @user.should respond_to(:encrypted_password)
    end

    it "should set the encrypted password attribute" do
      @user.encrypted_password.should_not be_blank
    end

  end

end

更新: 登録コントローラー内にいくつかのデバッグ ステートメントを挿入しました。非常に興味深いことに、それらのどれもトリガーされません。「ビルド リソースの前」と「ビルド リソースの後」のみで、「FIRST IF」、「SECOND IF」、または「THIRD IF」はトリガーされません。これはなぜでしょうか?

  class RegistrationsController < Devise::RegistrationsController
  # override #create to respond to AJAX with a partial
    def create
      logger.debug "before build resource"
      build_resource
      logger.debug "after build resource"
      if resource.save
        if resource.active_for_authentication?
          sign_in(resource_name, resource)
          (render(:partial => 'thankyou', :layout => false) && return)  if request.xhr?
          logger.debug "FIRST IF"
          respond_with resource, :location => after_sign_up_path_for(resource)
        else
          expire_session_data_after_sign_in!
          (render(:partial => 'thankyou', :layout => false) && return)  if request.xhr?
          logger.debug "SECOND IF"
          respond_with resource, :location => after_inactive_sign_up_path_for(resource)
        end
      else
        logger.debug "THIRD IF"
        clean_up_passwords resource
        render :action => :new, :layout => !request.xhr?
      end
    end

    protected

    def after_inactive_sign_up_path_for(resource)
      # the page prelaunch visitors will see after they request an invitation
      '/thankyou.html'
    end

    def after_sign_up_path_for(resource)
      # the page new users will see after sign up (after launch, when no invitation is needed)
  #    '/thankyou.html'
      redirect_to root_path
    end

  end
4

0 に答える 0