2

I'm building a call-tracking application as a way to learn rails and twilio.

Right now, I have the model scheme plans has_many users has_many phones.

In the plans model, I have a parameter called max_phone_numbers.

What I'd like to do is to limit the number of phones a user has based on the max_phone_numbers the plan gives.

The flow looks something like this :

1) User buys a bunch of phone numbers 2)When User.phones.count = max_phone numbers, then ability to buy more phone numbers is disabled, and a link pops up to the upgrade_path

I'm not quite sure how I would go about doing this though. What are the combinations of things I would need to do in my model, and in my controller?

What would I define in my controller, in such a way that in the view I can warp if/then statements around the buttons?
i.e if limit is reached, than show this, else show button

What would I put in my models to prevent someone from just visiting the link instead?

Any guidance, or resources on doing something like this would be greatly appreciated

Here's my current user model

# == 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+\-.]+@[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

1 Answer 1

3

You could add a custom validation to your Phone model to check if a user has reached their limit. That would prevent any new Phone's from being created if the user has reached their limit.

In your User class

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

In your Phone class

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

In your view/form, you would do something like this

<% 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 %>
1
  • Zeikt, this is genius. Thanks so much for helping deconstruct this. I'll implement it today or tomorrow and let you know how it goes. Oct 8, 2012 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.