Permalink
Browse files

Use Orm Adapter gem to handle conditions/order from associations

  • Loading branch information...
carlosantoniodasilva committed May 8, 2013
1 parent 30e81fe commit 12a6dd2440dee3b7b68f7ffe48acc105c95c9563
Showing with 22 additions and 14 deletions.
  1. +2 −0 Gemfile.lock
  2. +1 −0 lib/simple_form.rb
  3. +1 −1 lib/simple_form/form_builder.rb
  4. +1 −0 simple_form.gemspec
  5. +17 −13 test/support/models.rb
View
@@ -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
@@ -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'
@@ -180,7 +180,7 @@ def association(association, options={}, &block)
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
@@ -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
@@ -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,27 +24,19 @@ def number?
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?
true
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)

0 comments on commit 12a6dd2

Please sign in to comment.