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.20 lib/satis/forms/concerns/options.rb
satis-2.1.19 lib/satis/forms/concerns/options.rb
satis-2.1.18 lib/satis/forms/concerns/options.rb
satis-2.1.11 lib/satis/forms/concerns/options.rb
satis-2.1.10 lib/satis/forms/concerns/options.rb
satis-2.1.9 lib/satis/forms/concerns/options.rb
satis-2.1.8 lib/satis/forms/concerns/options.rb
satis-2.1.6 lib/satis/forms/concerns/options.rb
satis-2.1.5 lib/satis/forms/concerns/options.rb
satis-2.1.4 lib/satis/forms/concerns/options.rb
satis-2.1.3 lib/satis/forms/concerns/options.rb
satis-2.1.1 lib/satis/forms/concerns/options.rb
satis-2.1.0 lib/satis/forms/concerns/options.rb
satis-2.0.9 lib/satis/forms/concerns/options.rb
satis-2.0.8 lib/satis/forms/concerns/options.rb
satis-2.0.7 lib/satis/forms/concerns/options.rb
satis-1.0.75 lib/satis/forms/concerns/options.rb
satis-1.0.74 lib/satis/forms/concerns/options.rb
satis-1.0.70 lib/satis/forms/concerns/options.rb
satis-1.0.69 lib/satis/forms/concerns/options.rb