Limit By Scope Rails Plugin

Posted by acts_as_flinn Mon, 23 Jul 2007 19:51:00 GMT

I’m pleased to announce the release of the LimitByScope plugin, the second package in the Software as a Service project.

From the README:

This plugin adds class limit, available, and capacity methods to enforce quota limits on the creation of models. The plugin is primarily for use with the acts_as_scoped plugin (part of the saas project) because it otherwise enforces a flat limit on the creation of limited models. If used with the acts_as_scoped plugin you can lock users to a scope such as host or domain then ensure they can only create the number object within their plan. The Usage section below should explain a little better.

Documentation

http://saas.rubyforge.org/limit_by_scope

Rubyforge Project

http://rubyforge.org/projects/saas

Usage


# plan attributes:
# - name
# - price
# - item_limit

class Plan < ActiveRecord::Base
  has_many :hosts
end

# host attributes:
# - name
# - plan_id

class Host < ActiveRecord::Base
  cattr_accessor :current
  belongs_to :plan
end

# item attributes
# - name
# - host_id

class Item < ActiveRecord::Base
  limit_by_scope :host, :delegate => :plan, :error => 'Quota met: #{self.class.limit}, please upgrade your plan to add more.'
  belongs_to :host
end

# for best results, use with acts_as_scoped like so:

class Item < ActiveRecord::Base
  acts_as_scoped :host
  limit_by_scope :host, :delegate => :plan, :error => 'Your service tier only allows #{self.class.limit}, please <a href=\"/upgrade?id=#{self.host.id}\">upgrade</a> your plan to add more.'
  belongs_to :host
end

class ApplicationController < ActionController::Base 
...
  before_filter :current_host

  def current_host
    Host.current = Host.find_by_name(request.subdomains.first)
    redirect_to('/not_found.html') and return false if Host.current.nil?
  end
...

Installation

script/plugin install svn://rubyforge.org/var/svn/saas/limit_by_scope/trunk/limit_by_scope

Software as a Service

The plugin is part of the Software as a Service project on Rubyforge. You can use this plugin in conjunction with something similar to the userstamp plugin to automagically limit the scope of your find, calculate, save, delete methods. It is used with other plugins as a drop in that will allow you to turn just about any rails application into a software as a service.

Acts As Scoped Rails Plugin

Posted by acts_as_flinn Thu, 19 Jul 2007 12:15:00 GMT

I’m pleased to announce the public availability of the acts_as_scoped plugin.

From the README:

This plugin wraps find in a scope based on a persistent variable. This enables you to use something like the userstamp plugin to ensure a model’s find method returns objects scoped within the current user.

Documentation

http://saas.rubyforge.org/acts_as_scoped

Rubyforge Project

http://rubyforge.org/projects/saas

Usage


# You'll need to add a user_id column to the sandwiches table

class Sandwich < ActiveRecord::Base
  acts_as_scoped :user # belongs_to is included
end

class User < ActiveRecord::Base
  cattr_accessor :current
  has_many :sandwiches
end

class ApplicationController < ActionController::Base 
...
  before_filter :current_user

  def current_user
    User.current = User.find(session[:user_id]) unless session[:user_id].blank?
  end
...

Installation

script/plugin install svn://rubyforge.org/var/svn/saas/acts_as_scoped/trunk/acts_as_scoped


Software as a Service

The plugin is part of the Software as a Service project on Rubyforge. You can use this plugin in conjunction with something similar to the userstamp plugin to automagically limit the scope of your find, calculate, save, delete methods. It is used with other plugins as a drop in that will allow you to turn just about any rails application into a software as a service.