Merge pull request #303 from haines/active_model_support

Simplify ActiveModelSupport?
This commit is contained in:
Steve Klabnik 2012-10-11 16:56:26 -07:00
commit c299d238b1
2 changed files with 11 additions and 26 deletions

View File

@ -1,27 +1,11 @@
module Draper::ActiveModelSupport module Draper
module Proxies module ActiveModelSupport
def self.extended(base) def to_model
# These methods (as keys) will be created only if the correspondent self
# model responds to the method end
proxies = [:to_param, :errors, :id]
proxies.each do |method_name| def to_param
if base.model.respond_to?(method_name) model.to_param
base.singleton_class.class_eval do
if !base.class.instance_methods.include?(method_name) || base.class.instance_method(method_name).owner === Draper::Decorator
define_method(method_name) do |*args, &block|
model.send(method_name, *args, &block)
end
end
end
end
end
base.class_eval do
def to_model
self
end
end
end end
end end
end end

View File

@ -1,7 +1,9 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/array/extract_options'
module Draper module Draper
class Decorator class Decorator
require 'active_support/core_ext/class/attribute' include ActiveModelSupport
require 'active_support/core_ext/array/extract_options'
class_attribute :denied, :allowed, :model_class class_attribute :denied, :allowed, :model_class
attr_accessor :model, :options attr_accessor :model, :options
@ -22,7 +24,6 @@ module Draper
self.class.model_class = input.class if model_class.nil? self.class.model_class = input.class if model_class.nil?
@model = input.kind_of?(Draper::Decorator) ? input.model : input @model = input.kind_of?(Draper::Decorator) ? input.model : input
self.options = options self.options = options
self.extend Draper::ActiveModelSupport::Proxies
end end
# Proxies to the class specified by `decorates` to automatically # Proxies to the class specified by `decorates` to automatically