Use Orm Adapter gem to handle conditions/order from associations

This commit is contained in:
Carlos Antonio da Silva 2013-05-07 23:43:50 -03:00
parent 30e81fedb5
commit 12a6dd2440
5 changed files with 22 additions and 14 deletions

View File

@ -4,6 +4,7 @@ PATH
simple_form (3.0.0.beta1)
actionpack (>= 4.0.0.rc1, < 4.1)
activemodel (>= 4.0.0.rc1, < 4.1)
orm_adapter (~> 0.4.0)
GEM
remote: https://rubygems.org/
@ -31,6 +32,7 @@ GEM
json (1.7.7)
minitest (4.7.4)
multi_json (1.7.2)
orm_adapter (0.4.0)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)

View File

@ -1,4 +1,5 @@
require 'action_view'
require 'orm_adapter'
require 'simple_form/action_view_extensions/form_helper'
require 'simple_form/action_view_extensions/builder'
require 'active_support/core_ext/hash/slice'

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.to_adapter.find_all(reflection.options.slice(:conditions, :order))
}
attribute = case reflection.macro

View File

@ -22,4 +22,5 @@ Gem::Specification.new do |s|
s.add_dependency('activemodel', '>= 4.0.0.rc1', '< 4.1')
s.add_dependency('actionpack', '>= 4.0.0.rc1', '< 4.1')
s.add_dependency('orm_adapter', '~> 0.4.0')
end

View File

@ -1,3 +1,15 @@
Adapter = Struct.new(:all) do
def find_all(options)
if options[:conditions]
[all.first]
elsif options[:order]
[all.last]
else
all
end
end
end
Association = Struct.new(:klass, :name, :macro, :options)
Column = Struct.new(:name, :type, :limit) do
@ -12,15 +24,11 @@ Company = Struct.new(:id, :name) do
include ActiveModel::Conversion
def self.all(options={})
all = (1..3).map { |i| Company.new(i, "Company #{i}") }
(1..3).map { |i| new(i, "#{name} #{i}") }
end
if options[:conditions]
[all.first]
elsif options[:order]
[all.last]
else
all
end
def self.to_adapter
Adapter.new all
end
def persisted?
@ -28,11 +36,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)