Skip to content

Commit

Permalink
Use Orm Adapter gem to handle conditions/order from associations
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed May 8, 2013
1 parent 30e81fe commit 12a6dd2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -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/
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/simple_form.rb
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/form_builder.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions simple_form.gemspec
Expand Up @@ -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
30 changes: 17 additions & 13 deletions test/support/models.rb
@@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 12a6dd2

Please sign in to comment.