Sha256: c872831561665d72ab035431405901bf9159e940c07e972d2c0cb6b883fea688

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module Eaco
  module Adapters

    ##
    # PostgreSQL 9.4 and up backing store for ACLs.
    #
    # @see ACL
    # @see PostgresJSONb
    #
    module ActiveRecord
      autoload :PostgresJSONb, 'eaco/adapters/active_record/postgres_jsonb'
      autoload :Compatibility, 'eaco/adapters/active_record/compatibility'

      ##
      # Currently defined collection extraction strategies.
      #
      # @return Hash
      #
      def self.strategies
        {:pg_jsonb => PostgresJSONb}
      end

      ##
      # Checks whether the model's data structure fits the ACL persistance
      # requirements.
      #
      # @param base [Class] your application's model
      #
      # @return void
      #
      def self.included(base)
        Compatibility.new(base).check!

        column = base.columns_hash.fetch('acl', nil)

        unless column
          raise Malformed, "Please define a jsonb column named `acl` on #{base}."
        end

        unless column.type == :json || column.type == :jsonb
          raise Malformed, "The `acl` column on #{base} must be of the json type."
        end
      end

      ##
      # @return [ACL] this Resource's ACL.
      #
      # @see ACL
      #
      def acl
        acl = read_attribute(:acl)
        self.class.acl.new(acl)
      end

      ##
      # Sets the Resource's ACL.
      #
      # @param acl [ACL] the new ACL to set.
      #
      # @return [ACL]
      #
      # @see ACL
      #
      def acl=(acl)
        write_attribute :acl, acl.to_hash
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eaco-0.6.1 lib/eaco/adapters/active_record.rb
eaco-0.6.0 lib/eaco/adapters/active_record.rb
eaco-0.5.0 lib/eaco/adapters/active_record.rb