Sha256: bbabcb39f9c2ea341fd1ed92b93a6b84748f0a0b366ac7ce6da6912d2a73439d

Contents?: true

Size: 1.29 KB

Versions: 20

Compression:

Stored size: 1.29 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) #:nodoc:
        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) #:nodoc:
        reflection = reflection_for(method)
        reflection.macro if reflection
      end

      def association_primary_key_for_method(method) #:nodoc:
        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

20 entries across 20 versions & 1 rubygems

Version Path
formtastic-3.1.0 lib/formtastic/helpers/reflection.rb
formtastic-3.1.0.rc2 lib/formtastic/helpers/reflection.rb
formtastic-3.1.0.rc1 lib/formtastic/helpers/reflection.rb
formtastic-3.0.0 lib/formtastic/helpers/reflection.rb
formtastic-3.0.0.rc2 lib/formtastic/helpers/reflection.rb
formtastic-2.3.1 lib/formtastic/helpers/reflection.rb
formtastic-2.3.0 lib/formtastic/helpers/reflection.rb
formtastic-3.0.0.rc lib/formtastic/helpers/reflection.rb
formtastic-2.3.0.rc4 lib/formtastic/helpers/reflection.rb
formtastic-2.3.0.rc3 lib/formtastic/helpers/reflection.rb
formtastic-2.3.0.rc2 lib/formtastic/helpers/reflection.rb
formtastic-2.3.0.rc lib/formtastic/helpers/reflection.rb
formtastic-2.2.1 lib/formtastic/helpers/reflection.rb
formtastic-2.2.0 lib/formtastic/helpers/reflection.rb
formtastic-2.2.0.rc lib/formtastic/helpers/reflection.rb
formtastic-2.1.1 lib/formtastic/helpers/reflection.rb
formtastic-2.1.0 lib/formtastic/helpers/reflection.rb
formtastic-2.1.0.rc lib/formtastic/helpers/reflection.rb
formtastic-2.1.0.beta1 lib/formtastic/helpers/reflection.rb
formtastic-2.0.2 lib/formtastic/helpers/reflection.rb