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-1.0.68 lib/satis/forms/concerns/options.rb
satis-1.0.67 lib/satis/forms/concerns/options.rb
satis-1.0.66 lib/satis/forms/concerns/options.rb