Sha256: 8ba980edf19b153a6447eff41853130359fe157849ea4a923a742a2ced22bf84

Contents?: true

Size: 1.5 KB

Versions: 43

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module Satis
  module Forms
    module Concerns
      module Options
        extend ActiveSupport::Concern

        included do
          def value_text_methods(collection)
            # An array of models
            if collection.is_a?(Array) && collection.first.class < ActiveRecord::Base
              value_method ||= :id
              text_method ||= :name
            # An array of arrays, whereby the inner array is size 2: [[text,value],[text,value]]
            elsif collection.is_a?(Array) && collection.first.is_a?(Array) && collection.first.size == 2
              value_method ||= :second
              text_method ||= :first
            # An array:["textvalue","textvalue"]
            elsif collection.is_a?(Array) && !collection.first.is_a?(Array)
              value_method ||= :to_s
              text_method ||= :to_s
            # An activerecord relation
            elsif collection.class < ActiveRecord::Relation
              value_method ||= :id
              text_method ||= :name
            # An activerecord relation
            elsif collection.respond_to?(:each) && collection.first.respond_to?(:id) && collection.first.respond_to?(:name)
              value_method ||= :id
              text_method ||= :name
            # Whatever else
            else
              value_method ||= :second
              text_method ||= :first
            end
            [value_method, text_method]
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
satis-2.1.43 lib/satis/forms/concerns/options.rb
satis-2.1.42 lib/satis/forms/concerns/options.rb
satis-2.1.41 lib/satis/forms/concerns/options.rb
satis-2.1.40 lib/satis/forms/concerns/options.rb
satis-2.1.39 lib/satis/forms/concerns/options.rb
satis-2.1.38 lib/satis/forms/concerns/options.rb
satis-2.1.37 lib/satis/forms/concerns/options.rb
satis-2.1.36 lib/satis/forms/concerns/options.rb
satis-2.1.35 lib/satis/forms/concerns/options.rb
satis-2.1.33 lib/satis/forms/concerns/options.rb
satis-2.1.31 lib/satis/forms/concerns/options.rb
satis-2.1.30 lib/satis/forms/concerns/options.rb
satis-2.1.29 lib/satis/forms/concerns/options.rb
satis-2.1.28 lib/satis/forms/concerns/options.rb
satis-2.1.27 lib/satis/forms/concerns/options.rb
satis-2.1.26 lib/satis/forms/concerns/options.rb
satis-2.1.24 lib/satis/forms/concerns/options.rb
satis-2.1.23 lib/satis/forms/concerns/options.rb
satis-2.1.22 lib/satis/forms/concerns/options.rb
satis-2.1.21 lib/satis/forms/concerns/options.rb