Execute the condition in the object context.

This commit is contained in:
Lauro Caetano 2014-04-01 11:57:42 -03:00
parent 4ec9305e70
commit 39188661d1
3 changed files with 41 additions and 31 deletions

View File

@ -1,6 +1,7 @@
## master
### enhancements
* Execute the association `condition` in the object context. [@laurocaetano](https://github.com/laurocaetano)
* Check if the given association responds to `order` before calling it. [@laurocaetano](https://github.com/laurocaetano)
* Add Bootstrap 3 initializer template.
* For radio or checkbox collection always use `:item_wrapper_tag` to wrap the content and add `label` when using `boolean_style` with `:nested` [@kassio](https://github.com/kassio) and [@erichkist](https://github.com/erichkist)

View File

@ -183,37 +183,9 @@ module SimpleForm
raise "Association #{association.inspect} not found" unless reflection
options[:as] ||= :select
options[:collection] ||= options.fetch(:collection) {
conditions = reflection.options[:conditions]
conditions = conditions.call if conditions.respond_to?(:call)
relation = reflection.klass.where(conditions)
options[:collection] ||= fetch_association_collection(reflection, options)
if relation.respond_to?(:order)
relation.order(reflection.options[:order])
else
relation
end
}
attribute = case reflection.macro
when :belongs_to
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
when :has_one
raise ArgumentError, ":has_one associations are not supported by f.association"
else
if options[:as] == :select
html_options = options[:input_html] ||= {}
html_options[:multiple] = true unless html_options.key?(:multiple)
end
# Force the association to be preloaded for performance.
if options[:preload] != false && object.respond_to?(association)
target = object.send(association)
target.to_a if target.respond_to?(:to_a)
end
:"#{reflection.name.to_s.singularize}_ids"
end
attribute = build_association_attribute(reflection, association, options)
input(attribute, options.merge(reflection: reflection))
end
@ -477,6 +449,43 @@ module SimpleForm
private
def fetch_association_collection(reflection, options)
options.fetch(:collection) do
conditions = reflection.options[:conditions]
conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)
relation = reflection.klass.where(conditions)
if relation.respond_to?(:order)
relation.order(reflection.options[:order])
else
relation
end
end
end
def build_association_attribute(reflection, association, options)
case reflection.macro
when :belongs_to
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
when :has_one
raise ArgumentError, ":has_one associations are not supported by f.association"
else
if options[:as] == :select
html_options = options[:input_html] ||= {}
html_options[:multiple] = true unless html_options.key?(:multiple)
end
# Force the association to be preloaded for performance.
if options[:preload] != false && object.respond_to?(association)
target = object.send(association)
target.to_a if target.respond_to?(:to_a)
end
:"#{reflection.name.to_s.singularize}_ids"
end
end
# Find an input based on the attribute name.
def find_input(attribute_name, options = {}, &block) #:nodoc:
column = find_attribute_column(attribute_name)

View File

@ -153,7 +153,7 @@ class User
when :special_company
Association.new(Company, association, :belongs_to, { conditions: { id: 1 } })
when :extra_special_company
Association.new(Company, association, :belongs_to, { conditions: proc { { id: 1 } } })
Association.new(Company, association, :belongs_to, { conditions: proc { { id: self.id } } })
when :pictures
Association.new(Picture, association, :has_many, {})
end