Sha256: 12d9fdcddf5cab2dd005b2a871a689851e22fa898767bc97fd25dc7a4e9b0f19

Contents?: true

Size: 671 Bytes

Versions: 8

Compression:

Stored size: 671 Bytes

Contents

# frozen_string_literal: true
module C
  module Orderable
    extend ActiveSupport::Concern

    included do
      has_one :_weight, dependent: :destroy, class_name: 'C::Weight', as: :orderable
      accepts_nested_attributes_for :_weight

      scope :ordered, -> { includes(:_weight).order('c_weights.value asc') }

      def _weight
        super || build__weight
      end

      def weight
        _weight.value
      end

      def weight=(val)
        _weight.value = val
      end

      def self.update_order(ordered_ids)
        order = ordered_ids.each_with_index.map { |_, i| { weight: i + 1 } }
        update(ordered_ids, order)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cd2_catton_cms-1.1.35 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.34 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.33 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.32 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.31 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.30 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.20 app/models/concerns/c/orderable.rb
cd2_catton_cms-1.1.10 app/models/concerns/c/orderable.rb