Skip to content

Instantly share code, notes, and snippets.

@ileitch
Created October 18, 2012 23:46
Show Gist options
  • Save ileitch/3915461 to your computer and use it in GitHub Desktop.
Save ileitch/3915461 to your computer and use it in GitHub Desktop.
module ModelPresentable
class ModelPresentableError < StandardError; end
def self.included(base)
base.extend(ClassScope)
base.class_eval { include InstanceScope }
end
module InstanceScope
delegate :to_param, :to_key, :new_record?, :to => :@presentable
def presents_model(presentable)
@presentable = presentable
self.class.presentable_class = @presentable.class
end
end
module ClassScope
delegate :model_name, :to => :presentable_class
def presentable_class=(presentable_class)
@presentable_class = presentable_class
end
def presentable_class
if @presentable_class.nil?
raise ModelPresentableError, "#{self} does not call presents_model."
end
@presentable_class
end
end
end
# Example usage:
class MyPresenter
include ModelPresentable
def initialize(model_instance)
presents_model model_instance
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment