Sha256: bfd8197b031315195b1248581c97b49885dbe42266218ee1db8c68c599be3bf3

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'activerecord/sortable/acts_as_sortable/instance_methods'

require 'active_support'

module ActiveRecord
  module Sortable
    module ActsAsSortable
      extend ::ActiveSupport::Concern

      module ClassMethods
        def acts_as_sortable(&block)
          options = {
            relation: ->(instance) { instance.class },
            append: false,
            position_column: :position
          }
          block.call(options) if block_given?

          cattr_accessor :sortable_relation, instance_reader: false, instance_writer: false
          cattr_accessor :sortable_append, instance_reader: true, instance_writer: false
          cattr_accessor :sortable_position_column, instance_reader: true, instance_writer: false

          self.sortable_relation = options[:relation]
          self.sortable_append = options[:append]
          self.sortable_position_column = options[:position_column]

          scope "ordered_by_#{sortable_position_column}_asc".to_sym, -> { order(arel_table[sortable_position_column].asc) }

          before_create :sortable_set_default_position
          after_destroy :sortable_shift_on_destroy

          include ActiveRecord::Sortable::ActsAsSortable::InstanceMethods
        end
      end
    end
  end
end

require 'active_record'
ActiveRecord::Base.send :include, ActiveRecord::Sortable::ActsAsSortable

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-sortable-0.0.4 lib/activerecord/sortable/acts_as_sortable.rb