Sha256: 17d18d58952732c8a8c1914c0ec6534e0b37b410603073a8775a355a3092f06a

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

module Formtastic
  module Helpers
    # @private
    module Reflection
      # If an association method is passed in (f.input :author) try to find the
      # reflection object.
      def reflection_for(method) # @private
        if @object.class.respond_to?(:reflect_on_association)
          @object.class.reflect_on_association(method)
        elsif @object.class.respond_to?(:associations) # MongoMapper uses the 'associations(method)' instead
          @object.class.associations[method]
        end
      end

      def association_macro_for_method(method) # @private
        reflection = reflection_for(method)
        reflection.macro if reflection
      end

      def association_primary_key_for_method(method) # @private
        reflection = reflection_for(method)
        if reflection
          case association_macro_for_method(method)
          when :has_and_belongs_to_many, :has_many, :references_and_referenced_in_many, :references_many
            :"#{method.to_s.singularize}_ids"
          else
            return reflection.foreign_key.to_sym if reflection.respond_to?(:foreign_key)
            return reflection.options[:foreign_key].to_sym unless reflection.options[:foreign_key].blank?
            :"#{method}_id"
          end
        else
          method.to_sym
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/formtastic-4.0.0/lib/formtastic/helpers/reflection.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/formtastic-4.0.0/lib/formtastic/helpers/reflection.rb
formtastic-4.0.0 lib/formtastic/helpers/reflection.rb
formtastic-4.0.0.rc1 lib/formtastic/helpers/reflection.rb
formtastic-3.1.5 lib/formtastic/helpers/reflection.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/formtastic-3.1.4/lib/formtastic/helpers/reflection.rb
formtastic-3.1.4 lib/formtastic/helpers/reflection.rb
formtastic-3.1.3 lib/formtastic/helpers/reflection.rb
formtastic-3.1.2 lib/formtastic/helpers/reflection.rb
formtastic-3.1.1 lib/formtastic/helpers/reflection.rb