diff --git a/TODO.rdoc b/TODO.rdoc index 0d856858..d0cb7047 100644 --- a/TODO.rdoc +++ b/TODO.rdoc @@ -2,6 +2,8 @@ * Sample CSS * Add default size support +* HTML 5 support +* Pretty buttons == Validations diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 9e7b27ce..03a5499a 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -163,11 +163,13 @@ module SimpleForm end options[:collection] ||= begin - find_options = options.slice(:conditions, :order, :include, :joins) + finders = options.slice(:conditions, :order, :include, :joins) + finders[:conditions] = @reflection.klass.merge_conditions(finders[:conditions], + @reflection.options[:conditions]) klass = Array(options[:scope]).inject(@reflection.klass) do |klass, scope| klass.send(scope) end - klass.all(find_options) + klass.all(finders) end returning(input(attribute, options)) { @reflection = nil } diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 45fd63ff..b6eb55fd 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -462,6 +462,14 @@ class FormBuilderTest < ActionView::TestCase assert_no_select 'form select option[value=3]' end + test 'builder should use reflection conditions to find collection' do + with_association_for @user, :special_company + assert_select 'form select.select#user_special_company_id' + assert_select 'form select option[value=1]' + assert_no_select 'form select option[value=2]' + assert_no_select 'form select option[value=3]' + end + test 'builder should allow passing order to find collection' do with_association_for @user, :company, :order => 'name' assert_select 'form select.select#user_company_id' diff --git a/test/support/models.rb b/test/support/models.rb index 7027d2a0..13de64ad 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -6,19 +6,23 @@ Association = Struct.new(:klass, :name, :macro, :options) class Company < Struct.new(:id, :name) def self.all(options={}) all = (1..3).map{|i| Company.new(i, "Company #{i}")} - return [all.first] if options[:conditions] - return [all.last] if options[:order] - return all[0..1] if options[:include] - return all[1..2] if options[:joins] + return [all.first] if options[:conditions].present? + return [all.last] if options[:order].present? + return all[0..1] if options[:include].present? + return all[1..2] if options[:joins].present? all end + def self.merge_conditions(a, b) + (a || {}).merge(b || {}) + end + def new_record? false end end -class Tag < Struct.new(:id, :name) +class Tag < Company def self.all(options={}) (1..3).map{|i| Tag.new(i, "Tag #{i}")} end @@ -79,6 +83,8 @@ class User < OpenStruct Association.new(Tag, association, :has_many, {}) when :first_company Association.new(Company, association, :has_one, {}) + when :special_company + Association.new(Company, association, :belongs_to, { :conditions => { :id => 1 } }) end end