Skip to content

Commit

Permalink
Let formtastic support Mongoid's relational macros (referenced_in, re…
Browse files Browse the repository at this point in the history
…ferences_many, references_and_referenced_in_many)

Also nonkey-patch Mongoid::Relations::Metadata for Formtastic
  • Loading branch information
radicaled committed Feb 18, 2011
1 parent ea7d9c6 commit dafeb9f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require File.join(File.dirname(__FILE__), *%w[formtastic i18n])
require File.join(File.dirname(__FILE__), *%w[formtastic util])
require File.join(File.dirname(__FILE__), *%w[formtastic railtie]) if defined?(::Rails::Railtie)
require File.join(File.dirname(__FILE__), %w[orm mongoid]) if defined?(Mongoid)

module Formtastic #:nodoc:

Expand Down Expand Up @@ -49,6 +50,10 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder

INLINE_ERROR_TYPES = [:sentence, :list, :first]

HAS_MANY_RELATIONSHIP_TYPES = [:has_many, :has_and_belongs_to_many, :references_many, :references_and_referenced_in_many]

BELONGS_TO_RELATIONSHIP_TYPES = [:belongs_to, :referenced_in]

attr_accessor :template

# Returns a suitable form input for the given +method+, using the database column information
Expand Down Expand Up @@ -293,7 +298,7 @@ def inputs(*args, &block)
field_set_and_list_wrapping(*(args << html_options), &block)
else
if @object && args.empty?
args = association_columns(:belongs_to)
args = association_columns(*BELONGS_TO_RELATIONSHIP_TYPES)
args += content_columns
args -= RESERVED_COLUMNS
args.compact!
Expand Down Expand Up @@ -502,7 +507,7 @@ def error_keys(method, options)
@methods_for_error[method] ||= begin
methods_for_error = [method.to_sym]
methods_for_error << file_metadata_suffixes.map{|suffix| "#{method}_#{suffix}".to_sym} if is_file?(method, options)
methods_for_error << [association_primary_key(method)] if association_macro_for_method(method) == :belongs_to
methods_for_error << [association_primary_key(method)] if BELONGS_TO_RELATIONSHIP_TYPES.include?(association_macro_for_method(method))
methods_for_error.flatten.compact.uniq
end
end
Expand Down Expand Up @@ -813,7 +818,7 @@ def select_input(method, options)
html_options.delete(:multiple) if html_options[:multiple].nil?

reflection = reflection_for(method)
if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
if reflection && HAS_MANY_RELATIONSHIP_TYPES.include?(reflection.macro)
html_options[:multiple] = true if html_options[:multiple].nil?
html_options[:size] ||= 5
options[:include_blank] ||= false
Expand Down Expand Up @@ -1618,7 +1623,7 @@ def create_boolean_collection(options) #:nodoc:
#
def generate_association_input_name(method) #:nodoc:
if reflection = reflection_for(method)
if [:has_and_belongs_to_many, :has_many].include?(reflection.macro)
if HAS_MANY_RELATIONSHIP_TYPES.include?(reflection.macro)
"#{method.to_s.singularize}_ids"
elsif reflection.respond_to? :foreign_key
reflection.foreign_key
Expand Down
13 changes: 13 additions & 0 deletions lib/orm/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Mongoid
module Relations
# Monkey patch to allow for "options" hash onrelationship metadata.
class Metadata
def options
@options ||= {
:foreign_key => foreign_key,
:conditions => nil
}
end
end
end
end

0 comments on commit dafeb9f

Please sign in to comment.