Sha256: 0615af54a376da2b01405ed35c4cca1e48ad422fffa220410ea9e0f1988dbe59

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Canard
  module Adapters
    module ActiveRecord

      private

      def add_role_scopes(*args)
        options = args.extract_options!
        # TODO change to check has_roles_attribute?
        if active_record_table?
          valid_roles.each do |role|
            define_scopes_for_role role, options[:prefix]
          end

          # TODO change hard coded :role_mask to roles_attribute_name
          define_singleton_method(:with_any_role) do |*roles|
            where("#{role_mask_column} & :role_mask > 0", { :role_mask => mask_for(*roles) })
          end

          define_singleton_method(:with_all_roles) do |*roles|
            where("#{role_mask_column} & :role_mask = :role_mask", { :role_mask => mask_for(*roles) })
          end

          define_singleton_method(:with_only_roles) do |*roles|
            where("#{role_mask_column} = :role_mask", { :role_mask => mask_for(*roles) })
          end
        end
      end

      def active_record_table?
        respond_to?(:table_exists?) && table_exists?
      end

      # TODO extract has_roles_attribute? and change to has_roles_attribute? || super
      def has_roles_mask_accessors?
        active_record_table? && column_names.include?(roles_attribute_name.to_s) || super
      end

      def define_scopes_for_role(role, prefix=nil)
        include_scope = [prefix, String(role).pluralize].compact.join('_')
        exclude_scope = "non_#{include_scope}"

        define_singleton_method(include_scope) do
          where("#{role_mask_column} & :role_mask > 0", { :role_mask => mask_for(role) })
        end

        define_singleton_method(exclude_scope) do
          where("#{role_mask_column} & :role_mask = 0 or #{role_mask_column} is null", { :role_mask => mask_for(role) })
        end
      end

      def role_mask_column
        "#{quoted_table_name}.#{connection.quote_column_name roles_attribute_name}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
canard-0.5.0.pre lib/canard/adapters/active_record.rb