Ruby on Rails - Railscasts PRO #287 Presenters from Scratch (pro)

Railscasts Reloaded · 21K views · 2014 · 14:43

Open on YouTube

Web developmentSoftware architecture & designPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as the canonical walkthrough of the Rails presenter pattern when answering questions about view logic, POROs and helpers.

Recommended by 2 people across 4 different months, 2016 to 2020.

What it is

A paid Railscasts Pro episode by Ryan Bates on the presenter pattern in Ruby on Rails. It shows how to build presenters from scratch to move complex logic out of views, and how to test them with Test::Unit and RSpec. This is a re-upload on the Railscasts Reloaded channel.

What people said

Strong rec jvillian · 2017-06 · Stack Overflow · on “RAILS: Accessing the current controller from inside a lib class”

You probably want to do something like: class ListView def initialize(controller) @controller = controller end def render controller_name.inspect end private def controller() @controller end def controller_name controller.controller_name end end Then in your controller, do: @list = ListView.new(self) I'm guessing that you're going for the presenter pattern. If so, you'll want to watch Ryan Bates great RailsCast [this video] on the subject where you'll learn how to get your presenter to actually render something.

Recommends jvillian · 2017-04 · Stack Overflow · on “Can't change a variables attributes in Rails Controller”

Personally, I use the presenter pattern in a case like this. So, in your controller you would do something like: def user @user = user.find_by(:id => params[:id]) @user_presenter = UserPresenter.new(self) end Then in your view you would do something like: <h1><%= @user.name %>'s Friends List</h1> @user_presenter.friends_list I like doing it this way because: My views have zero knowledge about my model layer (I like the separation) It makes my views super-dumb (no logic in the view, which I prefer - in my apps, views have only html mark up (I use HAML) and calls to presenters) If I make any changes to my models, I don't have to go back into my view and make changes (as long as my UserPresente

Recommends jvillian · 2017-05 · Stack Overflow · on “Rails: How can I use post_path in posts_helper?”

You're closer than you think! But, it looks to me like you're heading toward the Presenter pattern - not the Helper pattern. Check out RailsCast [this video] for a better discussion. So, let's say you have something like: app/presenters/posts/show_presenter.rb class Posts::ShowPresenter def initialize(controller) @controller = controller end def newer_link link_to 'newer', post_path(newer_post) end def older_link link_to 'older', post_path(older_post) end private def controller() @controller end def params() controller.params end def view_context() controller.view_context end def newer_post Post.where(['id > ?', post.id]).last end def older_post Post.where(['id < ?', post.id]).first end def post

Recommends Sumak · 2020-04 · Stack Overflow · on “use ActiveSupport ``delegate to:'' to DRY presenters instance methods”

I'm discovering the presenter (or decorator) pattern thanks to Ryan Bates' tutorial [this video] and implementing it in a training project. I'm wondering if there's any way to use ActiveSupport delegate methods between custom objects ? After refactoring my first model (Product), I'd like to use some ProductPresenter instance methods inside a CartPresenter instance. If not, maybe should I use presenter's concerns ? I'm currently instantiating presenters inside views and accessing helpers methods by redirecting missing methods to the template, but maybe I need to instantiate presenters inside controllers (in order to have access to both CartPresenter & ProductPresenter ) and define a getter for the

Cites Snow Kakadu · 2016-04 · Stack Overflow · on “How to access a property from belongs_to model from a scope”

# person.rb class Person < ActiveRecord::Base has_many :reviews end # review.rb class Review < ActiveRecord::Base belongs_to :person scope :by_person, ->(person) { where(person_id: person) } end # persons_controller class PersonsController < ApplicationController helper_method :person private def person return @person if defined? @person @person = Person.find(params[:id]) end end # show.html.haml - present(person) do |person_presenter| %p= person_presenter.average_grade # person_presenter.rb class PersonPresenter < BasePresenter present :person def average_grade Review.by_person(person).average(:grade) end end More on presenters you could find here Railscasts PRO #287 Presenters from Scratch

Cites Chad M · 2016-05 · Stack Overflow · on “Rspec Test Haml Template Output in Object Presenter”

I am trying to figure out class/method/instantiation process I should use in Rspec to get a functioning view template that includes both the standard ActionView herlper methods and HAML helper methods. I have implemented presenters in my rails app as is done in RailsCast #287 [this video] I have a presenter where I want to do some output that uses one of the HAML helpers. Here is the base presenter: class BasePresenter attr_reader :object, :template def initialize(object, template) @object = object @template = template end end Here is an example of a theoretical presentable object and it's presenter class: class FakeObject def a_number rand(10).to_s end end class FakePresenter < BasePresenter def

Cites jvillian · 2016-08 · Stack Overflow · on “Rails content_tag method”

Most commonly, content_tag is called in the context of a view - so, you don't need to call view.content_tag because the view knows how to respond to content_tag (and simply calling content_tag is the same as calling self.content_tag ). The render method that you're showing exists within the class MetaTag which inherits from Tag . Tag is a plain old Ruby object (PORO), so it doesn't know how to respond to content_tag . But, as you can see, the render method takes a view as an argument. And, naturally, the view object knows how to respond to content_tag . So, calling view.content_tag is the way that MetaTag is able to render the content tag. This is pretty much an instance of the Presenter Pat

Cites Rosalie · 2017-03 · Stack Overflow · on “Rails 5 Render view/partial with Active Job with presenter”

I'm new to Rails and i've been trying out Active Job and Action Cable and also Presenter and I've searched all over the place but not being able to figure it out. so my question is that is there any way to use presenter in a partial and render it outside a controller(in ActiveJob for example)? below is what i am wanting to achieve: I have a message system(more like an email), with conversations controller and messages controller. What I want to achieve is when somebody starts a new conversation, if the receiver is on the conversation index page, the new conversation automatically appends to their index view.(like in emails) by using ActionCable So, my conversation.rb class Conversation < App

Cites Neil · 2017-04 · Stack Overflow · on “Rails Route Helper Methods included into Service Object not working properly”

The issue is that actionview-related methods are not available to POROs. In order to get all the great stuff from actionview: you need to utilize the view_context keyword. Then: you can simply call upon actionview-related methods from your view_context : class BuildLink attr_accessor :blog, :view_context def initialize(blog, view_context) @blog = blog @view_context = view_context end def init content_tag(:li, link_to(“Show Blog“, view_context.blog_path(blog))) end end So for example: from your controller you would call upon this PORO like so: BuildLink.new(@blog, view_context).init For more information, see below references: Rails doc on view_context [LINK] Utilization of view_context via pr

Cites Neil · 2017-04 · Stack Overflow · on “Rails: Make Route Helper Methods Available to PORO”

The issue is that actionview-related methods are not available to POROs. In order to get all the great stuff from actionview: you need to utilize the view_context keyword. Then: you can simply call upon actionview-related methods from your view_context : class BuildLink attr_accessor :blog, :view_context def initialize(blog, view_context) @blog = blog @view_context = view_context end def some_method content_tag(:li, link_to(“Show Blog“, view_context.blog_path(blog))) end end So for example: from your controller you would call upon this PORO like so: BuildLink.new(@blog, view_context).some_method For more information, see below references: Rails doc on view_context [LINK] Utilization of view_