Skip to content

Commit

Permalink
refactor collection_from_association
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfrench committed Jul 11, 2013
1 parent 1fe253f commit 6f996c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/formtastic/inputs/base/associations.rb
Expand Up @@ -12,6 +12,10 @@ def association
def reflection
@reflection ||= reflection_for(method)
end

def reflection_options?
reflection.respond_to?(:options)
end

def belongs_to?
association == :belongs_to
Expand Down
27 changes: 16 additions & 11 deletions lib/formtastic/inputs/base/collections.rb
Expand Up @@ -72,25 +72,30 @@ def collection_from_options

def collection_from_association
if reflection
if reflection.respond_to?(:options)
if reflection_options? && 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[:polymorphic] == true
"A collection must be supplied for #{method} input. Collections cannot be guessed for polymorphic associations."
)
end

conditions_from_reflection = (reflection.respond_to?(:options) && reflection.options[:conditions]) || {}
conditions_from_reflection = conditions_from_reflection.call if conditions_from_reflection.is_a?(Proc)

scope_conditions = conditions_from_reflection.empty? ? nil : {:conditions => conditions_from_reflection}

if Util.rails3?
reflection.klass.scoped(scope_conditions).all
reflection.klass.scoped(conditions_from_scope).all
else
reflection.klass.where(scope_conditions)
reflection.klass.where(conditions_from_scope)
end
end
end


def conditions_from_reflection
conditions = (reflection_options? && reflection.options[:conditions]) || {}
return conditions.call if conditions.is_a?(Proc)
return conditions
end

def conditions_from_scope
conditions_from_reflection.empty? ? nil : {:conditions => conditions_from_reflection}
end

def collection_for_boolean
true_text = options[:true] || Formtastic::I18n.t(:yes)
false_text = options[:false] || Formtastic::I18n.t(:no)
Expand Down

0 comments on commit 6f996c0

Please sign in to comment.