Sha256: c81d69026b8fca589a9e257a23b9ae49b9c4beffae4cc3560a0fe475337e356f

Contents?: true

Size: 580 Bytes

Versions: 16

Compression:

Stored size: 580 Bytes

Contents

module CMS::Orderable
  extend ActiveSupport::Concern

  def order_scope
    self.class
  end

  module ClassMethods
    def orderable name
      default_scope order(name)
      after_save :"order_#{name}"

      define_method :"order_#{name}" do
        order_scope.where("#{name} >= #{send(name)}").where("id != #{id}").select(:id).select(name).inject(send(name)) do |i, record|
          record.update_column name, (i += 1) ; i
        end

        order_scope.all.inject(1) do |i, record|
          record.update_column name, i ; i + 1
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
honey-cms-0.3.8 lib/cms/orderable.rb
honey-cms-0.3.7 lib/cms/orderable.rb
honey-cms-0.3.5 lib/cms/orderable.rb
honey-cms-0.3.4 lib/cms/orderable.rb
honey-cms-0.3.3 lib/cms/orderable.rb
honey-cms-0.3.2 lib/cms/orderable.rb
honey-cms-0.3.1 lib/cms/orderable.rb
honey-cms-0.3.0 lib/cms/orderable.rb
honey-cms-0.2.4 lib/cms/orderable.rb
honey-cms-0.2.3 lib/cms/orderable.rb
honey-cms-0.2.2 lib/cms/orderable.rb
honey-cms-0.2.1 lib/cms/orderable.rb
honey-cms-0.2.0 lib/cms/orderable.rb
honey-cms-0.1.2 lib/cms/orderable.rb
honey-cms-0.1.1 lib/cms/orderable.rb
honey-cms-0.1.0 lib/cms/orderable.rb