Module: Boundary::Controller

Defined in:
lib/boundary/controller.rb

Instance Method Summary collapse

Instance Method Details

#bound_to(model, *args, &block) ⇒ Object

Adds a scope helper that call the model with a given parameter (ie: subdomain)

Options:

  • :by - Target scope model name (default: :company)

  • :scope_function - Name of a function that returns the target scope object. This object should contain an ID column that matches with the foreign_id column of the scoped object (default: :current_company)

bound_to :subscription, :by => :account, :scope_function => :current_employee

In your actions,

def show

@subscription = bound_by_account { Subscription.find(params) }
...

end

For multiple scopes,

bound_to :subscription, :by => :account, :scope_function => :current_employee bound_to :transaction, :by => :account, :scope_function => :current_employee

def show

@subscription =  { Subscription.find(params) }
@transaction  =  { Transaction.find(params) }

end



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/boundary/controller.rb', line 27

def bound_to(model, *args, &block)
  options = args.extract_options!
  
  model_name  = model.to_s.camelize
  options[:by]            ||= :company
  options[:scope_function]  ||= "current_#{options[:by]}"

  helper_name   = "#{model}_bound_by_#{options[:by]}"

  self.class_eval <<-"end_eval", __FILE__, __LINE__
      def #{helper_name}(&block)
        #{model_name}.bound_by_#{options[:by]}(#{options[:scope_function]}.id, &block)
      end
      
      alias_method :bound_by_#{options[:by]}, :#{helper_name}
  end_eval
end