Sha256: c6c4676605e429a1fe4fccc8c69f08a298def3d64b067e45615f9b441e9caaf0
Contents?: true
Size: 1.72 KB
Versions: 1
Compression:
Stored size: 1.72 KB
Contents
require 'active_support/inflector' module CanTango::Api::Ability class MissingRelationError < StandardError; end class Relation attr_reader :attribute, :abil, :scope, :models include CanTango::Adaptor include CanTango::CanCan::RuleClass def initialize attribute, ability, scope, *models, &block @attribute = attribute @abil = ability @scope = scope @models = models check_models use_adaptor! self, user_scope end delegate :category, :any, :to => :ability def can(action) models.each do |model| rules << rule_class.new(true, action, model, nil, condition_block(model)) end self end def cannot(action) models.each do |model| rules << rule_class.new(false, action, model, nil, condition_block(model)) end self end protected def condition_block model return attribute_condition(attribute, user_scope) if model.new.respond_to?(attribute) return include_condition(plural_attribute, user_scope) if model.new.respond_to?(plural_attribute) end def user_scope @user_scope ||= (scope == :account) ? ability.account : ability.user end def scope_key @scope_key ||= (scope == :account) ? :user_account : :user end def ability abil.respond_to?(:ability) ? abil.ability : abil end def rules ability.send :rules end def plural_attribute :"#{attribute.to_s.pluralize}" end def check_models models.each do |model| raise MissingRelationError, "#{model} has no :#{attribute} or :#{plural_attribute} defined" if !model.new.respond_to?(attribute) && !model.new.respond_to?(plural_attribute) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cantango-api-0.1.3 | lib/cantango/api/ability/relation.rb |