Permalink
Browse files

Refactored associations. There is not Reflection#options for mongoid.

  • Loading branch information...
ebeigarts authored and justinfrench committed May 3, 2011
1 parent 6971a15 commit 80b05914492d188d84c739babdc0b3f92f4b4425
@@ -94,7 +94,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_for_method(method)] if association_macro_for_method(method) == :belongs_to
methods_for_error.flatten.compact.uniq
end
end
@@ -107,18 +107,6 @@ def has_errors?(method, options)
def render_inline_errors?
@object && @object.respond_to?(:errors) && Formtastic::FormBuilder::INLINE_ERROR_TYPES.include?(inline_errors)
end
-
- def association_macro_for_method(method) #:nodoc:
- reflection = reflection_for(method)
- reflection.macro if reflection
- end
-
- def association_primary_key(method)
- reflection = reflection_for(method)
- reflection.options[:foreign_key] if reflection && !reflection.options[:foreign_key].blank?
- :"#{method}_id"
- end
-
end
end
end
@@ -7,6 +7,27 @@ module Reflection
def reflection_for(method) #:nodoc:
@object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
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
@@ -2,29 +2,23 @@ module Formtastic
module Inputs
module Base
module Associations
+ include Formtastic::Helpers::Reflection
# :belongs_to, etc
def association
- @association ||= reflection.macro if reflection
+ @association ||= association_macro_for_method(method)
end
def reflection
- @reflection ||= object.class.reflect_on_association(method) if object.class.respond_to?(:reflect_on_association)
+ @reflection ||= reflection_for(method)
end
def belongs_to?
association == :belongs_to
end
def association_primary_key
- if association
- return reflection.options[:foreign_key] unless reflection.options[:foreign_key].blank?
- return reflection.foreign_key if reflection.respond_to?(:foreign_key)
- return :"#{method}_id" if belongs_to?
- return "#{method.to_s.singularize}_id".pluralize.to_sym
- else
- return method.to_s
- end
+ association_primary_key_for_method(method)
end
end
@@ -54,11 +54,11 @@ def collection_from_options
def collection_from_association
if reflection
- raise PolymorphicInputWithoutCollectionError.new("A collection must be supplied for #{method} input. Collections cannot be guessed for polymorphic associations.") if reflection.options[:polymorphic] == true
+ raise PolymorphicInputWithoutCollectionError.new("A collection must be supplied for #{method} input. Collections cannot be guessed for polymorphic associations.") if reflection.options && reflection.options[:polymorphic] == true
find_options_from_options = options[:find_options] || {}
conditions_from_options = find_options_from_options[:conditions] || {}
- conditions_from_reflection = reflection.options[:conditions] || {}
+ conditions_from_reflection = reflection.options && reflection.options[:conditions] || {}
if conditions_from_options.any?
reflection.klass.where(
@@ -31,23 +31,11 @@ def humanized_method_name
method.to_s.send(builder.label_str_method)
end
end
-
- # TODO this seems to overlap or be confused with association_primary_key
+
def input_name
- if reflection
- if [:has_and_belongs_to_many, :has_many].include?(reflection.macro)
- "#{method.to_s.singularize}_ids"
- elsif reflection.respond_to? :foreign_key
- reflection.foreign_key
- else
- reflection.options[:foreign_key] || "#{method}_id"
- end
- else
- method
- end.to_sym
+ association_primary_key
end
-
-
+
end
end
end
View
@@ -221,7 +221,7 @@ def new_author_path; "/authors/new"; end
when :main_post
mock('reflection', :options => {}, :klass => ::Post, :macro => :belongs_to)
when :mongoid_reviewer
- mock('reflection', :options => {}, :klass => ::Author, :macro => :referenced_in, :foreign_key => "reviewer_id") # custom id
+ mock('reflection', :options => nil, :klass => ::Author, :macro => :referenced_in, :foreign_key => "reviewer_id") # custom id
end
end

0 comments on commit 80b0591

Please sign in to comment.