Module: Boundary::Controller
- Defined in:
- lib/boundary/controller.rb
Instance Method Summary collapse
-
#bound_to(model, *args, &block) ⇒ Object
Adds a scope helper that call the model with a given parameter (ie: subdomain).
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_bound_by_account { Subscription.find(params) }
@transaction = transaction_bound_by_account { 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) = args. model_name = model.to_s.camelize [:by] ||= :company [:scope_function] ||= "current_#{[:by]}" helper_name = "#{model}_bound_by_#{[:by]}" self.class_eval <<-"end_eval", __FILE__, __LINE__ def #{helper_name}(&block) #{model_name}.bound_by_#{[:by]}(#{[:scope_function]}.id, &block) end alias_method :bound_by_#{[:by]}, :#{helper_name} end_eval end |