Sha256: aae074a2f394838d4fbc55815935a0a2b05569ff59e61329626aded2701d7740

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

module ActiveRecordPresenter
  def error_messages?
    !presentee.errors.empty?
  end
  
  def error_messages
    return if presentee.errors.empty?
    presentee.errors.full_messages
  end
  
  def method_missing(name, *args)
    if presentee.respond_to?(name)
      # Pass presenter method call to model so you don't have to
      # redefine every model method in the presenter class.
      presentee.send(name, *args)
    elsif context.respond_to?(name)
      # Instead of saying context.link_to('Some site', some_path)
      # can just use link_to
      context.send(name, *args)
    end
  end

  # Extend scope of respond_to? to model.
  def respond_to?(name)  
    if super
      true
    else
      presentee.respond_to?(name)
    end
  end
  
  def define_paths(model)
    define_action(model, 'show')                      # show_path(@user)
    define_action(model, 'update')                    # update_path(@user)
    define_action(model, 'delete')                    # delete_path(@user)
    define_action("edit_#{model}", 'edit')            # edit_path(@user)
    define_action(model.pluralize, 'index', false)    # index_path
    define_action(model.pluralize, 'create', false)   # create_path
    define_action("new_#{model}", 'new', false)       # new_path
  end
  
  private
  
  def define_action(model, action, use_presentee = true)
    if use_presentee
      self.class.send(:define_method, "#{action}_path") do
        context.send("#{model}_path", presentee)
      end      
      self.class.send(:define_method, "#{action}_url") do
        context.send("#{model}_url", presentee)
      end      
    else
      self.class.send(:define_method, "#{action}_path") do
        context.send("#{model}_path")
      end      
      self.class.send(:define_method, "#{action}_url") do
        context.send("#{model}_url")
      end      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruhl-0.14.0 lib/ruhl/rails/active_record_presenter.rb
ruhl-0.13.0 lib/ruhl/rails/active_record_presenter.rb
ruhl-0.12.1 lib/ruhl/rails/active_record_presenter.rb