1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Merge branch 'fix-relation-all-deprecation'

Relation#all is deprecated in Rails 4, use Relation#to_a instead.

Closes #759, thanks @tubaxenor.
Closes #798, no orm adapter for now.

Mongoid is known to not work with Simple Form collections automatically
since ever, users are supposed to give a :collection option when dealing
with associations, so this doesn't change that.

We may get better support for collections in mongoid in the future, but
lets get Simple Form out with better Rails 4 support for now, keeping
the same compatibility we always had.
This commit is contained in:
Carlos Antonio da Silva 2013-05-09 21:27:49 -03:00
commit 79ba6bfad1
3 changed files with 25 additions and 16 deletions

View file

@ -2,8 +2,9 @@
### enhancements
* Make `field_error_proc` configurable [@dfens](https://github.com/dfens)
### bug fix
* Remove deprecation warnings related to `Relation#all` from Rails 4.
* Form builder can be used outside the context of a controller [@jasonwebster](https://github.com/jasonwebster)
* Skip pattern attribute when using `validates_format_of` with `:without` option [@glebm](https://github.com/glebm)

View file

@ -180,7 +180,7 @@ module SimpleForm
options[:as] ||= :select
options[:collection] ||= options.fetch(:collection) {
reflection.klass.all(reflection.options.slice(:conditions, :order))
reflection.klass.where(reflection.options[:conditions]).order(reflection.options[:order]).to_a
}
attribute = case reflection.macro

View file

@ -7,20 +7,32 @@ Column = Struct.new(:name, :type, :limit) do
end
end
Relation = Struct.new(:all) do
def where(conditions = nil)
self.class.new conditions ? all.first : all
end
def order(conditions = nil)
self.class.new conditions ? all.last : all
end
alias_method :to_a, :all
end
Company = Struct.new(:id, :name) do
extend ActiveModel::Naming
include ActiveModel::Conversion
def self.all(options={})
all = (1..3).map { |i| Company.new(i, "Company #{i}") }
class << self
delegate :order, :where, to: :_relation
end
if options[:conditions]
[all.first]
elsif options[:order]
[all.last]
else
all
end
def self._relation
Relation.new(all)
end
def self.all
(1..3).map { |i| new(i, "#{name} #{i}") }
end
def persisted?
@ -28,11 +40,7 @@ Company = Struct.new(:id, :name) do
end
end
class Tag < Company
def self.all(options={})
(1..3).map { |i| Tag.new(i, "Tag #{i}") }
end
end
class Tag < Company; end
TagGroup = Struct.new(:id, :name, :tags)