lib/ruhl/rails/ruhl_presenter.rb in ruhl-0.17.0 vs lib/ruhl/rails/ruhl_presenter.rb in ruhl-0.18.0

- old
+ new

@@ -7,14 +7,18 @@ include Ruhl::Rails::ActiveRecord include Ruhl::Rails::Helper attr_reader :presentee, :context - def initialize(obj, context) - @presentee = obj + def initialize(context, obj = nil) @context = context - define_paths(obj.class.name.underscore.downcase) + + # May only want to use the form helper + if obj + @presentee = obj + define_paths(obj.class.name.underscore.downcase) + end end def method_missing(name, *args) if presentee.respond_to?(name) # Pass presenter method call to model so you don't have to @@ -42,17 +46,31 @@ module ActionController class Base protected - def present(object_sym, action_sym) - render :template => "#{object_sym.to_s.pluralize}/#{action_sym}", - :locals => {:object => presenter_for( instance_variable_get("@#{object_sym}") )} + def present(action_sym = action_name, object = nil) + object_sym = object || controller_name.singularize + + render :template => "#{controller_name}/#{action_sym}", + :locals => {:object => presenter_for(object_sym) } end - def presenter_for(obj) - Object.const_get("#{obj.class.name}Presenter").new(obj, @template) + def presenter_for(object) + + if object.is_a?(Symbol) || object.is_a?(String) + # Set instance variable if it exists + if instance_variables.include?("@#{object}") + obj = instance_variable_get("@#{object}") + end + name = object.to_s.camelize + else + name = object.class.name.camelize + obj = object + end + + Object.const_get("#{name}Presenter").new(@template, obj) end - + helper_method :presenter_for end end