2012-10-08 47 views
2

限制用戶所在列的行數我正在構建一個呼叫跟蹤應用程序,作爲學習rails和twilio的一種方式。根據參數

現在,我有模型方案計劃has_many用戶has_many手機。

在計劃模型中,我有一個名爲max_phone_numbers的參數。

我想要做的就是根據計劃給出的max_phone_numbers限制用戶擁有的電話數量。

流程看起來是這樣的:

1)用戶購買了一堆電話號碼 2)的當User.phones.count = max_phone號碼,然後購買多個電話號碼能力被禁止,並且鏈接彈出到upgrade_path

我不太清楚我會如何去做這件事。我需要在我的模型和我的控制器中做什麼組合?

我會在我的控制器中定義什麼,以這種方式在視圖中,如果/然後圍繞按鈕聲明,我可以扭曲?
即如果達到極限,比證明這一點,否則顯示按鈕

我把我的模型是什麼,以防止剛訪問的鏈接,而不是別人?

任何指導,或者在做這樣的事情的資源,將不勝感激

這裏是我當前的用戶模型

# == Schema Information 
# 
# Table name: users 
# 
# id     :integer   not null, primary key 
# name     :string(255) 
# email     :string(255) 
# created_at   :datetime   not null 
# updated_at   :datetime   not null 
# password_digest  :string(255) 
# remember_token  :string(255) 
# twilio_account_sid :string(255) 
# twilio_auth_token  :string(255) 
# plan_id    :integer 
# stripe_customer_token :string(255) 
# 

# Twilio authentication credentials 

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation, :plan_id, :stripe_card_token 
    has_secure_password 
    belongs_to :plan 
    has_many :phones, dependent: :destroy 

    before_save { |user| user.email = email.downcase } 
    before_save :create_remember_token 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: true 

    validates :password, presence: true, length: { minimum: 6 }, on: :create 
    validates :password_confirmation, presence: true, on: :create 
    validates_presence_of :plan_id 

    attr_accessor :stripe_card_token 

    def save_with_payment 
    if valid? 
     customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token) 
     self.stripe_customer_token = customer.id 
     save! 
    end 
    rescue Stripe::InvalidRequestError => e 
    logger.error "Stripe error while creating customer: #{e.message}" 
    errors.add :base, "There was a problem with your credit card." 
    false 
    end 

    def create_twilio_subaccount  
     @client = Twilio::REST::Client.new(TWILIO_PARENT_ACCOUNT_SID, TWILIO_PARENT_ACCOUNT_TOKEN) 
     @subaccount = @client.accounts.create({:FriendlyName => self[:email]}) 
     self.twilio_account_sid = @subaccount.sid 
     self.twilio_auth_token = @subaccount.auth_token 
     save! 
    end 

    private 

     def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
     end 


end 

回答

3

你可以自定義驗證添加到您的手機型號來檢查用戶已達到其極限。這可以防止在用戶達到其限制時創建任何新電話。

在User類

def at_max_phone_limit? 
    self.phones.count >= self.plan.max_phone_numbers 
end 

在手機類

validate :check_phone_limit, :on => :create 

def check_phone_limit 
    if User.find(self.user_id).at_max_phone_limit? 
    self.errors[:base] << "Cannot add any more phones" 
    end 
end 

在你看來/形式,你會做這樣的事情

<% if @user.at_max_phone_limit? %> 
    <%= link_to "Upgrade your Plan", upgrade_plan_path %> 
<% else %> 
    # Render form/widget/control for adding a phone number 
<% end %> 
+0

Zeikt,這就是天才。非常感謝幫助解構這個。我會在今天或明天實施它,讓你知道它是如何發展的。 –