Sha256: 570cc975806b5846b170e8ebd6608789bfd561755bc8b531ed4148da39c5eddf

Contents?: true

Size: 1.67 KB

Versions: 14

Compression:

Stored size: 1.67 KB

Contents

require 'active_support/inflector'

module CanTango
  module Rules
    class UserRelation
      attr_reader :attribute, :permit, :scope, :models

      include CanTango::Rules::Adaptor
      include CanTango::Rules::RuleClass

      def initialize attribute, permit, scope, *models, &block
        @attribute = attribute
        @scope = scope
        @permit = permit
        @models = models

        check_models
        use_adaptor! self, user_scope
      end

      def can(action)
        models.each do |model|
          rules << rule_class.new(true, action, model, nil, condition_block(model))
        end
      end

      def cannot(action)
        models.each do |model|
          rules << rule_class.new(false, action, model, nil, condition_block(model))
        end
      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) ? permit.user_account : permit.user
      end

      def scope_key
        @scope_key ||= (scope == :account) ? :user_account : :user
      end

      def ability
        permit.ability
      end

      def rules
        ability.send :rules # FIXME !?
      end

      def plural_attribute
        :"#{attribute.to_s.pluralize}"
      end

      def check_models
        models.each do |model|
          raise "#{model} has no :#{attribute} or :#{plural_attribute} defined" if !model.new.respond_to?(attribute) && !model.new.respond_to?(plural_attribute)
        end
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cantango-0.9.3.2 lib/cantango/rules/user_relation.rb
cantango-0.8.9.5 lib/cantango/rules/user_relation.rb
cantango-0.8.9.4 lib/cantango/rules/user_relation.rb
cantango-0.8.9.3 lib/cantango/rules/user_relation.rb
cantango-0.8.9.2 lib/cantango/rules/user_relation.rb
cantango-0.8.9.1 lib/cantango/rules/user_relation.rb
cantango-0.8.9 lib/cantango/rules/user_relation.rb
cantango-0.8.8.1 lib/cantango/rules/user_relation.rb
cantango-0.8.8 lib/cantango/rules/user_relation.rb
cantango-0.8.7 lib/cantango/rules/user_relation.rb
cantango-0.8.6.2 lib/cantango/rules/user_relation.rb
cantango-0.8.6.1 lib/cantango/rules/user_relation.rb
cantango-0.8.6 lib/cantango/rules/user_relation.rb
cantango-0.8.5.1 lib/cantango/rules/user_relation.rb