Sha256: 4084a8a0fbcae80d55b52c27e4bf811403147c53f53b538a93ee0033156942de

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module Drafting
  module BaseClassMethods
    ALLOWED_DRAFT_OPTION_KEYS = [ :parent ]

    def has_drafts(options={})
      raise ArgumentError unless options.is_a?(Hash)
      raise ArgumentError unless options.keys.all? { |k| ALLOWED_DRAFT_OPTION_KEYS.include?(k) }

      class_attribute :draft_parent

      if options[:parent]
        parent_class = self.reflect_on_all_associations(:belongs_to).find { |a| a.name == options[:parent] }.try(:klass)
        raise ArgumentError unless parent_class

        unless parent_class.method_defined? :drafts
          parent_class.class_eval do
            def drafts(user)
              Draft.where(
                user: user,
                user_type: user.try(:class).try(:name),
                parent: self
              )
            end

            def self.child_drafts(user)
              Draft.where(
                user: user,
                user_type: user.try(:class).try(:name),
                parent_type: self.base_class.name
              )
            end
          end
        end

        self.draft_parent = options[:parent]
      end

      include Drafting::InstanceMethods
      extend Drafting::ClassMethods

      attr_accessor :draft_id
      after_create :clear_draft
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
drafting-0.5.1 lib/drafting/base_class_methods.rb
drafting-0.5.0 lib/drafting/base_class_methods.rb