DISQUS

James on Software: Introducing ActivePresenter: The presenter library you already know.

  • lucashungaro · 1 year ago
    Wow! Very nice. Congratulations once more!
  • Piotr Usewicz · 1 year ago
    This is simply brilliant!
  • Eric Anderson · 1 year ago
    The API looks good and I am sure the implementation is nice but I'm not sure I sold on the concept of a presenter. Seems like you are for the most part the developer is just implementing their controller in another object and then including a reference to the object in the controller. Almost like a mixin for controllers only.

    There might be a few cases where you would need the same forms in multiple controllers (i.e. the abstraction and reuse you provide with a presenter) but it seems that that would be a rare case. Is it worth the mental overhead of having this "presenter" concept for those few rare cases? My preference would be to just reuse the mixin concept people are already familiar with (i.e. implement the various REST actions on a module then mixin to multiple controllers). This would seem to give you the same reuse the presenter provides without the additional mental overhead of an entirely new concept.

    Just my two cents
  • macournoyer · 1 year ago
    awesome work James & Daniel!

    @Eric, this is an implementation of the Presenter design pattern: http://blog.jayfields.com/2007/03/rails-present...
  • Mike Subelsky · 1 year ago
    hey James, great work!

    I led a BOF session at Railsconf 2008 on this topic, and there's definitely a big interest and need for this gem. In fact I registered "active_presenter" on RubyForge, but never got around to doing anything with it, so if you want to publish this gem there, I'd be glad to transfer the project to you. Just hit me up with an email at mike /-at-/ subelsky.com.

    -Mike
  • Daniel Haran · 1 year ago
    @Eric Anderson: If you can avoid multi-object creation, then more power to you. This is indeed only for rare cases - although I think you confuse things by calling it a mixin. Check out the link macournoyer to get a better sense of what this encapsulates.
  • Mathijs Kwik · 1 year ago
    Will this gem also handle cases where I want to create/edit a single model with some 'child' models?

    So one project with (to leave javascript out of the equation) 10 tasks in a single form for creating and updating the tasks?
    What about deleting them?
  • James Golick · 1 year ago
    @Mathijs: It doesn't yet, but we're hoping to have that functionality going soon.
  • Swami Atma · 1 year ago
    Hi James,

    Nice article.

    We met in Berlin last year. I need to send you an email. Can you email me to the address included in this form?

    Thanks.
  • John Corrigan · 1 year ago
    Hiya, I can't seem to grab this gem. I'm getting not found errors when I run the gem install command. Thoughts?
  • James Golick · 1 year ago
    @John - No idea. I can run it ok. You're trying to download it from rubyforge?
  • Geoff Garside · 1 year ago
    This looks much nicer than the presenter plugin I slapped together, thanks.
  • Michail · 1 year ago
    Thanks! It looks good. But what if there are equal attribute names? Like User.name and Account.name. What form will be in this case?
  • Rob · 1 year ago
    This looks great for multiple unrelated models or two models with a belongs_to relationship, but doesn't handle has_many scenarios, correct?

    <rant>
    Why are multi-model forms so fraking hard (no standard solution, gotta write extra code, little documentation on the solutions that exists) in Rails when WebObjects was doing this 10 years ago with keypaths?
    </rant>
  • Nate · 1 year ago
    Awesome stuff. I do have a question though. After installing the plugin and then trying to run rake routes I get an error: uninitialized constant ActivePresenter.

    I installed from Github as a plugin. Also, when using the presenter to create a user and an account I'm getting some weird posting issues.

    so my AccountSignupPresenter presents :account, :user

    then on my form I have this: form_for @account_signup_presenter, :url => accounts_path

    for some reason everything looks great and works, but the form is posting to the accounts index action. It obviously should post to the accounts create action.

    Any help would be appreciated. Thanks again for the great work!
  • Mark A. Richman · 1 year ago
    @Nate, have you tried something like this:

    :url => { :controller => 'accounts', :action => :create }

    I haven't tried active_presenter yet, but it does seem intriguing.
  • Max · 1 year ago
    Hi, I've been using it and works really nice. Any plans to include a before_validation callback?
  • James Golick · 1 year ago
    @Max - Sure. If there's a need for one, I can definitely add it. Could you not put that validation in one of the models, though?
  • Zach Dennis · 1 year ago
    ActivePresenter seems to be doing two things: handling presentation logic for one or more models, and also handling save/update logic for more than one model. It's meddling in two distinct and completely separate responsibilities. This seems to encourages bad practice when it comes to dealing with either presentation logic and/or multiple model forms. Presenters should only be handling presentation logic. The save/update logic should be done in the model, or if it can't be done there in a domain Service/Manager object.

    CachingPresenter is a strict presentation-logic presenter. It does cool stuff with caching/memoization. Should it provide useful to James or anyone else seeing this post please check it out:

    * Github - http://github.com/zdennis/caching_presenter/

    * Docs - http://github.com/zdennis/caching_presenter/wik...
  • Pat Osterday · 1 year ago
    I'd love to see a full example of this working with ResourceController. The Spree code doesn't really make full use of ActivePresenter and I didn't think the Blank app did either. I tried for hours to get it to work and gave up.

    If I have a User model with an Address as Addressable, how would this work? I tried doing a UserPresenter using the UserController directly, then a ProfilePresenter using a seperate ProfileController and nothing would work.

    In particular an edit example would be great. Or maybe I just missed something!

    Anyway... great work!
  • Pat Osterday · 1 year ago
    Ok, I figured out some of my problems with ActivePresenter and ResourceController and it's working now except for "error_messages_for :payment_presenter". I always get "can't convert Array into String" errors. Not sure if you still check out these comments James, but any help would be appreciated.
  • Pat Osterday · 1 year ago
    Ok, sorry again... I guess there's some issues with the different versions of the gems/plugins I'm using. I switched to the version with Spree and now it's working. Thanks!
  • Samiha · 10 months ago
    Hey,
    James I found some problems when I used this gem with select_tags and names of models are like this

    presents :photographer,
    :user,
    :location,
    :photographer_award,
    :photographers_language,
    :photographer_nationality,
    :photographer_reference,
    :visa

    so when I want to put <%= f.select :photographer_language_language_id, [[l(:select_one), nil]] + @languages.collect{|language| [l(:spoken_language, language.name.to_sym), language.id]}, :id => nil %>

    and photographer_language_language_id is to catch language_id field from photographer_languages table
  • Steven Luscher · 9 months ago
    @nate – About your uninitialized constant problem… find active_presenter/lib/tasks/gem.rake and add this to the top:

    require File.expand_path(File.dirname(__FILE__) + '/../active_presenter')