Sha256: edd3e1292fa372790fa9a8b2e721765e5742c0b23ff64d542d8a737e35dc04cc

Contents?: true

Size: 885 Bytes

Versions: 53

Compression:

Stored size: 885 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # A concern with the components needed when you want a model to have a component.
  module Randomable
    extend ActiveSupport::Concern

    class_methods do
      # Public: Randomly orders a collection given a seed.
      def order_randomly(seed)
        transaction do
          connection.execute("SELECT setseed(#{connection.quote(seed)})")
          # Include the record IDs as a base number for the order calculation
          # in order to avoid PostgreSQL random ordering when the records are
          # updated. PostgreSQL can randomly change the base ordering in case
          # the records are changed which is not desired as we want consistent
          # orders for the records.
          order(arel_table[primary_key] * Arel.sql("RANDOM()")).load
        end
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
decidim-core-0.30.0.rc1 lib/decidim/randomable.rb
decidim-core-0.29.2 lib/decidim/randomable.rb
decidim-core-0.28.5 lib/decidim/randomable.rb
decidim-core-0.29.1 lib/decidim/randomable.rb
decidim-core-0.28.4 lib/decidim/randomable.rb
decidim-core-0.27.9 lib/decidim/randomable.rb
decidim-core-0.29.0 lib/decidim/randomable.rb
decidim-core-0.28.3 lib/decidim/randomable.rb
decidim-core-0.27.8 lib/decidim/randomable.rb
decidim-core-0.29.0.rc4 lib/decidim/randomable.rb
decidim-core-0.29.0.rc3 lib/decidim/randomable.rb
decidim-core-0.29.0.rc2 lib/decidim/randomable.rb
decidim-core-0.29.0.rc1 lib/decidim/randomable.rb
decidim-core-0.28.2 lib/decidim/randomable.rb
decidim-core-0.27.7 lib/decidim/randomable.rb
decidim-core-0.28.1 lib/decidim/randomable.rb
decidim-core-0.27.6 lib/decidim/randomable.rb
decidim-core-0.26.10 lib/decidim/randomable.rb
decidim-core-0.26.9 lib/decidim/randomable.rb
decidim-core-0.28.0 lib/decidim/randomable.rb