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_#{options[:by]}" helper_name = "#{model}_bound_by_#{options[:by]}" self.class_eval " def \#{helper_name}(&block)\n \#{model_name}.bound_by_\#{options[:by]}(\#{options[:scope_function]}.id, &block)\n end\n \n alias_method :bound_by_\#{options[:by]}, :\#{helper_name}\n end_eval\nend\n", __FILE__, __LINE__ |