Sha256: 75cb49f5db279d738bc250f4ab36ea2a00902cd9984920e73d91bc084c5ad440

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'kaminari/models/active_record_extension'

module ActiveAdmin

  # manually initialize kaminari for this model
  ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension

  class Comment < ActiveRecord::Base
    belongs_to :resource, :polymorphic => true
    belongs_to :author,   :polymorphic => true

    unless Rails::VERSION::MAJOR > 3 && !defined? ProtectedAttributes
      attr_accessible :resource, :resource_id, :resource_type, :body, :namespace
    end

    validates_presence_of :body, :namespace, :resource

    # @returns [String] The name of the record to use for the polymorphic relationship
    def self.resource_type(record)
      record.class.base_class.name.to_s
    end

    def self.resource_id_cast(record)
      # Postgres adapters won't compare strings to numbers (issue 34)
      if resource_id_type == :string
        record.id.to_s
      else
        record.id
      end
    end

    def self.find_for_resource_in_namespace(resource, namespace)
      where :resource_type => resource_type(resource),
            :resource_id   => resource_id_cast(resource),
            :namespace     => namespace.to_s
    end

    def self.resource_id_type
      columns.detect{ |i| i.name == "resource_id" }.type
    end

    def self.table_name
      @table_name ||= ActiveRecord::Migrator.proper_table_name("active_admin_comments")
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aa-rails4-0.6.0 lib/active_admin/comments/comment.rb