mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Add support scopes to be used in associations.
This commit is contained in:
parent
d98c645ce4
commit
6e7a5da9ca
3 changed files with 31 additions and 20 deletions
|
@ -449,22 +449,26 @@ module SimpleForm
|
|||
|
||||
private
|
||||
|
||||
def fetch_association_collection(reflection, options)
|
||||
def fetch_association_collection(reflection, options) #:nodoc:
|
||||
options.fetch(:collection) do
|
||||
conditions = reflection.options[:conditions]
|
||||
conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)
|
||||
relation = reflection.klass.all
|
||||
|
||||
relation = reflection.klass.where(conditions)
|
||||
|
||||
if relation.respond_to?(:order)
|
||||
relation.order(reflection.options[:order])
|
||||
if reflection.respond_to?(:scope) && reflection.scope
|
||||
relation = reflection.klass.instance_exec(&reflection.scope)
|
||||
else
|
||||
relation
|
||||
order = reflection.options[:order]
|
||||
conditions = reflection.options[:conditions]
|
||||
conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)
|
||||
|
||||
relation = relation.where(conditions)
|
||||
relation = relation.order(order) if relation.respond_to?(:order)
|
||||
end
|
||||
|
||||
relation
|
||||
end
|
||||
end
|
||||
|
||||
def build_association_attribute(reflection, association, options)
|
||||
def build_association_attribute(reflection, association, options) #:nodoc:
|
||||
case reflection.macro
|
||||
when :belongs_to
|
||||
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
|
||||
|
|
|
@ -113,6 +113,14 @@ class AssociationTest < ActionView::TestCase
|
|||
assert_no_select 'form select option[value=3]'
|
||||
end
|
||||
|
||||
test 'builder allows collection to have a scope' do
|
||||
with_association_for @user, :special_pictures
|
||||
assert_select 'form select.select#user_special_picture_ids'
|
||||
assert_select 'form select option[value=3]', '3'
|
||||
assert_no_select 'form select option[value=1]'
|
||||
assert_no_select 'form select option[value=2]'
|
||||
end
|
||||
|
||||
test 'builder marks the record which already belongs to the user' do
|
||||
@user.company_id = 2
|
||||
with_association_for @user, :company, as: :radio_buttons
|
||||
|
|
|
@ -23,21 +23,17 @@ Picture = Struct.new(:id, :name) do
|
|||
extend ActiveModel::Naming
|
||||
include ActiveModel::Conversion
|
||||
|
||||
class << self
|
||||
delegate :where, to: :_relation
|
||||
end
|
||||
|
||||
def self._relation
|
||||
all
|
||||
def self.where(conditions = nil)
|
||||
if conditions.is_a?(Hash) && conditions[:name]
|
||||
all.to_a.last
|
||||
else
|
||||
all
|
||||
end
|
||||
end
|
||||
|
||||
def self.all
|
||||
Relation.new((1..3).map { |i| new(i, "#{name} #{i}") })
|
||||
end
|
||||
|
||||
def persisted?
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
Company = Struct.new(:id, :name) do
|
||||
|
@ -74,7 +70,8 @@ class User
|
|||
:delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
|
||||
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
|
||||
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
|
||||
:extra_special_company_id, :pictures, :picture_ids
|
||||
:extra_special_company_id, :pictures, :picture_ids, :special_pictures,
|
||||
:special_picture_ids
|
||||
|
||||
def self.build(extra_attributes = {})
|
||||
attributes = {
|
||||
|
@ -156,6 +153,8 @@ class User
|
|||
Association.new(Company, association, :belongs_to, nil, { conditions: proc { { id: self.id } } })
|
||||
when :pictures
|
||||
Association.new(Picture, association, :has_many, nil, {})
|
||||
when :special_pictures
|
||||
Association.new(Picture, association, :has_many, proc { where(name: self.name) }, {})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue